gpt4 book ai didi

Powershell - 循环脚本,直到用户选择退出

转载 作者:行者123 更新时间:2023-12-04 22:34:12 24 4
gpt4 key购买 nike

我怎样才能重新开始一个脚本?我有 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.

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

最佳答案

简单、简短、愚蠢:

& cmd /c pause
exit

这甚至会提供 TO 请求的“按任意键”消息。如果您更喜欢留在 PowerShell 中:
Read-Host "Press any key to exit..."
exit

但我们也可以得到输入:
$reply = Read-Host "Please type EXIT to exit"
if ($reply -eq "EXIT") { exit; }

我喜欢 Read-Host输入 Ctrl-C 时退出脚本,如 cmd 的 pause做。

关于Powershell - 循环脚本,直到用户选择退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11637311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com