XAML 通过powershell中的消息框显示“您确定吗?”

7gyucuyw  于 2022-12-07  发布在  Shell
关注(0)|答案(2)|浏览(182)

早上好,开发人员,
我正在尝试在powershell中创建一个删除函数。我希望有这样的东西:

function deleteEnv(){
    $result = [System.Windows.Forms.MessageBox]::Show('Are you sure?''Yes', 'No' 'Info')
    if(yes){
     //Delete
}

else {
//do nothing
}
}

当我单击删除按钮时,必须首先显示一个消息框,其中有两个按钮“是”或“否”。如果是,则删除,否则不执行任何操作。我该如何操作?
此致

bgtovc5b

bgtovc5b1#

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$result = [System.Windows.Forms.MessageBox]::Show('Are you sure?' , "Info" , 4)
if ($result -eq 'Yes') {
    do stuff
}

其他阅读:http://powershell-tips.blogspot.com.by/2012/02/display-messagebox-with-powershell.html

yzxexxkh

yzxexxkh2#

2022年更新:
加上@4c74356b41的回答:
如果要在从Powershell调用时使用现代Windows样式,则在Windows 8、10、11中,需要启用视觉样式,并且不需要反射:

[System.Windows.Forms.Application]::EnableVisualStyles();

$result = [System.Windows.Forms.MessageBox]::Show("Are you sure?", "Title", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Question)

if ($result -eq "Yes") {
    # do stuff
}

更简洁,您可以事先将引用传递给变量(在预装在任何窗口上的Powershell ISE中,您可以在::后使用CTRL+空格键来完成代码以列出选项)

$mb = [System.Windows.Forms.MessageBox]
$mbIcon = [System.Windows.Forms.MessageBoxIcon]
$mbBtn = [System.Windows.Forms.MessageBoxButtons]

[System.Windows.Forms.Application]::EnableVisualStyles();

$result = $mb::Show("Are you sure?", "Title", $mbBtn::YesNo, $mbIcon::Question)
Echo $result

If ($result -eq "Yes") {
    # do stuff
}
Else {
    # do stuff
}

相关问题