Low*_*113 4 powershell 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
}
}
Run Code Online (Sandbox Code Playgroud)
效果非常好。直到我使用批处理文件启动 PS1。
这是我用来运行批处理文件的代码:
Powershell.exe -executionpolicy remotesigned -windowstyle hidden -File "C:\Updater 2.0.ps1"
Run Code Online (Sandbox Code Playgroud)
批处理文件有效,但弹出窗口不会出现。
所以我改变了方向并尝试使用这样的弹出窗口:
$msgBoxInput = [System.Windows.MessageBox]::Show("You are about to publish Part $UserPart1, would you like to coninue?",'Confirm Update','YesNo')
Run Code Online (Sandbox Code Playgroud)
再次,消息框没有弹出。如果我删除消息开头的“$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"
}
}
Run Code Online (Sandbox Code Playgroud)
输出(省略-windowstyle hidden在当前窗口中查看返回值cmd):
Powershell.exe -executionpolicy remotesigned -File D:\PShell\SO\72366658.ps1
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Runs Code