Created
June 12, 2017 16:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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