使用命令提示符以提升的权限调用 PowerShell 脚本

huangapple go评论63阅读模式
英文:

Call a PowerShell script with elevated privileges via command prompt

问题

I want to call my PowerShell script with elevated privileges in a non-administrator command prompt.
When I manually open the Command Prompt with 'Run as Administrator' and enter the line:

powershell -ExecutionPolicy Unrestricted -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File example.ps1 param1'"

My script PowerShell script runs just fine.

However, when I run it in a non-administrator command prompt, I see a PowerShell window appear for a split second and exits.

Any ideas on how to resolve this issue?

  • Same thing happens, the window opens for a split second and then exits

https://stackoverflow.com/questions/48945292/run-powershell-script-via-batch-file-with-elevated-privileges

runas.exe /netonly /noprofile /user:domainadm@domain "powershell.exe -noprofile -File "C:\Users...\Desktop\Powershell_scripts\New-ADuser.ps1" -verb RunAs"

  • Same thing happens here as well, the window opens for a split second and then exits

https://stackoverflow.com/questions/48838760/run-a-powershell-script-from-batch-file-with-elevated-privileges

powershell -Command "&{ Start-Process powershell -ArgumentList '-File example.ps1 param1' -Verb RunAs}"

  • Same issue, the window opens for a split second and then exits

https://stackoverflow.com/questions/65679034/run-a-powershell-script-from-cmd-with-elevated-privilages-and-passing-parameters

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "example.ps1 param1"'"

CONTEXT: My PowerShell script is for downloading a NSIS Installer specified at param1

EDIT: Script code:

$param1 = $args[0]
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

$installer = Start-Process -FilePath $param1 -PassThru
$installerWindowName = "Installer Setup"

while (-not ($installer.MainWindowTitle -match "$installerWindowName")) {
Start-Sleep -Milliseconds 100
$installer.Refresh()
}

英文:

Problem:

I want to call my PowerShell script with elevated privileges in a non-administrator command prompt.
When I manually open the Command Prompt with 'Run as Administrator' and enter the line:

powershell -ExecutionPolicy Unrestricted -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File example.ps1 param1'"

My script PowerShell script runs just fine.

However, when I run it in a non-administrator command prompt, I see a PowerShell window appear for a split second and exits.

Any ideas on how to resolve this issue?

Tries

  • Same thing happens, the window opens for a split second and then exits

https://stackoverflow.com/questions/48945292/run-powershell-script-via-batch-file-with-elevated-privileges

runas.exe /netonly /noprofile /user:domainadm@domain "powershell.exe -
noprofile -File "C:\Users\...\Desktop\Powershell_scripts\New-
ADuser\.ps1" -verb RunAs"
  • Same thing happens here as well, the window opens for a split second and then exits

https://stackoverflow.com/questions/48838760/run-a-powershell-script-from-batch-file-with-elevated-privileges

powershell -Command "&{ Start-Process powershell -ArgumentList '-File example.ps1 param1' -Verb RunAs}"
  • Same issue, the window opens for a split second and then exits

https://stackoverflow.com/questions/65679034/run-a-powershell-script-from-cmd-with-elevated-privilages-and-passing-parameters

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "example.ps1 param1"'"

CONTEXT: My PowerShell script is for downloading a NSIS Installer specified at param1

EDIT: Script code:

$param1 = $args[0]
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

$installer = Start-Process -FilePath $param1 -PassThru
$installerWindowName = "Installer Setup"

while (-not ($installer.MainWindowTitle -match "$installerWindowName")) {
    Start-Sleep -Milliseconds 100
    $installer.Refresh()
}

答案1

得分: 0

* 在 Windows 上,从非提升的控制台启动的提升进程始终在新窗口中运行,当提升进程退出时会自动关闭。

  * 用于故障排除的方法是在 `-Command` 参数之前加上 `-NoExit`,这会使提升的 PowerShell 会话保持打开状态,直到手动退出,从而允许检查可能发生的任何错误。

* 在 Windows PowerShell 中(其 [CLI](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe) 是 `powershell.exe`),提升的进程将 `$env:Windir\System32` 作为其工作目录(幸运的是,[PowerShell(核心)7+](https://github.com/PowerShell/PowerShell/blob/master/README.md) 现在会保留调用者的工作目录)。

  * 因此,使用相对路径如 `example.ps1` 是行不通的,你应该传递完整的路径。
    * 如果你的脚本依赖于工作目录与调用者相同,那么需要更多的工作。

注意 "$PWD\example.ps1" 部分

powershell -ExecutionPolicy Bypass -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "$PWD\example.ps1" param1'"

英文:

<!-- language-all: sh -->

  • On Windows, an elevated process launched from a non-elevated console invariably runs in a new window, which closes automatically when the elevated process exits.

    • For troubleshooting, you can place -NoExit before the -Command argument, which keeps the elevated PowerShell session open until you exit it manually, allowing you to inspect any errors that may have occurred.
  • In Windows PowerShell (whose CLI is powershell.exe), an elevated process launches with $env:Windir\System32 as its working directory (fortunately, PowerShell (Core) 7+ now preserves the caller's working directory).

    • Therefore, using a relative path such as example.ps1 does not work, and you should pass a full path instead.
      • If your script relies on the working directory to be the same as the caller's, more work would be needed.
# Note the `&quot;$PWD\example.ps1`&quot; part
powershell -ExecutionPolicy Bypass -Command &quot;Start-Process Powershell -Verb RunAs -Wait -ArgumentList &#39;-NoProfile -ExecutionPolicy Bypass -File `&quot;$PWD\example.ps1`&quot; param1&#39;&quot;

huangapple
  • 本文由 发表于 2023年3月4日 06:09:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定