簡體   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