1

代码(不是我的实际代码,但它也会产生相同的行为):

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。

4

1 回答 1

1

这是一个Read-Host问题。它不仅影响 64 位操作系统,我刚刚在 x86 和 x64 Windows 7 Pro 上进行了验证。要重现,只需Read-Host在任何 PowerShell 控制台上运行并关闭窗口。

更新:

我刚刚在各种版本的 Windows 和 bit-ness 上进行了测试,我发现结果并不一致。Read-Host有时似乎在每次运行时表现不同。

于 2013-01-13T05:49:08.593 回答