mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2024-11-14 14:45:52 -06:00
46 lines
1.1 KiB
PowerShell
46 lines
1.1 KiB
PowerShell
function Invoke-WPFRunspace {
|
|
|
|
<#
|
|
|
|
.DESCRIPTION
|
|
Simple function to make it easier to invoke a runspace from inside the script.
|
|
|
|
.EXAMPLE
|
|
|
|
$params = @{
|
|
ScriptBlock = $sync.ScriptsInstallPrograms
|
|
ArgumentList = "Installadvancedip,Installbitwarden"
|
|
Verbose = $true
|
|
}
|
|
|
|
Invoke-WPFRunspace @params
|
|
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
Param (
|
|
$ScriptBlock,
|
|
$ArgumentList
|
|
)
|
|
|
|
#Crate a PowerShell instance.
|
|
$script:powershell = [powershell]::Create()
|
|
|
|
#Add Scriptblock and Arguments to runspace
|
|
$script:powershell.AddScript($ScriptBlock)
|
|
$script:powershell.AddArgument($ArgumentList)
|
|
$script:powershell.RunspacePool = $sync.runspace
|
|
|
|
#Run our RunspacePool.
|
|
$script:handle = $script:powershell.BeginInvoke()
|
|
|
|
#Cleanup our RunspacePool threads when they are complete ie. GC.
|
|
if ($script:handle.IsCompleted)
|
|
{
|
|
$script:powershell.EndInvoke($script:handle)
|
|
$script:powershell.Dispose()
|
|
$sync.runspace.Dispose()
|
|
$sync.runspace.Close()
|
|
[System.GC]::Collect()
|
|
}
|
|
} |