Created
October 30, 2014 00:34
-
-
Save lantrix/e969dabb9551aabc1b0c to your computer and use it in GitHub Desktop.
Invoke parametised jobs on a Remote Host with Powershell
This file contains hidden or 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
| $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