2

How does error handling work when I use Invoke-Command over many computers? For example, what if a computer name is misspelled or the user doesn't exist on a specific computer?

Invoke-Command -ComputerName Computer1,Computer2,Computer3  -ScriptBlock {        
    Get-LocalUser -Name User1 -ErrorAction SilentlyContinue 
}
stackprotector
  • 10,498
  • 4
  • 35
  • 64
Rod
  • 14,529
  • 31
  • 118
  • 230

1 Answers1

1

You can use the -ErrorVariable common parameter for this:

Invoke-Command -ComputerName Computer1, Computer2, doesNotexist -ScriptBlock {        
    Get-LocalUser -Name User1 -ErrorAction SilentlyContinue
} -ErrorAction SilentlyContinue -ErrorVariable errors

# error details here
$errors | Select-Object TargetObject, ErrorDetails
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37