wpf - 电源外壳。访问运行空间实例之外的变量
问题描述
我使用 WPF 和 Runspaces 编写了一个小型应用程序,但遇到了一些困难。
$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”中,该函数之前执行该函数。任何想法如何实现?
PS我这里的脚本代码因为规则我被迫删除了一些行,不要试图复制和运行代码,它不会工作。
解决方案
可能最简单的方法是将其添加到 $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
}
推荐阅读
- css - 相对于父元素定位元素
- c# - 将身份模型和 dbcontext 移动到类库后注册异步不起作用
- php - 在 PHP 命名空间或类元素中 \ 用于什么
- sql - SQL Server 中的分类帐类型更新
- node.js - 如果 url 中没有给出路径参数,Express midlleware 仍在调用
- sql - 从数据库中搜索数据
- python - 如何在 keras 中添加自定义图像锐化层?
- java - Jackson obectMapperwriteValue() 没有按预期工作
- javascript - 动态下拉选项数据不存储在数据库中,但仅存储索引号
- ios - iOS:使用 InMobi 进行 Admob 中介:没有可显示的广告