mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2024-12-24 23:11:31 -06:00
61 lines
1.8 KiB
PowerShell
61 lines
1.8 KiB
PowerShell
function Invoke-WPFRunspace {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
Creates and invokes a runspace using the given scriptblock and argumentlist
|
|
|
|
.PARAMETER ScriptBlock
|
|
The scriptblock to invoke in the runspace
|
|
|
|
.PARAMETER ArgumentList
|
|
A list of arguments to pass to the runspace
|
|
|
|
.PARAMETER ParameterList
|
|
A list of named parameters that should be provided.
|
|
.EXAMPLE
|
|
Invoke-WPFRunspace `
|
|
-ScriptBlock $sync.ScriptsInstallPrograms `
|
|
-ArgumentList "Installadvancedip,Installbitwarden" `
|
|
|
|
Invoke-WPFRunspace`
|
|
-ScriptBlock $sync.ScriptsInstallPrograms `
|
|
-ParameterList @(("PackagesToInstall", @("Installadvancedip,Installbitwarden")),("ChocoPreference", $true))
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
Param (
|
|
$ScriptBlock,
|
|
$ArgumentList,
|
|
$ParameterList,
|
|
$DebugPreference
|
|
)
|
|
|
|
# Create a PowerShell instance
|
|
$script:powershell = [powershell]::Create()
|
|
|
|
# Add Scriptblock and Arguments to runspace
|
|
$script:powershell.AddScript($ScriptBlock)
|
|
$script:powershell.AddArgument($ArgumentList)
|
|
|
|
foreach ($parameter in $ParameterList) {
|
|
$script:powershell.AddParameter($parameter[0], $parameter[1])
|
|
}
|
|
$script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block
|
|
$script:powershell.RunspacePool = $sync.runspace
|
|
|
|
# Execute the RunspacePool
|
|
$script:handle = $script:powershell.BeginInvoke()
|
|
|
|
# Clean up the RunspacePool threads when they are complete, and invoke the garbage collector to clean up the memory
|
|
if ($script:handle.IsCompleted) {
|
|
$script:powershell.EndInvoke($script:handle)
|
|
$script:powershell.Dispose()
|
|
$sync.runspace.Dispose()
|
|
$sync.runspace.Close()
|
|
[System.GC]::Collect()
|
|
}
|
|
# Return the handle
|
|
return $handle
|
|
}
|