Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Error handling in remote jobs

$
0
0

Hello, i need to solve error handling when i run some jobs on remote computers via invoke-command.

Some computers might be offline, have PSRemoting disabled, I have access denied to them and so on.   The red text on the whole screen is kinda annoying :)

So, I have very basic scriptblock and i run it against few computers, included fakeones.

create-failed-jobs.ps1

$sourcetxt = "
fakeone1
server1
server2
server3
fakeone2
server4"
$global:source=$sourcetxt.Split() | ?{$_ -ne ""}

## scriptblock
$sblock = { gwmi win32_bios}

$result = @()
[int]$batchsize = [math]::Ceiling($source.Count / 2)

## checking / closing running jobs
if (get-job|? {$_.name -like "Script*"}){
    get-job|? {$_.name -like "Script*"}|% {remove-job $_}
    write-host "Existing jobs removed, re-run the script .."
    exit
    }

$i = 0
$itemCount = $source.count

## Jobs counter
$activeJobCount = 0
$totalJobCount = 0

for ($i=0; $i -lt $itemCount;$i += $batchSize){
    $activeJobCount += 1; $totalJobCount += 1; $HostList = @()
    $HostList += $source |select -skip $i -first $batchsize
    $j = invoke-command -computername $Hostlist -scriptblock $sblock -AsJob -ea stop
    $j.name = "Script`:$totalJobCount`:$($i+1)`:$($HostList.count)"
  }
write-host "Failed jobs created .."
start-sleep 5
get-job |receive-job -Keep

If i just run get-job |receive-job, i get some results together with red text complaining something is wrong. 

So i came up with this:

$cresult = @()
$fresult = @()
Clear-Variable result -ErrorAction SilentlyContinue
foreach ($j in get-job -IncludeChildJob |?{$_.name -notlike "*Script*"})
{
 if ($j.State -eq "Completed")
 {
  $temp = @()
  $temp += receive-job $j
  $cresult += $temp
 }
 elseif ($j.State -eq "Failed")
 {
  $ftemp = @()
  $ftemp   += $j |select @{n="PSComputerName";e={$_.location}},@{n="PS Error";e={($_.jobstateinfo.reason.transportmessage -split "(\.)")[0]}}
  $fresult += $ftemp
 }
}
$cresult
$fresult

Now, red text is gone but i still dont like the output.

SMBIOSBIOSVersion : 090004
Manufacturer      : American Megatrends Inc.
Name              : BIOS Date: 03/19/09 22:51:32  Ver: 09.00.04
SerialNumber      : 5620-8598-5008-0771-6516-8269-37
Version           : VRTUAL - 3000919
PSComputerName    : server1

PSComputerName : fakeone1
PS Error       : The network path was not found

PSComputerName : fakeone2
PS Error       : The network path was not found

PSComputerName : server2
PS Error       : Access is denied

See, i would like to have output like

$result = PSComputerName;SerialNumber  (+whaterver what comes from $cresult);PS Error

So i need to merge $cresult + $fresult to $result 

$result = $cresult + $fresult   doesnt work for obvious reasons..
$cresult = TypeName: Deserialized.System.Management.ManagementObject#root\cimv2\Win32_BIOS
$fresult = TypeName: Selected.System.Management.Automation.PSRemotingChildJob

Or if u came up with better code/idea. ..   

After this i will go for catching up errors inside scriptblock .. but this has to be solved first


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>