flu*_*ter 3 powershell windows-10
使用Start-Process
,当Verb
使用时,该Workingdirectory
选项不起作用,新的 powershell 总是以C:\WINDOWS\system32
. 为什么是这样?如果没有额外的cd
命令,我怎么能做到这一点?
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14393.0
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.0
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\> Start-Process -FilePath powershell.exe -Verb Runas -WorkingDirectory C:\ws\
# the new ps shell always in system32:
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS C:\WINDOWS\system32> pwd
Path
----
C:\WINDOWS\system32
Run Code Online (Sandbox Code Playgroud)
至于为什么-请参阅此答案的底部。
至于有效的解决方法(但是,需要使用cd
( Set-Location
)):
Start-Process -FilePath powershell.exe -Verb Runas `
-ArgumentList '-NoExit -Command "cd C:\ws"'
Run Code Online (Sandbox Code Playgroud)
为了避免引用麻烦,您还可以单独传递参数,除了命令字符串本身:
Start-Process -FilePath powershell.exe -Verb Runas `
-ArgumentList '-NoExit', '-Command', 'cd C:\ws'
Run Code Online (Sandbox Code Playgroud)
Venryx指出,如果要将此技术应用于调用通常使用-File
而不是调用的脚本文件,则-Command
必须切换到-Command
先更改位置然后 ( ;
) 调用的方法(假设两个参数不能组合)( &
) 脚本,script.ps1
在新位置,这个例子:
Start-Process -FilePath powershell.exe -Verb Runas `
-ArgumentList '-Command', 'cd C:\ws; & .\script.ps1'
Run Code Online (Sandbox Code Playgroud)
有关涉及嵌套引用的更复杂示例,请参阅此答案。
在实践中 - 并且文档没有提到-如果您启动提升的进程(具有管理权限,这是- 有点模糊 - 所做的),则不会遵守该-WorkingDirectory
参数:位置默认为-Verb RunAs
$env:SYSTEMROOT\system32
(通常为C:\Windows\System32
)。
请注意,这不是-Verb
参数本身的问题,而是它的具体RunAs
参数。
与 结合使用的唯一情况-WorkingDirectory
是-Verb RunAs
正在启动的程序是否为 PowerShell Core,即pwsh.exe
- 无论您是从Windows PowerShell 还是 PowerShell Core调用。
这种行为是否有很好的技术原因,或者这是否是一个错误,我不知道。如果您这样做,请告诉我们。
在所有其他场景中:
以当前用户身份运行(默认)
冒充不同的用户-Verb RunAsUser
(使用始终交互的凭据提示)
作为不同的用户以非交互方式运行 -Credential <PSCredential>
该-WorkingDirectory
参数被尊重。
但是,如果(不同的)目标用户缺乏访问隐含(当前位置)或明确指定的工作目录(via -WorkingDirectory
)的权限,则当前位置默认为C:\
情况(2),并导致命令失败,Start-Process
情况为( 3)。
归档时间: |
|
查看次数: |
1002 次 |
最近记录: |