Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用快捷方式以管理员身份运行PowerShell脚本?_Powershell_Working Directory_Elevated Privileges - Fatal编程技术网

如何使用快捷方式以管理员身份运行PowerShell脚本?

如何使用快捷方式以管理员身份运行PowerShell脚本?,powershell,working-directory,elevated-privileges,Powershell,Working Directory,Elevated Privileges,我正在尝试使用快捷方式以管理员身份运行PowerShell脚本。我尝试了很多方法,但仍然不起作用: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -Verb RunAs Start-Process powershell.exe -ArgumentList '-file C:\project\test.ps1' 使用此命令,它将创建两个PowerShell窗口,一

我正在尝试使用快捷方式以管理员身份运行PowerShell脚本。我尝试了很多方法,但仍然不起作用:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -Verb RunAs Start-Process powershell.exe -ArgumentList '-file C:\project\test.ps1'
使用此命令,它将创建两个PowerShell窗口,一个窗口将关闭

我也试过这个:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit Start-Process powershell.exe -Verb RunAs -File 'C:\project\test.ps1'
有人能帮忙吗?

Tl;博士 这将实现以下目的:

powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"

解释 首先,您必须调用PowerShell才能执行
启动进程
。此时不需要任何其他参数,因为您只需使用第一个PowerShell启动另一个PowerShell。你是这样做的:

powershell.exe -Command "& {...}"
在花括号内,可以插入任何脚本块。首先,您将检索当前工作目录(CWD)以在新启动的PowerShell中进行设置。然后使用
启动进程
调用PowerShell,并添加
-动词RunAs
参数来提升它:

$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList ...
然后需要将所有所需的PowerShell参数添加到
参数列表中。在您的情况下,这些将是:

-ExecutionPolicy ByPass -NoExit -Command ...
最后,将要执行的命令传递给
-Command
参数。基本上,您希望调用脚本文件。但在执行此操作之前,将CWD设置为以前检索到的目录,然后调用脚本:

Set-Location $wd; C:\project\test.ps1
总计:

powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"

我还尝试了“powershell.exe-ExecutionPolicy Bypass-NoExit-File”C:\project\test.ps1“我可以正常运行,但不能以管理员身份运行,因为当我运行管理员时,它将转到我的Powershell路径,而不是项目。如果您只想以管理员身份启动Powershell,它将是这样的
Powershell.exe-WindowStyle Hidden-command“&{start process Powershell-verb runas}”
@AaronLi您能给出任何反馈吗?我的解决方案对你无效吗?你找到另一个解决方案了吗?你的问题还存在吗?