【发布时间】:2021-10-19 04:23:33
【问题描述】:
在我的脚本的开头,我添加了一段代码来确保 shell run with full admin privileges:
# Running with full admin privileges
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
# tried to elevate, did not work, aborting
} else {
Start-Process pwsh -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
# Other stuff below...
之后是一个普通的脚本(Other stuff below 部分)。问题是,我想在脚本完成后自动关闭 shell 窗口。在我添加完整的特权代码之前,将exit 放在脚本底部可以正常工作,但现在不行了。我不明白为什么,因为本节中的其他所有内容都可以正常工作。
【问题讨论】:
标签: powershell exit