是否有方法在PowerShell中显示与Windows系统和MacOS兼容的弹出消息框?
我知道您可以在PowerShell版本5(如下所示)上执行类似的操作,但是有没有办法重现一个在Windows和Mac OS平台上都能工作的弹出消息框是否有方法在PowerShell中显示与Windows系统和MacOS兼容的弹出消息框?,windows,macos,powershell,cross-platform,Windows,Macos,Powershell,Cross Platform,我知道您可以在PowerShell版本5(如下所示)上执行类似的操作,但是有没有办法重现一个在Windows和Mac OS平台上都能工作的弹出消息框 $ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information $MessageBody = "Message Body goes here" $Messag
$ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK
$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information
$MessageBody = "Message Body goes here"
$MessageTitle = "Title"
$Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
以上代码的输出
下面定义的
显示消息框
功能在Windows和macOS上提供了消息框功能,以WinForms消息框
类为模型
示例:
# Message box with custom message, OK button only, and default title and icon
$null = Show-MessageBox 'some message'
# Message box with custom message and title, buttons OK and cancel, and
# the Stop icon (critical error)
# Return value is the name of the button chosen.
$buttonChosen = Show-MessageBox 'some message' 'a title' -Buttons OKCancel -Icon Stop
语法(为便于阅读,跨多行排列):
此时,唯一真正跨平台的消息显示是使用控制台。在web浏览器中显示?请允许我向新来者提供标准建议:如果答案解决了您的问题,请单击大复选标记接受它(✓) 在它旁边,也可以选择向上投票(向上投票需要至少15个信誉点)。如果您发现其他答案有帮助,请向上投票。接受(为此您将获得2个信誉点)和向上投票有助于未来的读者。有关更多信息,请参阅。如果您的问题尚未完全回答,请提供反馈或。PowerShell core中是否内置了$IsLinux和$IsMacOS自动变量?@rshelat:是的,(以及
$IsWindows
)-请注意,我刚刚进行了一次更新,以确保该代码也在Windows PowerShell中工作(Set-StrictMode-Version 1
->Set-StrictMode-Off
),其中未定义这些变量。
Show-MessageBox [-Message] <string> [[-Title] <string>]
[[-Buttons] {OK | OKCancel | AbortRetryIgnore | YesNoCancel | YesNo | RetryCancel}]
[-Icon {Information | Warning | Stop}]
[-DefaultButtonIndex {0 | 1 | 2}]
function Show-MessageBox {
[CmdletBinding(PositionalBinding=$false)]
param(
[Parameter(Mandatory, Position=0)]
[string] $Message,
[Parameter(Position=1)]
[string] $Title,
[Parameter(Position=2)]
[ValidateSet('OK', 'OKCancel', 'AbortRetryIgnore', 'YesNoCancel', 'YesNo', 'RetryCancel')]
[string] $Buttons = 'OK',
[ValidateSet('Information', 'Warning', 'Stop')]
[string] $Icon = 'Information',
[ValidateSet(0, 1, 2)]
[int] $DefaultButtonIndex
)
# So that the $IsLinux and $IsMacOS PS Core-only
# variables can safely be accessed in WinPS.
Set-StrictMode -Off
$buttonMap = @{
'OK' = @{ buttonList = 'OK'; defaultButtonIndex = 0 }
'OKCancel' = @{ buttonList = 'OK', 'Cancel'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
'AbortRetryIgnore' = @{ buttonList = 'Abort', 'Retry', 'Ignore'; defaultButtonIndex = 2; ; cancelButtonIndex = 0 };
'YesNoCancel' = @{ buttonList = 'Yes', 'No', 'Cancel'; defaultButtonIndex = 2; cancelButtonIndex = 2 };
'YesNo' = @{ buttonList = 'Yes', 'No'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
'RetryCancel' = @{ buttonList = 'Retry', 'Cancel'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
}
$numButtons = $buttonMap[$Buttons].buttonList.Count
$defaultIndex = [math]::Min($numButtons - 1, ($buttonMap[$Buttons].defaultButtonIndex, $DefaultButtonIndex)[$PSBoundParameters.ContainsKey('DefaultButtonIndex')])
$cancelIndex = $buttonMap[$Buttons].cancelButtonIndex
if ($IsLinux) {
Throw "Not supported on Linux."
}
elseif ($IsMacOS) {
$iconClause = if ($Icon -ne 'Information') { 'as ' + $Icon -replace 'Stop', 'critical' }
$buttonClause = "buttons { $($buttonMap[$Buttons].buttonList -replace '^', '"' -replace '$', '"' -join ',') }"
$defaultButtonClause = 'default button ' + (1 + $defaultIndex)
if ($null -ne $cancelIndex -and $cancelIndex -ne $defaultIndex) {
$cancelButtonClause = 'cancel button ' + (1 + $cancelIndex)
}
$appleScript = "display alert `"$Title`" message `"$Message`" $iconClause $buttonClause $defaultButtonClause $cancelButtonClause" #"
Write-Verbose "AppleScript command: $appleScript"
# Show the dialog.
# Note that if a cancel button is assigned, pressing Esc results in an
# error message indicating that the user canceled.
$result = $appleScript | osascript 2>$null
# Output the name of the button chosen (string):
# The name of the cancel button, if the dialog was canceled with ESC, or the
# name of the clicked button, which is reported as "button:<name>"
if (-not $result) { $buttonMap[$Buttons].buttonList[$buttonMap[$Buttons].cancelButtonIndex] } else { $result -replace '.+:' }
}
else { # Windows
Add-Type -Assembly System.Windows.Forms
# Show the dialog.
# Output the chosen button as a stringified [System.Windows.Forms.DialogResult] enum value,
# for consistency with the macOS behavior.
[System.Windows.Forms.MessageBox]::Show($Message, $Title, $Buttons, $Icon, $defaultIndex * 256).ToString()
}
}