Recipes by topic

Inventory of several computers in parallel

PowerShell 7: ForEach-Object -Parallel runs one block per machine, with a concurrency limit (-ThrottleLimit). Each block opens its own CIM session, reads Win32_ComputerSystem, Win32_OperatingSystem and Win32_BIOS, then closes the session. A failing machine produces an Error row without stopping the others.

$computerName = 'PC01', 'PC02', 'PC03'
$throttleLimit = 8

$inventory = $computerName | ForEach-Object -ThrottleLimit $throttleLimit -Parallel {
    $name = $_
    $cimSession = $null
    $result = [ordered]@{
        ComputerName    = $name
        Status          = 'OK'
        Manufacturer    = $null
        Model           = $null
        SerialNumber    = $null
        OperatingSystem = $null
        OSVersion       = $null
        MemoryGB        = $null
        LastBoot        = $null
        Error           = $null
    }

    try {
        $sessionParameters = @{
            ComputerName        = $name
            OperationTimeoutSec = 20
            ErrorAction         = 'Stop'
        }
        $cimSession = New-CimSession @sessionParameters

        $queryParameters = @{
            CimSession  = $cimSession
            ErrorAction = 'Stop'
        }
        $computerSystem = Get-CimInstance @queryParameters -ClassName Win32_ComputerSystem
        $operatingSystem = Get-CimInstance @queryParameters -ClassName Win32_OperatingSystem
        $bios = Get-CimInstance @queryParameters -ClassName Win32_BIOS

        $result['Manufacturer'] = $computerSystem.Manufacturer
        $result['Model'] = $computerSystem.Model
        $result['SerialNumber'] = $bios.SerialNumber
        $result['OperatingSystem'] = $operatingSystem.Caption
        $result['OSVersion'] = $operatingSystem.Version
        $result['MemoryGB'] = [math]::Round($computerSystem.TotalPhysicalMemory / 1GB, 1)
        $result['LastBoot'] = $operatingSystem.LastBootUpTime
    } catch {
        $result['Status'] = 'Error'
        $result['Error'] = $_.Exception.Message
    } finally {
        if ($cimSession) {
            Remove-CimSession -CimSession $cimSession
        }
    }

    [PSCustomObject]$result
}

$csvParameters = @{
    Path       = 'inventory.csv'
    Encoding   = 'utf8'
    UseCulture = $true
}
$inventory |
    Sort-Object -Property ComputerName |
    Export-Csv @csvParameters

$inventory |
    Where-Object -FilterScript { $_.Status -eq 'Error' } |
    Select-Object -Property ComputerName, Error

Each parallel block runs in its own runspace: it does not see the script variables, hence the session created inside (a session object is not shared between blocks). OperationTimeoutSec bounds each operation, and finally closes the session even after an error. The $result object has the same columns for every machine, which keeps the CSV consistent. -UseCulture uses the list separator of the culture (a semicolon in French), so the file opens directly in Excel.

Variant: explicit credentials

A PSCredential object is passed to the parallel block with $using::

$credential = Get-Credential
$inventory = $computerName | ForEach-Object -ThrottleLimit $throttleLimit -Parallel {
    $sessionParameters = @{
        ComputerName = $_
        Credential   = $using:credential
    }
    $cimSession = New-CimSession @sessionParameters
    try {
        Get-CimInstance -CimSession $cimSession -ClassName Win32_BIOS
    } finally {
        Remove-CimSession -CimSession $cimSession
    }
}

Classes used

← All recipes