header image

W2KSG: Mutiple Runs

Posted by: | August 27, 2008 | No Comment |

We have a script that will run through the logical disks on a machine and return the free space.  How do we run that multiple times?

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

$convert = 1MB
$computer = "pcrs2"
#$pause = 3600     ## 1 hour
$pause = 10        ## 10 seconds

for ($i=1; $i -le 12; $i++){
## iterate through results collection
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk

Get-Date -DisplayHint Time

    foreach ($disk in $disks) {
        Write-Host "There are $([int]($disk.Freespace/$convert)) megabytes of free disk space on $($disk.DeviceID)"
    Start-Sleep -Seconds $pause

If we take our script from last time and warp it in another loop we can control how many times it runs.  In this case I’ve use a for loop with a counter that increments from 1-12. Within that loop we use get-date to display the time  and then display the free space.  PowerShell would run the loops continuously so we we use start-sleep to pause the script. For testing I used 10 seconds but you can choose any number of seconds – 3600 would be running it hourly.

It doesn’t take a lot of PowerShell to give some very useful functionality.


Share this post :


Technorati Tags:

under: PowerShell original