Monthly Archive


W2KSG: Making Decisions

Lets continue extending our PowerShell use and look at making decisions.  This corresponds to Listing 2.11 in the Scripting Guide -

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Concepts and Technologies for System Administration > VBScript Primer > VBScript Overview > Making Decisions Taking Multiple Actions by Using If Then Else

I'm skipping Listing 2.10 as is fully incorporated into 2.11.

We want to make some choices based on how much disk space is left.

## listing 2.11


$convert = 1MB
$computer = "pcrs2"
$threshold = 100

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

New addition is a $threshold variable which is our limit in megabytes of acceptable free space.  We use WMI as before and loop through the collection. This time we compute the free space and compare if to the threshold value. Note the use of -lt for less than.  PowerShell comparison operators are -eq, -ne, -lt, -le, -gt, -ge NOT the =, !=, <, <=, >, >= that you might be used to. Expect to have to double check these when you start with PowerShell!

If the free space is less than the threshold value we print a warning otherwise (else) we print a message stating its OK.  For completeness Listing 2.10 is the same as this but without the else statement,


Share this post :

Technorati Tags: PowerShell

Leave a Reply