Add argumentlist and rename CommandinRunspace

This commit is contained in:
Floris van Enter
2019-12-24 16:39:39 +01:00
parent 512a874388
commit 128850d1d2

View File

@ -1,4 +1,4 @@
function Invoke-CommandInRunspace { function Invoke-CommandAsync {
<# <#
.SYNOPSIS .SYNOPSIS
Invoke a command on multiple computers asynchronously Invoke a command on multiple computers asynchronously
@ -14,9 +14,14 @@
.PARAMETER Throttle .PARAMETER Throttle
How many threads can run simultaniously How many threads can run simultaniously
.EXAMPLE .EXAMPLE
Invoke-CommandInRunspace -Computername 'DC01','DC02','FILE01' -ScriptFile '.\testCopy.ps1' Invoke-CommandAsync -Computername 'DC01','DC02','FILE01' -ScriptFile '.\testCopy.ps1'
Example with a file running on three servers simultanously
.EXAMPLE .EXAMPLE
Invoke-CommandInRunspace -Computername 'FILE01','SQL01','MAIL01' -ScriptBlock { Get-Process } Invoke-CommandAsync -Computername 'FILE01','SQL01' -argumentlist 'notepad' -ScriptBlock { param($proc) Get-Process -Name $proc }
Example with a scriptblock with parameters running on two servers simultanously
.EXAMPLE
Invoke-CommandAsync -Computername (Get-Content .\computers.txt) -ScriptBlock { Get-Process }
Example with running a scriptblock on several machines in text file
.LINK .LINK
https://www.entermi.nl https://www.entermi.nl
.NOTES .NOTES
@ -38,6 +43,8 @@
[Parameter(Mandatory=$False, [Parameter(Mandatory=$False,
ValueFromPipeline=$False)] ValueFromPipeline=$False)]
[sring]$ScriptFile = $null, [sring]$ScriptFile = $null,
[Parameter(Mandatory=$False, ValueFromPipeline=$False)]
$ArgumentList = $null,
[Parameter(Mandatory=$False, [Parameter(Mandatory=$False,
ValueFromPipeline=$True)] ValueFromPipeline=$True)]
[int]$Throttle = 20 [int]$Throttle = 20
@ -45,7 +52,7 @@
$Stopwatch = New-Object System.Diagnostics.Stopwatch $Stopwatch = New-Object System.Diagnostics.Stopwatch
$Stopwatch.start() $Stopwatch.start()
Write-Verbose -Message "Begin Invoke-CommandInRunspace" Write-Verbose -Message "Begin Invoke-CommandAsync"
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,$Throttle) $RunspacePool = [runspacefactory]::CreateRunspacePool(1,$Throttle)
$RunspacePool.ApartmentState = "MTA" $RunspacePool.ApartmentState = "MTA"
@ -55,15 +62,16 @@
Param( Param(
[string] $Computer, [string] $Computer,
[scriptblock] $ScriptBlock = $null, [scriptblock] $ScriptBlock = $null,
[string] $ScriptFile = $null [string] $ScriptFile = $null,
$ArgumentList
) )
if($ScriptBlock) { if($ScriptBlock) {
Write-Verbose -Message "Run Invoke-Command on $Computer with scriptblock" Write-Verbose -Message "Run Invoke-Command on $Computer with scriptblock"
return (Invoke-Command -ComputerName $Computer -ScriptBlock $ScriptBlock) return (Invoke-Command -ComputerName $Computer -ScriptBlock $ScriptBlock -ArgumentList (,$ArgumentList))
} elseif($ScriptFile) { } elseif($ScriptFile) {
Write-Verbose -Message "Run Invoke-Command on $Computer with scriptfile; $ScriptFile" Write-Verbose -Message "Run Invoke-Command on $Computer with scriptfile; $ScriptFile"
return (Invoke-Command -ComputerName $Computer -FilePath $ScriptFile) return (Invoke-Command -ComputerName $Computer -FilePath $ScriptFile -ArgumentList (,$ArgumentList))
} else { } else {
return $null return $null
} }
@ -75,7 +83,8 @@
$ParamContainer = @{ $ParamContainer = @{
Computer = $Computer Computer = $Computer
ScriptBlock = $ScriptBlock ScriptBlock = $ScriptBlock
ScriptFile = $ScriptFile ScriptFile = $ScriptFile
ArgumentList = $ArgumentList
} }
$RunspaceObject = [PSCustomObject]@{ $RunspaceObject = [PSCustomObject]@{
@ -88,13 +97,18 @@
$RunspaceObject.Invoker = $RunspaceObject.Runspace.BeginInvoke() $RunspaceObject.Invoker = $RunspaceObject.Runspace.BeginInvoke()
$Threads += $RunspaceObject $Threads += $RunspaceObject
Write-Verbose -Message "Finished creating runspace for $Computer. Elapsed time: $($Stopwatch.Elapsed)" Write-Verbose -Message "Created runspace for $Computer. Elapsed time: $($Stopwatch.Elapsed)"
} }
Write-Verbose -Message "Finished creating runspaces for all computers. Elapsed time: $($Stopwatch.Elapsed)" Write-Verbose -Message "Created runspaces for all computers. Elapsed time: $($Stopwatch.Elapsed)"
While ($Threads.Invoker.IsCompleted -contains $false) { While ($Threads.Invoker.IsCompleted -contains $false) {
Write-Host "Waiting for all threads to complete" if($stopwatch.Elapsed.Minutes -eq $minutes) {
Start-Sleep -Seconds 3 Write-Host "Waiting for all threads to complete"
} else {
Write-Host "Waiting for all threads to complete. Elapsed time: $($Stopwatch.Elapsed)"
$minutes = $stopwatch.Elapsed.Minutes
}
Start-Sleep -Seconds 3
} }
Write-Host "All threads are completed, time elapsed: $($Stopwatch.Elapsed)" Write-Host "All threads are completed, time elapsed: $($Stopwatch.Elapsed)"
@ -106,5 +120,5 @@
$RunspacePool.Close() $RunspacePool.Close()
$RunspacePool.Dispose() $RunspacePool.Dispose()
Write-Verbose -Message "End Invoke-CommandInRunspace, time elapsed: $($Stopwatch.Elapsed)" Write-Verbose -Message "End Invoke-CommandAsync, time elapsed: $($Stopwatch.Elapsed)"
} }