简体   繁体   English

Start-Process -WorkingDirectory 作为管理员未设置位置

[英]Start-Process -WorkingDirectory as administrator does not set location

When I enter the command当我输入命令时

Start-Process powershell -WorkingDirectory "D:\folder"

it opens new PowerShell window with D:\\folder location set.它打开新的 PowerShell 窗口,其中设置了D:\\folder位置。

But when I enter the command但是当我输入命令时

Start-Process powershell -WorkingDirectory "D:\folder" -Verb RunAs

it opens new PowerShell window with admin rights but with C:\\Windows\\system32 location set.它使用管理员权限打开新的 PowerShell 窗口,但设置C:\\Windows\\system32位置。

How can I open new PowerShell window with admin rights and my own location determined?如何使用管理员权限打开新的 PowerShell 窗口并确定我自己的位置?

I also had the same problem and solved it with this command:我也遇到了同样的问题并使用以下命令解决了它:

Start-Process powershell.exe -verb runAs -ArgumentList '-NoExit', '-Command', 'cd D:\folder'

Once you run the above command, Windows will launch with admin authority and the specified directory.运行上述命令后,Windows 将以管理员权限和指定目录启动。

Here's another example which can be used for opening CMD from PowerShell as an administrator into the current folder:这是另一个示例,可用于以管理员身份从 PowerShell 打开 CMD 到当前文件夹:

Start-Process cmd -ArgumentList ("/k cd {0}" -f (Get-Location).path) -Verb RunAs
if used within a script you can use如果在脚本中使用,您可以使用
Start-Process cmd -ArgumentList ("/k cd {0}" -f $PSScriptRoot) -Verb RunAs

If you want to open a new elevated PowerShell session from the current one which is not elevated you can use:如果您想从当前未提升的 PowerShell 会话打开一个新的提升的 PowerShell 会话,您可以使用:
Start-Process powershell.exe -ArgumentList ("-NoExit",("cd {0}" -f (Get-Location).path)) -Verb RunAs
or或者
Start-Process powershell.exe -ArgumentList ("-NoExit",("cd {0}" -f (Get-Location).path)) -Verb RunAs
when used inside scripts在脚本中使用时

Once you run Powershell as administrator;一旦您以管理员身份运行 Powershell;

user the push-location command like so:像这样使用 push-location 命令:

Push-Location -Path C:\

or put it into your script and run the script from the elevated Powershell prompt.或将其放入您的脚本并从提升的 Powershell 提示符运行脚本。

When using Start-Process with -Verb RunAs , a -WorkingDirectory argument is honored if the target executable is a .NET executable ;当使用Start-Process-Verb RunAs ,一个-WorkingDirectory如果目标可执行文件是.NET可执行参数兑现; examples:例子:

  • pwsh.exe (the PowerShell (Core) CLI) does honor it. pwsh.exePowerShell(核心) CLI)确实支持它。
  • cmd.exe and, curiously, powershell.exe (the Windows PowerShell CLI) do not , and invariably use C:\\Windows\\System32 . cmd.exe和奇怪的是powershell.exeWindows PowerShell CLI)没有,并且总是使用C:\\Windows\\System32
    • The problem exists at the level of the .NET API that PowerShell uses behind the scenes (see System.Diagnostics.ProcessStartInfo ), as of this writing (.NET 6.0.0-preview.4.21253.7).在撰写本文 (.NET 6.0.0-preview.4.21253.7) 时,问题存在于 PowerShell 在幕后使用的 .NET API 级别(请参阅System.Diagnostics.ProcessStartInfo )。

Unless you know that you're invoking a .NET executable, a workaround that changes to the desired working folder in the new process is therefore required;除非您知道您正在调用一个 .NET 可执行文件,否则需要一种在新进程中更改所需工作文件夹的解决方法 to offer a more robust alternative toふゆな's helpful answer :ふゆな的有用答案提供更强大的替代方案

$dir = $PWD.ProviderPath # use the current dir.

Start-Process -Verb RunAs powershell.exe @"
-noexit -c Set-Location -LiteralPath "$dir"
"@
  • The embedded "..." quoting around $dir ensures that paths with spaces are also handled correctly. $dir周围的嵌入式"..."引用确保带空格的路径也得到正确处理。 (To use the current directory without an intermediate variable, replace "dir" with "$($PWD.ProviderPath)" . (要使用没有中间变量的当前目录,请将"dir"替换为"$($PWD.ProviderPath)"

    • Using a here-string ( @"<newline>...<newline>"@ ) isn't strictly necessary, but simplifies the embedded quoting;使用here-string ( @"<newline>...<newline>"@ ) 不是绝对必要的,但可以简化嵌入的引用; with a regular expandable string ( "..." ), the embedded " must be escaped as `" (or "" ).使用常规的可扩展字符串( "..." ),嵌入的"必须转义为`" (或"" )。
  • Using $PWD 's .ProviderPath property ensures that a file-system- native path is used (based on drive letters also seen in cmd.exe , for instance), given that the calling session's current location may be based on a PowerShell-only drive (see New-PSDrive ) that the elevated process may not have defined (at all or not based on the same root location).使用$PWD.ProviderPath属性可确保使用文件系统本机路径(例如,基于cmd.exe驱动器号),因为调用会话的当前位置可能仅基于 PowerShell提升的进程可能未定义的驱动器(请参阅New-PSDrive )(完全或不基于相同的根位置)。

    • Caveat : If the native path is on a mapped network drive , this won't work, because elevated processes do not see the same drive mappings;警告:如果本地路径是映射的网络驱动器上,这是行不通的,因为提升的进程看不到相同的驱动器映射; in that event, pass the underlying UNC path.在这种情况下,传递底层UNC路径。

Workaround for launching a GUI application elevated from a given working directory :启动从给定工作目录提升的GUI应用程序的解决方法

Since changing to the working directory must happen in the new, elevated process, a helper shell process is needed to perform this operation, which is best done via cmd.exe (for better performance):由于必须在新的提升进程中更改工作目录,因此需要一个辅助 shell 进程来执行此操作,最好通过cmd.exe完成(以获得更好的性能):

$exeToLaunch = 'Notepad.exe' # may include arguments
$dir = $PWD.ProviderPath # use the current dir.

Start-Process -Verb RunAs -WindowStyle Hidden cmd.exe @"
/c cd "$dir" & $exeToLaunch
"@

I just ran your code example and it opened correctly for me at the WorkingDirectory location.我刚刚运行了您的代码示例,它在WorkingDirectory位置为我正确打开。 Ensure the directory exists before you run the command.在运行命令之前确保该目录存在。 I tested against a drive on C and secondary drive as well and both worked.我也对C驱动器和辅助驱动器上的驱动器进行了测试,两者都有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM