Salve
Salve

Reputation: 165

Invoke-Command to multiple servers, what is the current server?

Below is my striped down code. How can I determine what the current server is when parsing to Write-Host? With other words, what can I use instead of "$CURRENTSERVER"?

$servers = SERVER1,SERVER2,SERVER3

Try
{
    Invoke-Command -ComputerName $servers -ErrorAction Stop -ScriptBlock{CODE}
}
Catch
{
    Write-Host -ForegroundColor Yellow "[WARNING] Cannot connect to $CURRENTSERVER"
}

The information is there somewhere, if I run the command without try-catch I get

[SERVER3] Connecting to remote server SERVER3 failed with the following error message : WinRM cannot process the request.

Upvotes: 0

Views: 405

Answers (1)

Salve
Salve

Reputation: 165

I found the parameter for the information: $_.TargetObject

Upvotes: 1

Related Questions