Skip to content

Instantly share code, notes, and snippets.

@milesgratz
Created June 12, 2017 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milesgratz/1b46860263e9663569d4383635730cd9 to your computer and use it in GitHub Desktop.
Save milesgratz/1b46860263e9663569d4383635730cd9 to your computer and use it in GitHub Desktop.
Custom function to start Invoke-Command "jobs" on remote systems ... but with double hop consideration
$FirstHop = "Server1"
$SecondHop = "Server2"
$Cred = Get-Credential
Function Run-CustomFunction {
param(
$FirstHop,
$SecondHop,
$Cred
)
# Create a job on FirstHop server for tracking results
Start-Job -Name $FirstHop -ScriptBlock {
# Define argumentlist
$FirstHop = $args[0]
$SecondHop = $args[1]
$Cred = $args[2]
# Run the command on the FirstHop Remote Computer
Invoke-Command -ComputerName $FirstHop -ScriptBlock {
# Define argumentlist
$SecondHop = $args[0]
$Cred = $args[1]
# Run the command from FirstHop to SecondHop
Invoke-Command -ComputerName $SecondHop -Credential $Cred -ScriptBlock {
Get-ChildItem "C:\Temp"
}
} -ArgumentList $SecondHop,$Cred
} -ArgumentList $FirstHop,$SecondHop,$Cred
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment