user2978216
user2978216

Reputation: 568

Efficiently Retrieving Microsoft Edge Versions from Multiple Remote Machines Using PowerShell Jobs

$command = {
        try {
            $package = Get-Package -Name "*Microsoft Edge*" 
            return [PSCustomObject]@{Version = $package.Version}
        } catch {
            return "Can't recieve version"
        }
    }

$comps = "SMZ-02122141-LB", "MTR-03103102-LB", "SMZ-02122087-LB", "SMZ-S0102573-DB", "SMZ-PF1BGR9B-LB"
$job = Invoke-Command -AsJob -ComputerName $comps -ScriptBlock $command
$completed = $job | Wait-Job -Timeout 30 | Receive-Job

$results = @()
foreach ($result in $completed) {            
            $results += [PSCustomObject]@{ComputerName = $result.PSComputerName; Version = $result.Version}
        }
$results

I'm attempting to retrieve the version of Microsoft Edge from multiple remote machines using PowerShell. I'm using jobs this time to handle potential issues where some remote machines might hang, which would stop the entire operation if I used simple Invoke-Command. How can I improve this code? Specifically, if a connection to a remote machine fails, how do I gracefully handle this and include it in the results? I'd prefer not to rely on Test-Connection for this purpose.

Upvotes: 0

Views: 207

Answers (1)

js2010
js2010

Reputation: 27546

It runs in parallel anyway and times out eventually, within the throttlelimit (is there a way to change the timeout of the receiver? New-PSSessionOption -OpenTimeout?).

invoke-command $comps { get-package 'microsoft edge' | select version }

Quickly get a list of online computers first (powershell 5.1):

$upComps = test-connection $comps -AsJob -count 1 | receive-job -wait -auto | 
  ? responsetime | % address

Something like this took me 38 sec (get-history -count 1 | fl):

$so = new-pssessionoption -OperationTimeout 100 -OpenTimeout 100 `
  -MaxConnectionRetryCount 0 -IdleTimeout 60000 -CancelTimeout 100
invoke-command $comps -sessionoption $so { get-package 'microsoft edge' | 
  select version }

IdleTimeout has a minimum of 60000 ms.

Upvotes: 1

Related Questions