Freigeben über


Perform asynchronous asset management using Windows Powershell cmdlets for WMI

With Windows 7 wmi cmdlets now have support to perform tasks asynchronously.

Asynchronous execution typically involves –

· Run commands in background

· Wait/stop on them

· Retrieve results

· Check status of execution

Thus to collect hard inventory data like total Physical Memory from a list of machines in your domain, while not blocking the original machine from which the inventory collection is being triggered, you’ll follow these steps -

Step 1: On a Windows Powershell console on your machine, run the following command -

PS C:\> $c = gwmi -class win32_computersystem -asjob -throttlelimit 2 –computername “machine1”,”machine2”,”mchine3”

Now you go about completing other tasks.

Two parameters to note here

1. Asjob – This parameters tells Windows Powershell that the specified task has to be run in background. By specifying -asjob, you have ensured that you are not blocked on the completion of this task.

2. Throttlelimit – you can choose to limit the number of simultaneous tasks started with this parameter. In the above case, we get data from only two machines at a time because the throttlelimit was specified as 2. When the data is returned from one of the two machines, only then is a connection to third machine made to get data. This gives you control on resource utilization (network bandwidth etc.)

Step 2: When you are ready to process the results of the above command, run the following on Windows Powershell console -

PS C:\> wait-job $c

When this commands returns you would have the results in $c.childJobs

Step3: Now you can use the powerful processing and reporting capabilities of Windows Powershell to collate the data just gathered.

Below we are getting Total Physical Memory data from all the machines and putting it in a CSV file through a Windows Powershell script snippet.

$ShareinfoName = @()

$filename = 'ShareInfo.csv'

foreach ($share in $c.ChildJobs) {

            if($share.Output.Count)

        {

          $ShareinfoName += $share.Output[0] | % {

            $_| select @{e={$share.Output[0].TotalPhysicalMemory};n='Name'},

            @{e={$share.Output[0].Name};n='TotalPhysicalMemory'}

          }

      }

}

      

$ShareInfoName | select Name,TotalPhysicalMemory | export-csv -noType $filename

Kapil Mathur [MSFT]

Comments

  • Anonymous
    January 21, 2010
    I believe Wait-PsJob has been deprecated, no longer works - IIRC Wait-Job replaced it, more details here (syntax changed as well): http://technet.microsoft.com/en-us/library/dd347634.aspx You can confirm this by running gmc -noun job in PS - you should see something like this: PS C:> gcm -noun job Get-Job                                             Get-Job [[-Id] <Int32[]>] [-Verbose] [-Debug] [-... Receive-Job                                         Receive-Job [-Job] <Job[]> [[-Location] <String[... Remove-Job                                          Remove-Job [-Id] <Int32[]> [-Force] [-Verbose] [... Start-Job                                           Start-Job [-ScriptBlock] <ScriptBlock> [[-Initia... Stop-Job                                            Stop-Job [-Id] <Int32[]> [-PassThru] [-Verbose] ... Wait-Job                                            Wait-Job [-Id] <Int32[]> [-Any] [-Timeout <Int32...

  • Anonymous
    January 21, 2010
    Thanks! You are right, I corrected the blog post.