i'm trying to know if my scipt was effectively executed on all the remote hosts when using invoke-command like this :
Invoke-Command -ComputerName "test1","test2" -ScriptBlock {$env:computername}
when it runs interactively it's OK i can see error messages like this :
- CategoryInfo : OpenError: (test1:String) [], PSRemotingTransportException
- FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken
but how to do when running the script in 'batch mode' ?
I tried the try{} catch{} statement but it doesnt seem to work,
then I tried to handle the result in a variable, didnt work either :
PS>$result=Invoke-Command -ComputerName "test1","test2","rodc1" -ScriptBlock {$env:computername}
PS>$result
rodc1
I have ended up to use new-pssession for each host and test it like this
$computers=@("test1","test2","rodc1")
$computers|%{
$s=new-PSSession -ComputerName $_
if($s -eq $null){
$errs +="$_ : cant connect to host `n<br/>"
}
else{
$sess+=$s
}
}
invoke-Command -Session $sess -ScriptBlock {$env:computername} -asJob -jobName "test"
now, i can use $errs to know which computers failed
Is there a better/simplier way to do this ? thank you
Some errors were still not catched here is the part to modify :
$computers=@("test1","test2","rodc1")
$computers|%{
try{
$s=new-PSSession -ComputerName $_
if($s -eq $null){
$rapport+="$_ : cant connect to host`n<br/>"
}
else{
$sess+=$s
}
}
catch{
$rapport+="$_ : cant connect to host`n<br/>"
}
}