英文:
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
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
powershell -Command "&{ Start-Process powershell -ArgumentList '-File example.ps1 param1' -Verb RunAs}"
- Same issue, the window opens for a split second and then exits
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
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
powershell -Command "&{ Start-Process powershell -ArgumentList '-File example.ps1 param1' -Verb RunAs}"
- Same issue, the window opens for a split second and then exits
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.
- For troubleshooting, you can place
-
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.
- Therefore, using a relative path such as
# Note the `"$PWD\example.ps1`" part
powershell -ExecutionPolicy Bypass -Command "Start-Process Powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File `"$PWD\example.ps1`" param1'"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论