powershellpowershell-jobs

Powershell - how to get Location of job with job result in one table?


I want to get serial numbers of remote PC's with the name of PC, in one table. $PCNameArray is array of remote PC I want to get serial numbers.

$complete = $false
$arrayJobs = @()

Foreach($a in $PCNameArray) {
    $arrayJobs += Get-WmiObject -ComputerName $a -Class Win32_BIOS -asjob
}

while (-not $complete) {
    $arrayJobsInProgress = $arrayJobs |
        Where-Object { $_.State -match ‘running’ }
    if (-not $arrayJobsInProgress) { “All Jobs Have Completed” ; $complete = $true }
}

$arrayJobs | Receive-Job

this code will give me only serial numbers. How can I add each PC names to this result? Thank you.


Solution

  • The computer name is returned as a property of the returned WMI object, it's just not shown by default. You can show it like this:

    $arrayJobs = Get-WmiObject -ComputerName $PCNameArray -Class Win32_BIOS -asjob
    
    Wait-Job $arrayJobs
    
    $arrayJobs |
        Receive-Job |
        Format-Table PsComputerName, SerialNumber -AutoSize
    

    This gives output like this:

    PSComputerName SerialNumber                    
    -------------- ------------                    
    Server1        0000-0000-2620-2195-4000-8654-21
    Server2        0000-0013-3219-1963-9428-8113-87
    Server3        0000-0004-9473-4695-9509-1141-89
    

    PsComputerName is added to the WMI object by PowerShell, but the object itself has a __Server property that also contains the computer name. You can see everything that is returned by replacing Format-Table PsComputerName, SerialNumber -AutoSize in the above example with Format-List *