我用WPF和Runspace写了一个小应用程序,遇到了一些困难。
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$code = {
#Build the GUI
[xml]$xaml = @"
$xaml_code
"@
$syncHash = [hashtable]::Synchronized(@{ })
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window = [Windows.Markup.XamlReader]::Load($reader)
function RunspaceScript {
param($syncHash, $Servers, $TargetBox)
$syncHash.Host = $host
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
$Runspace.SessionStateProxy.SetVariable("Servers", $Servers)
$Runspace.SessionStateProxy.SetVariable("TargetBox", $TargetBox)
$code = {
Function Add-OutputBoxLine {
Param ([string]$Message,[string]$Color = "Black")
$syncHash.Window.Dispatcher.invoke(
[action]{
$RichTextRange = New-Object System.Windows.Documents.TextRange($syncHash.$TargetBox.Document.ContentEnd, $syncHash.$TargetBox.Document.ContentEnd)
$RichTextRange.Text = "$Message`r`n"
$RichTextRange.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, "$Color")
$syncHash.$TargetBox.ScrollToCaret()
}
)
}
$syncHash.Window.Dispatcher.invoke(
[action]{ $syncHash.$TargetBox.Clear(); $syncHash.Start_Switchover.IsEnabled = $False })
if (!$az_connection) {
$az_connection = Connect-AzAccount
}
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$PSinstance.Runspace.Name = "SwitchOver"
$job = $PSinstance.BeginInvoke()
}
# Click Actions
$syncHash.Start_Switchover.Add_Click(
{
RunspaceScript -syncHash $syncHash -Servers $syncHash.Servers.Text -TargetBox "Process_Output"
})
$syncHash.Window.ShowDialog()
$Runspace.Close()
$Runspace.Dispose()
}
$PSinstance1 = [powershell]::Create().AddScript($Code)
$PSinstance1.Runspace = $Runspace
$job = $PSinstance1.BeginInvoke()我想要访问一个运行空间内的变量$az_connection,该运行空间位于一个函数"RunspaceScript“内,而该函数是在上次执行该函数时使用的。有什么想法可以实现吗?
附言:由于规则的原因,我被迫从这里的脚本代码中删除了一些行,不要试图复制并运行代码,它将不起作用。
发布于 2021-07-28 01:49:42
可能最简单的方法是将其添加到$syncHash - $syncHash.AzConnection = $az_connection中。否则,您需要将其作为脚本块的输出返回,然后在AsyncResult完成后提取输出。
function RunspaceScript {
param($syncHash, $Servers, $TargetBox)
$syncHash.Host = $host
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = 'STA'
$Runspace.ThreadOptions = 'ReuseThread'
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable('syncHash', $syncHash)
$Runspace.SessionStateProxy.SetVariable('Servers', $Servers)
$Runspace.SessionStateProxy.SetVariable('TargetBox', $TargetBox)
$code = {
Function Add-OutputBoxLine {
Param ([string]$Message, [string]$Color = 'Black')
$syncHash.Window.Dispatcher.invoke(
[action] {
$RichTextRange = New-Object System.Windows.Documents.TextRange($syncHash.$TargetBox.Document.ContentEnd, $syncHash.$TargetBox.Document.ContentEnd)
$RichTextRange.Text = "$Message`r`n"
$RichTextRange.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, "$Color")
$syncHash.$TargetBox.ScrollToCaret()
}
)
}
$syncHash.Window.Dispatcher.invoke(
[action] { $syncHash.$TargetBox.Clear(); $syncHash.Start_Switchover.IsEnabled = $False })
if (!$az_connection) {
$az_connection = Connect-AzAccount
}
## Output the connection from your scriptblock ##
$az_connection
## OR just add it to your synchronized hashtable ##
$syncHash.AzConnection = $az_connection
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$PSinstance.Runspace.Name = 'SwitchOver'
$job = $PSinstance.BeginInvoke()
## Wait for the $code scriptblock to finish ##
While (-not $job.IsCompleted) {
Start-Sleep -Seconds 1
}
## Pass the job to EndInvoke() to receive the output ##
$az_connection = $PSinstance.EndInvoke($job)
## Or access it from your synchronized hashtable ##
$syncHash.AzConnection
}https://stackoverflow.com/questions/68551202
复制相似问题