代码(不是我的实际代码,但它也会产生相同的行为):
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "The script tries to re-run itself using Administrator privileges.";
try {
Start-Process powershell -ArgumentList ($MyInvocation.MyCommand.Path) -Verb runAs;
}
catch [InvalidOperationException] {
Write-Warning "You canceled the privilege request. This script WON'T WORK, please re-run it!";
Read-Host;
Exit;
}
Exit;
}
# Here comes the code which will be run if started with Admin rights
Read-Host;
当我接受 Windows UAC 并通过 关闭出现的 PS 窗口时[X]
,窗口会在 3-5 秒内消失,而不是直接关闭!
我使用的是64 位Windows 8 Pro。