PowerShell - 提示 'Would you like to continue'
PowerShell - Prompt for 'Would you like to continue'
我有一个用于自动执行 WSUS 进程的脚本,它的最后阶段继续删除所有 old/unnecessary files/objects.
我想在清理阶段之前提示“按 'enter' 继续删除或按任何其他键停止”,让人们可以选择不 运行 它。
我目前在脚本末尾的代码在这里:
Get-WsusServer 10.1.1.25 -PortNumber 8530 | Get-WsusUpdate -Classification All -Approval Unapproved -Status FailedOrNeeded | Approve-WsusUpdate -Action Install -Target $ComputerTarget -Verbose
Write-Host "Updates have been approved!"
Write-Host "Preparing to clean WSUS Server of obsolete computers, updates, and content files."
#Part2 - WSUS Server Cleanup
##Run Cleanup Command
Get-WsusServer $WSUS_Server -PortNumber $PortNumber | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles
就在#Part2 之前,我想要提示 'Press enter to continue or any other key to abort'
我似乎找不到一个简单的方法来做到这一点?我所看到的一切似乎都涉及将整个脚本嵌套在我不想这样做的代码块中。 =/
谢谢!
这并不完美,但它会让您的用户有机会逃脱脚本。它实际上可能更好,因为这意味着您的用户不会意外点击反斜杠按钮并在他们想按回车键时取消脚本。
Write-Host "Press `"Enter`" to continue or `"Ctrl-C`" to cancel"
do
{
$key = [Console]::ReadKey("noecho")
}
while($key.Key -ne "Enter")
Write-Host "Complete"
你可以这样提示用户:
$response = read-host "Press enter to continue or any other key (and then enter) to abort"
如果用户只是按下回车键,那么 $response
将为空。 Powershell 会将空字符串转换为布尔值 false:
$aborted = ! [bool]$response
或者您可以只查询特定字符:
$response = read-host "Press a to abort, any other key to continue."
$aborted = $response -eq "a"
所以我手头上有一个 Show-MsgBox 函数可以扔到脚本中。这样我就可以用一个简单的命令随意显示一个对话框,它可以选择要显示的按钮、要显示的图标、window 标题和框中的文本。
Function Show-MsgBox ($Text,$Title="",[Windows.Forms.MessageBoxButtons]$Button = "OK",[Windows.Forms.MessageBoxIcon]$Icon="Information"){
[Windows.Forms.MessageBox]::Show("$Text", "$Title", [Windows.Forms.MessageBoxButtons]::$Button, $Icon) | ?{(!($_ -eq "OK"))}
}
这就是函数的全部内容,然后在您的情况下,您可以执行以下操作:
If((Show-MsgBox -Title 'Confirm CleanUp' -Text 'Would you like to continue with the cleanup process?' -Button YesNo -Icon Warning) -eq 'No'){Exit}
然后它会弹出是和否按钮,如果他们单击否,它会退出脚本。
我有一个用于自动执行 WSUS 进程的脚本,它的最后阶段继续删除所有 old/unnecessary files/objects.
我想在清理阶段之前提示“按 'enter' 继续删除或按任何其他键停止”,让人们可以选择不 运行 它。
我目前在脚本末尾的代码在这里:
Get-WsusServer 10.1.1.25 -PortNumber 8530 | Get-WsusUpdate -Classification All -Approval Unapproved -Status FailedOrNeeded | Approve-WsusUpdate -Action Install -Target $ComputerTarget -Verbose
Write-Host "Updates have been approved!"
Write-Host "Preparing to clean WSUS Server of obsolete computers, updates, and content files."
#Part2 - WSUS Server Cleanup
##Run Cleanup Command
Get-WsusServer $WSUS_Server -PortNumber $PortNumber | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles
就在#Part2 之前,我想要提示 'Press enter to continue or any other key to abort'
我似乎找不到一个简单的方法来做到这一点?我所看到的一切似乎都涉及将整个脚本嵌套在我不想这样做的代码块中。 =/
谢谢!
这并不完美,但它会让您的用户有机会逃脱脚本。它实际上可能更好,因为这意味着您的用户不会意外点击反斜杠按钮并在他们想按回车键时取消脚本。
Write-Host "Press `"Enter`" to continue or `"Ctrl-C`" to cancel"
do
{
$key = [Console]::ReadKey("noecho")
}
while($key.Key -ne "Enter")
Write-Host "Complete"
你可以这样提示用户:
$response = read-host "Press enter to continue or any other key (and then enter) to abort"
如果用户只是按下回车键,那么 $response
将为空。 Powershell 会将空字符串转换为布尔值 false:
$aborted = ! [bool]$response
或者您可以只查询特定字符:
$response = read-host "Press a to abort, any other key to continue."
$aborted = $response -eq "a"
所以我手头上有一个 Show-MsgBox 函数可以扔到脚本中。这样我就可以用一个简单的命令随意显示一个对话框,它可以选择要显示的按钮、要显示的图标、window 标题和框中的文本。
Function Show-MsgBox ($Text,$Title="",[Windows.Forms.MessageBoxButtons]$Button = "OK",[Windows.Forms.MessageBoxIcon]$Icon="Information"){
[Windows.Forms.MessageBox]::Show("$Text", "$Title", [Windows.Forms.MessageBoxButtons]::$Button, $Icon) | ?{(!($_ -eq "OK"))}
}
这就是函数的全部内容,然后在您的情况下,您可以执行以下操作:
If((Show-MsgBox -Title 'Confirm CleanUp' -Text 'Would you like to continue with the cleanup process?' -Button YesNo -Icon Warning) -eq 'No'){Exit}
然后它会弹出是和否按钮,如果他们单击否,它会退出脚本。