PowerShell: How To Bypass Double Hop Problems

# This is a working example of hoping without delegation. Fresh creds can be passed in a nested -ScriptBlock

$hop1="SERVER01"
$hop2="SERVER02"
$domainAdmin="someAdmin"

$cred = Get-Credential "$env:USERDOMAIN`\$domainAdmin"

Invoke-Command -ComputerName $hop1 -Credential $cred -ScriptBlock{
param($hop2)
"Currently at hop: $ENV:computername.";

Invoke-Command -ComputerName $hop2 -Credential $Using:cred -ScriptBlock {
"Reached hop: $ENV:computername.";
}

} -Args $hop2

Leave a Reply

Your email address will not be published. Required fields are marked *