PowerShell 提示继续执行代码

Abr*_*xas 15 scripting powershell

我有一个用于自动化WSUS进程的脚本,它的最后一个阶段继续删除所有旧的/不必要的文件/对象。

我想在清理阶段之前提示“按‘回车’继续删除或任何其他键停止”,让人们可以选择不运行它。

我目前在脚本末尾的代码在这里:

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
Run Code Online (Sandbox Code Playgroud)

就在#Part2 之前,我想要提示“按回车继续或任何其他键中止”

有没有一种简单的方法可以做到这一点?

我所看到的一切似乎都涉及将整个脚本嵌套在我不想这样做的代码块中。=/

小智 13

另一个简单的解决方案是使用:

Read-Host -Prompt "Press any key to continue or CTRL+C to quit" 
Run Code Online (Sandbox Code Playgroud)

我相信这是对当前接受的答案的更好解决方案,因为需要在键盘上按 Enter 键。我不相信按 Enter 键会接受 UI 提示,除非该 UI 元素处于焦点。


小智 7

您可以使用写警告选项。相当时尚:

Write-Warning "This is only a test warning." -WarningAction Inquire
WARNING: This is only a test warning.
Confirm
Continue with this operation?
 [Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"):
Run Code Online (Sandbox Code Playgroud)


Rob*_*yer 5

只需将 -confirm 添加到您的 Invoke-WsusServerCleanup 命令。它是内置的。