【问题标题】:How to exit shell when the script contains restarting a new instance?当脚本包含重新启动新实例时如何退出shell?
【发布时间】: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


    【解决方案1】:

    通常脚本会在最后自动退出。 尝试从参数列表中省略-noexit 参数并删除脚本底部的exit。 对我来说,用-noexit 调用脚本并将exit 放在它的末尾是没有意义的。没有这些,一切都会自动工作。

    如果您想检查脚本编写过程中发生的情况,您可以输入例如。最后是 timeout /t 90 命令,因此您有 90 秒的时间在输出自动关闭之前检查输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2013-03-20
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2018-08-23
      • 2011-09-16
      相关资源
      最近更新 更多