Powershell从cmd运行消息框

时间:2014-09-14 18:22:53

标签: powershell batch-file cmd

我正在努力实现以下目标:

我需要从批处理文件psh脚本运行,但不能从文件,solo de命令行运行。  我已经尝试了ps控制台中的以下代码并且它可以工作,但是当从cmd传递时 - 不是。

powershell.exe -ExecutionPolicy Bypass -Command {[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('We are proceeding with next step.')}

任何人都可以帮忙找出问题吗?格拉西亚斯。

4 个答案:

答案 0 :(得分:3)

切换{bracket}代替“引号”:

@powershell.exe -ExecutionPolicy Bypass -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('We are proceeding with next step.')"

(还添加了@ for echo off)

答案 1 :(得分:1)

很棒的回答约翰。我想用稍微不同的方法对其进行扩展。

如果您在评论部分中查看此问题的“脚本专家”页面的以下链接,可以很好地参考单个命令消息弹出窗口。

根据JohnLawvan的回答和Dan Hayward在下面的页面上的评论 1 的代码,我组装并测试了一个非常快速的命令行来自控制台窗口的回显命令和powershell输出。

@powershell.exe -ExecutionPolicy Bypass -Command "(new-object -ComObject wscript.shell).Popup(Popup message', 0, 'Popup Title')" 1> nul 2>&1

Scripting Guy: PowerTip: Use PowerShell to Display Pop-Up Window

答案 2 :(得分:0)

Iirc您必须在特定的线程单元模式下运行power shell才能使win表单正常运行,因此您需要使用powershell.exe命令行上的相关开关显式指定线程模式。

答案 3 :(得分:0)

使用using语句:

powershell "using assembly system.windows.forms; using namespace system.windows.forms; [messagebox]::show('hi there')"