Skip to content

Instantly share code, notes, and snippets.

@lantrix
Created October 30, 2014 00:34
Show Gist options
  • Select an option

  • Save lantrix/e969dabb9551aabc1b0c to your computer and use it in GitHub Desktop.

Select an option

Save lantrix/e969dabb9551aabc1b0c to your computer and use it in GitHub Desktop.
Invoke parametised jobs on a Remote Host with Powershell
$remoteCode = {
#Passing in two parameters
param(
[Parameter(Position=0, Mandatory=$true)][ValidateNotNullOrEmpty()][Int]$jobLoop,
[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()][Int]$jobCount
)
$job = {
param(
[Parameter(Position=0, Mandatory=$true)][ValidateNotNullOrEmpty()][String]$jobParam
)
1..$jobLoop | % { Start-Sleep -Milliseconds 10 }
Write-Host $jobParam
}
1..$jobCount | % {
$jobArgList = @()
#Must be in this sequence - Two Arguments to pass in are in order
$jobArgList += "My Param Value"
Start-Job -ScriptBlock $job -ArgumentList $jobArgList
}
$keepreading = 1
While ($keepreading) {
$keepreading = 0
Get-Job | % {
if ($_.State -eq "Running") {
$keepreading++
}
elseif (@("Completed", "Failed") -Contains $_.State) {
Write-Host
Receive-Job $_.Id
Remove-Job $_.Id -Force
}
}
Write-Host -NoNewLine "$keepreading left... "
Start-Sleep 2
}
}
$creds = Get-Credential
$session = New-PSSession -ComputerName myServer -Credential $creds -ErrorAction SilentlyContinue
$ArgList = @()
#Must be in this sequence - Two Arguments to pass in are in order
$ArgList += 15
$ArgList += 5
$RemoteResult = Invoke-Command -Session $session -ScriptBlock $remoteCode -ArgumentList $ArgList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment