繁体   English   中英

从批处理文件启动 Powershell 时弹出是/否消息框

[英]Popup Yes/No message Box When Launching Powershell From a Batch File

我正在努力寻找答案,这可能意味着我问错了问题。

我写了一个有弹出窗口的 PS1 脚本,一切都很好! 我使用了这种方法:

$msgBoxInput =  [System.Windows.MessageBox]::Show("You are about to publish Part $UserPart1, would you like to coninue?",'Confirm Update','YesNo')switch  ($msgBoxInput) 
      {
          'Yes' 
          {
          Runs Code
          }
          'No' 
          {
          Return
          }
      }

那工作得很好。 直到我使用批处理文件启动 PS1。

这是我用来运行批处理文件的代码:

Powershell.exe -executionpolicy remotesigned -windowstyle hidden -File  "C:\Updater 2.0.ps1"

批处理文件有效,但弹出窗口不会发生。

所以我换了档,尝试使用这样的弹出窗口:

 $msgBoxInput =  [System.Windows.MessageBox]::Show("You are about to publish Part $UserPart1, would you like to coninue?",'Confirm Update','YesNo')

      

再一次,消息框没有弹出。 如果我删除消息开头的“$msgBoxInput =”,则会弹出该框,但无论用户选择什么,代码都会像按下“是”一样运行。

这可能是完全错误的方法,我真的不知道。 我一直为我的用户组(我有 30 多个用户)使用批处理文件,因为它比尝试使用实际的 PS1 更容易。 如果有更好/更简单的路线,我会全力以赴!

这是我使用 PS1 的第一个表单,所以我也可能做错了。

谢谢大家的帮助。

System.Windows.MessageBox类型不会在 PowerShell 中自动加载(ISE 除外)。 以下代码片段有效:

if ( $null -eq ('System.Windows.MessageBox' -as [type]) ) {
    Add-Type -AssemblyName PresentationFramework
}
$UserPart1 = "XXX"
$msgBoxInput =  [System.Windows.MessageBox]::Show(
    "You are about to publish Part $UserPart1, would you like to coninue?",
    'Confirm Update',
    'YesNo')
switch  ($msgBoxInput) 
      {
          'Yes' 
          {
            # your code here
            Return "Runs Code"
          }
          'No' 
          {
            Return "Runs Nothing"
          }
      }

输出(省略-windowstyle hidden以查看当前cmd窗口中的返回值):

在此处输入图像描述

Powershell.exe -executionpolicy remotesigned -File D:\PShell\SO\72366658.ps1
 Runs Code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM