PowerShell - 提示“您要继续吗" [英] PowerShell - Prompt for 'Would you like to continue'
问题描述
我有一个脚本用于自动化 WSUS 进程,它的最后一个阶段继续删除所有旧的/不需要的文件/对象.
I have a script I am using to automate WSUS processes and the last stage of it goes on to remove all old/unnecessary files/objects.
我想在清理阶段之前提示按‘回车’继续删除或任何其他键停止",让人们可以选择不运行它.
I would like to prompt 'Press 'enter' to continue with removal or any other key to stop' before the cleanup stage to give people the option to not run it.
我目前在脚本末尾的代码在这里:
The code I currently have at the end of the script is here:
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 之前,我想要提示按回车键继续或任何其他键中止"
Just prior to #Part2 I would like to have the prompt 'Press enter to continue or any other key to abort'
我似乎找不到一种简单的方法来做到这一点?我所看到的一切似乎都涉及将整个脚本嵌套在我不想这样做的代码块中.=/
I can't seem to find a simple way to do this? everything I've seen appears to involve nesting the entire script inside of a code block which I'd rather not do. =/
谢谢!
推荐答案
你可以这样提示用户:
$response = read-host "Press enter to continue or any other key (and then enter) to abort"
如果用户只是按回车键,则 $response
将为空.Powershell 会将空字符串转换为布尔值 false:
If the user just presses enter, then the $response
will be empty. Powershell will convert an empty string to boolean false:
$aborted = ! [bool]$response
或者你可以只查询一个特定的字符:
Or you can just query for a particular character:
$response = read-host "Press a to abort, any other key to continue."
$aborted = $response -eq "a"
这篇关于PowerShell - 提示“您要继续吗"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!