So I am running invoke-command against multiple computers using an array. I am trying to find workstations with a specific file on them. This works great. I can get a list of 5 workstations with this file, and I have the data I need. However, it also outputs 95 instances of results being produced in red lines telling me file not found. How do I manage error handling to ignore this kind of stuff? I just want the positive output, not errors.
It helps if you post your code you’re working with and make sure to format it with the ‘</>’ button
<$computers = Get-ADComputer -filter * -SearchBase "ou=some,ou=ou workstations,dc=domain,dc=coml" | select-object -Property dnshostname -ExpandProperty dnshostname
Invoke-Command -ComputerName $computers -Credentials domain\admin -ScriptBlock {Get-WmiObject -class win32_product | Where-Object {$_.name -like "*name*"}} -erroraction silentlycontinue>
This works great… I get the data I need but then get tons of output like this for the other computers that don’t have that info:
<[COMPUTER.DOMAIN.] Connecting to remote server COMPUTER.DOMAIN failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is
accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the same local
subnet. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: ([COMPUTER.DOMAIN:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionStateBroken>
Pretty much, I want to ignore all of that in my output so I can get a cleaner presentation of the data I need without the data I DON’T need.
ErrorAction SilentlyContinue only handles non-terminating errors.
You can add to ignore the errors
try {
Invoke-Command....
} catch {}
hello,
why not also put a try-catch block in the script block. This prevent remote computers to return error to calling computer.
I use this pattern
Invoke-Command -ComputerName $computerlist -ScriptBlock $script -ErrorAction SilentlyContinue -ErrorVariable +errors
This will prevent errors generated by Invoke-Command while collecting the errors in the $errors varialbe so you can look at them afterwards if you like. The + sign on the variable name makes the errors append to the variable, otherwise it will overwrite.
This is nice, I like that you can store them later for review. Thank you!
This did it, thank you!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.