chr*_*ris 14 powershell start-process
您是否可能知道为什么会出现此错误以响应下面的代码.用户名和密码已验证为正确.
$secPassword = ConvertTo-SecureString "Password" -AsPlaintext -Force
$farmCredential = New-Object System.Management.Automation.PsCredential "SharePoint\SP_Farm",$secPassword
Start-Process $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait
Run Code Online (Sandbox Code Playgroud)
错误;
Start-Process : This command cannot be executed due to the error: The directory name is invalid.
At C:\Users\Administrator.SHAREPOINT\AppData\Local\Temp\fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1:8 char:14
+ Start-Process <<<< $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output
`"Hello:`"`$outvar1`"}`"" -Wait
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Run Code Online (Sandbox Code Playgroud)
但是,这很好用.
Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait
Run Code Online (Sandbox Code Playgroud)
注意:这是在PowerGUI或ISE ide中执行的时候.文件fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1确实存在于路径位置,因此由于某种原因,ide对此有了不足之处.然而,它直接在power shell命令提示符/ shell中运行时工作.我使用本地管理员运行的本地计算机帐户登录,该脚本将执行指向没有管理员权限的域帐户,并且仅使用用户权限运行.
这是一个错误,因为作为一个开发人员,IDE不应被这个绊倒,因为它在powershell命令提示符窗口中运行块时工作?
小智 11
我有同样的错误.
Powershell ISE可以使用此功能,但不适用于PowerGUI
Start-Process -FilePath "C:\WINDOWS\System32\cmd.exe" -Credential $credential -ArgumentList ("/c $sFileExecutable")
Run Code Online (Sandbox Code Playgroud)
它适用于WorkingDirectory参数
Start-Process -FilePath 'cmd.exe' -Credential $credential -ArgumentList ("/c $sFileExecutable") -WorkingDirectory 'C:\Windows\System32'
Run Code Online (Sandbox Code Playgroud)
mkl*_*nt0 11
对问题的最佳解释隐藏在Nathan Hartley的评论中,所以让我在这里总结一下:
该问题仅与文件系统权限有关,与主机环境(控制台与 ISE)无关:
当您在不使用Start-Process指定目标目录的情况下使用时-WorkingDirectory,PowerShell 的当前位置(目录)也用于目标进程。
由于您正在-Credential以不同的用户身份运行- 那时没有提升 -目标用户可能缺乏访问当前目录的权限,例如,如果当前目录位于当前用户的主目录子树中,就会发生这种情况。
The directory name is invalid.修复:
-WorkingDirectory参数显式设置目标进程的当前目录。例如,要从目标脚本所在的目录启动目标进程,您可以使用以下内容:
$script = 'c:\path\to\your\script.ps1'
Start-Process -WorkingDirectory (Split-Path $script) -Credential ...
Run Code Online (Sandbox Code Playgroud)
这是一个很奇怪的问题,但是我重新创建了错误并解决了这个问题。
http://support.microsoft.com/kb/832434
基本上,将Powershell_ISE(或PowerGUI!)的启动目录修改为系统范围的值。
小智 5
我知道这已经很晚了,但是该线程帮助了我(特别是来自 @Dionysoos的建议),希望我的回答可以帮助其他人。
我有同样的错误...
Start-Process : This command cannot be executed due to the error: The directory name is invalid.
Run Code Online (Sandbox Code Playgroud)
...在无人看管的情况下运行脚本时,它在 ISE 中工作。
无人参与的脚本使用特定于用户的$env:TEMP作为工作目录,这意味着新进程无法访问它。-WorkingDirectory $env:windir在Start-Process命令上指定解决了问题。
| 归档时间: |
|
| 查看次数: |
30988 次 |
| 最近记录: |