powershell 调用 OpenFileDialog类 假死问题解决。

简介:

我创建了一个打开文件的函数。代码内容:

1
2
3
4
5
6
7
8
9
function  Openfile {  
Add-Type  -AssemblyName System.Windows.Forms  
$openfile  New-Object  System.Windows.Forms.OpenFileDialog  
$openfile . Filter  '图片|*.jpg|All Files|*.*'  
$openfile .InitialDirectory =  "C:\ManageOperate\Staff Photo"  
[void] $openfile .ShowDialog()  
$Openfile .title= "请选择一张照片"  
$Openfile .FileName  
}
1
当在Win7SP1X64环境中调用的时候没有问题。

image

但当把脚本放到Windows Server2008R2环境中却出现了假死的问题。脚本始终停在这里没有窗口弹出。

image

原来是由于脚本运行在MTA也就是多线程模式导致的。Powershell2.0控制台默认是MTA,Powershell3.0 控制台默认是STA。

什么是MTA和STA:

官方解释:

STA:http://msdn.microsoft.com/en-us/library/ms680112(v=vs.85).aspx

MTA:http://msdn.microsoft.com/en-us/library/ms693421(v=vs.85).aspx

判断Powershell的版本和线程模式 。

Win7SP1 X64

image

WinServer2008R2

image

 

解决方法:在代码ShowDialog之前加入*.ShowHelp = $true判断参数

1
2
3
4
5
6
7
8
9
10
function  Openfile {   
Add-Type  -AssemblyName System.Windows.Forms    
$openfile  New-Object  System.Windows.Forms.OpenFileDialog    
$openfile . Filter  '图片|*.jpg|All Files|*.*'    
$openfile .InitialDirectory =  "C:\ManageOperate\Staff Photo"    
$openfile .ShowHelp= $true       
[void] $openfile .ShowDialog()    
$Openfile .title= "请选择一张照片"    
$Openfile .FileName    
}

 

我们再次在Server上执行这个函数。已经可以弹出窗口了。但窗口有点小呵呵。

image



本文转自handsome7038 51CTO博客,原文链接:http://blog.51cto.com/lixiaosong/1536071

相关文章
PowerShell 5.0和跨平台PowerShell支持class类编程
PowerShell 5.0和跨平台PowerShell支持class类编程 PowerShell 5.0支持class类编程,具体查看:https://technet.
1000 0
|
3月前
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
106 0
|
9月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
71 0
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
149 0
|
应用服务中间件 nginx Windows
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
Windows PowerShell 中启动 Nginx 报错解决方案
|
XML 监控 数据格式
利用powershell进行windows日志分析
0x00 前言   Windows 中提供了 2 个分析事件日志的 PowerShell cmdlet:一个是Get-WinEvent,超级强大,但使用起来比较麻烦;另一个是Get-EventLog,使得起来相当简单,可以实时筛选,接下来,我们利用PowerShell 来自动筛选 Windows 事件日志。
2484 0
|
前端开发 Shell 开发工具
前端开发工具 vscode 使用技巧篇:控制台由powershell切换为cmd方法,windows下新旧版控制台cmd与powershell互切方法
前端开发工具 vscode 使用技巧篇:控制台由powershell切换为cmd方法,windows下新旧版控制台cmd与powershell互切方法
417 0
前端开发工具 vscode 使用技巧篇:控制台由powershell切换为cmd方法,windows下新旧版控制台cmd与powershell互切方法