开发者们早上好,
我正在尝试在powershell中创建删除功能。我想要这样的东西:
function deleteEnv(){
$result = [System.Windows.Forms.MessageBox]::Show('Are you sure?''Yes', 'No' 'Info')
if(yes){
//Delete
}
else {
//do nothing
}
}
当我单击删除按钮时,必须首先显示一个消息框,其中包含两个按钮“是”或“否”。如果是,则删除,否则不执行任何操作。我该怎么做?
亲切的问候
[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
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
}
更简洁,您可以预先将引用传递给变量(在任何 Windows 上预安装的
Powershell ISE
中,您可以在 ::
之后使用 CTRL+SPACE 来完成代码以列出选项)
$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
}
关于命名函数,您应该阅读
Get-Help about_Functions
:
函数名称
您可以为函数指定任何名称,但您共享的函数除外 其他人应该遵循为所有人建立的命名规则 PowerShell 命令。
函数名称应由动词-名词对组成,其中动词 标识函数执行的操作,名词标识 cmdlet 对其执行操作的项目。
这是适合您情况的示例函数:
function Remove-Something ($object) {
if (-not $object) {
# Nothing to delete
Write-Warning "No object given as parameter. Nothing to delete."
return
}
Add-Type -AssemblyName System.Windows.Forms
$text = "Do you want to delete?"
$caption = "Delete..."
$buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo
$icon = [System.Windows.Forms.MessageBoxIcon]::Question
$defaultButton = [System.Windows.Forms.MessageBoxDefaultButton]::Button2 # default: no - avoid accidential deletion
#Show the MessageBox:
$YouAreSure = @{"Yes"=$true;"No"=$false}."$([System.Windows.Forms.MessageBox]::Show($text,$caption,$buttons,$icon,$defaultButton))"
if ($YouAreSure) {
Write-Output "It should now delete $object"
# insert delete commands here
}
}
使用
Remove-Something "that needs to be removed"
启动您的函数
如果您需要不同的 MessageBox,这可能会有所帮助
$icon = [System.Windows.Forms.MessageBoxIcon]::None
$icon = [System.Windows.Forms.MessageBoxIcon]::Information
$icon = [System.Windows.Forms.MessageBoxIcon]::Question
$icon = [System.Windows.Forms.MessageBoxIcon]::Warning
$icon = [System.Windows.Forms.MessageBoxIcon]::Error
$buttons = [System.Windows.Forms.MessageBoxButtons]::OK
$buttons = [System.Windows.Forms.MessageBoxButtons]::OKCancel
$buttons = [System.Windows.Forms.MessageBoxButtons]::RetryCancel
$buttons = [System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore
$buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo
$buttons = [System.Windows.Forms.MessageBoxButtons]::YesNoCancel
$defaultButton = [System.Windows.Forms.MessageBoxDefaultButton]::Button1
$defaultButton = [System.Windows.Forms.MessageBoxDefaultButton]::Button2
$defaultButton = [System.Windows.Forms.MessageBoxDefaultButton]::Button3
有关 MessageBox 的更多详细信息可以在此处找到 https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox