Powershell按任意键退出

Rob*_*ert 4 powershell powershell-2.0

如何重新启动脚本?我有3个开关,我希望它们恢复到脚本的开头.

Import-Module ActiveDirectory
Write-Host "--Please Login using a.account--"
#login
$credential = Get-Credential
#Main
Write-Host "--Remote Computer Rename v2.0--"
Write-Host "1. Query AD (Outputs to a text file)"
Write-Host "2. Quick computer rename"
Write-host "3. Quit"
$choice=Read-Host "Chose a number to continue"

#AD Query for computer
switch ($choice)
{
 1 {
Write-Host "--Enter first five characters of computer name or full computer name i.e.     USCLT--"
$cn=Read-Host 'Computer name'
$out="$cn*"
Get-ADComputer -Filter 'SamAccountName -like $out' >> c:\myscripts\dsquery.txt
Write-Host "Query complete.  See dsquery.txt saved to Desktop."
}

...rest of my code.
Run Code Online (Sandbox Code Playgroud)

所以看看dsquery.txt保存到桌面后."我希望它回到写主机部分.

And*_*ler 6

简单,简短,愚蠢:

& cmd /c pause
exit
Run Code Online (Sandbox Code Playgroud)

这甚至会提供TO请求的"按任意键"消息.如果您希望继续使用PowerShell:

Read-Host "Press any key to exit..."
exit
Run Code Online (Sandbox Code Playgroud)

但我们也可能得到回馈:

$reply = Read-Host "Please type EXIT to exit"
if ($reply -eq "EXIT") { exit; }
Run Code Online (Sandbox Code Playgroud)

我喜欢Read-Host在键入Ctrl-C时退出脚本,就像cmd pause一样.