W2KSG: Using Array

If we want to extend our disk checking to multiple machines we can input the computer names via an array.

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > VBScript Primer > VBScript Overview Arrays

## listing 2.12
cls

$convert = 1MB
$computers = "pcrs2", "pcrs3", "pcrs4"
$threshold = 100

foreach ($computer in $computers) {
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk
    foreach ($disk in $disks) {
        $free = [int]($disk.Freespace/$convert)
        if ($free -lt $threshold){Write-Host "$computer $($disk.DeviceID) is low on diskspace"}
    }
}

We then do a simple foreach loop for each computer in the array and substitute its name into Get-WMIObject.  Note that the -Computername parameter of Get-WMIObject does not accept pipeline input so we need a foreach of some kind

 


Share this post :

 

Technorati Tags:

About these ads
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s