Handling Complex PowerShell Scripts

Handling simple scripts may be great for now, but taking a look at a more complicated script will probably give you a glimpse of what PowerShell is capable of. In this tutorial, you will get introduced to a far more complex script in PowerShell in order to reveal its true potential. This script will require that you have an Active Directory domain to connect to. This tutorial assumes that you have some knowledge about Active Directory and Windows Management Instrumentation (WMI).

Setup

If you have not already done so, click on your start button and type in Windows PowerShell ISE. Go ahead select and open Windows PowerShell ISE.

Step one.

This script will retrieve computer information from computers belonging to an active directory OU

Example

$ou = [ADSI] "LDAP://ou=test,dc=testlab,dc=local"
$computers = $ou.PSBase.Get_Children()
$arrInfo = @()
foreach($node in $computers) {
 $arrInfo += Get-WmiObject –query "Select `
 Name, Manufacturer, Model, `
 NumberOfProcessors, `
 TotalPhysicalMemory `
 From Win32_ComputerSystem" `
 -computername $node.Name
}
$arrInfo | format-table Name, Manufacturer, `
 Model, NumberOfProcessors, TotalPhysicalMemory

Copy and Try it

One thing we can take from this, the script is fairly powerful and yet it's only used this amount of lines for the code. With just these lines present we can query objects in Active Directory and also query the object's properties through WMI and output it in a table. The script assumes every item given is a computer. Of course there will need to be error checks and filters placed to be more accurate in the listing.

Step two.

In order to see how this script runs on your own system, go ahead and paste it. Change the LDAP path to an existing OU within your Active Directory, then save it as pclist.ps1. Run the script and observe the results.

Notice that some lines end with a back tick (`) character. It means that the next line is being continued on that one line and is not a separate command. This comes in handy when one has long commands such as this one, that might need a little, well, breaking up if you will.

Step three.

Let's break down the script. It is made up of three parts: part one connects to Active Directory and queries a list of computer names in a specific OU; part two queries each computer from the list to retrieve various computer properties using the Win32_ComputeSystem WMI class; and part three displays the results on the screen in a neat table.

Part One:

Example

$ou = [ADSI] "LDAP://ou=test,dc=testlab,dc=local"
 
$computers = $ou.PSBase.Get_Children()

Copy and Try it

The first line is the easiest way to make a connection to Active Directory using the Active Directory Services Interface (ADSI). You have to provide the correct path to an existing OU or container, or the script will return an error. The next step uses the PSBase.Get_Children function to return a collection (group) of objects that represent each item in the OU and store it in a variable called $computers.

Part Two:

Example

$arrInfo = @()
 
Foreach($node in $computers) {
 
                $arrInfo += Get-WmiObject –query "Select `
 
                Name, Manufacturer, Model, `
 
                NumberOfProcessors, `
 
                TotalPhysicalMemory `
 
                From Win32_ComputerSystem" `
 
                -computername $node.Name
 
}

Copy and Try it

The first thing you do is declare an array called $arrInfo. The second line starts a foreach loop where you perform one repetition of the loop for each item that's stored in the collection referenced to by the $computers variable. During each repetition of the loop, the current item is temporarily referred to as a $node.

Then you use Get-WmiObject to query the computers Win32_Computer System name space and then add this to the $arrInfo array using the += operator. Once the loop is done going through all the items in your OU, $arrInfo will contain a collection of objects that in turn have information on the various properties you queried through WMI.

Part Three:

Example

$arrInfo | format-table Name, Manufacturer, `
 
                Model, NumberOfProcessors, TotalPhysicalMemory

Copy and Try it

Here you submit the contents of the $arrInfo array into the Format-Table command, where you select which columns you want to display and in which order. Format-Table is then responsible for rendering these objects in a table.

Remarks last but not least…

So as you can see, PowerShell is truly a powerful thing. One of the biggest things in PowerShell is that it caters to people who don't have a strong programming background, that want to be able to automate things, and give them that ability. For those with programming background, PowerShell has advanced features that gives the user a far higher control over how to implement your solutions. Join us next time for more PowerShell tutorials