[英]Multithreaded Powershell GUI Application never displays when called from CMD.exe
我终于完成了供我公司内部使用的powershell wpf应用程序,当从powershell窗口调用该程序时,它按预期运行,但是当我尝试从CMD或从Visual Studio作为外部工具进行调用时,GUI窗口从没有出现。
我的应用程序与此编写非常接近,对显示和在后台运行的功能进行了一些更改。
谁能解释为什么我的ps1文件从CMD调用时不显示gui,但是从powershell命令窗口调用时却显示gui? 这是脚本的要点:
$Global:syncHash = [hashtable]::Synchronized(@{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$psCmd = [PowerShell]::Create().AddScript({
[xml]$xaml = @"
<valid wpf>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[xml]$XAML = $xaml
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | %{
#Find all of the form types and add them as members to the synchash
$syncHash.Add($_.Name,$syncHash.Window.FindName($_.Name) )
}
## Custom functions for items in syncHash here ##
$syncHash.Window.ShowDialog() | Out-Null
$syncHash.Error = $Error
})
$psCmd.Runspace = $newRunspace
$date = $psCmd.BeginInvoke()
我在cmd.exe中将此脚本称为“ C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ powershell.exe C:\\ pathToFile \\ file.ps1”
我的执行策略已更新,因此我也可以运行脚本。
感谢您的所有帮助,我明白了。 由于某些原因,我需要使用NoExit选项运行命令
powershell.exe -ExecutionPolicy Bypass -NoExit -File "pathToFile\File.ps1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.