提供凭据时,启动进程会引发错误 - 可能的错误

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)

  • 嗨,欢迎来到StackOverflow.我编辑了你的答案,以便更容易理解.请看看它是否正确. (2认同)
  • 这不是 ISE 与控制台的问题。Start-Process 似乎将工作目录默认为当前(或起始)目录。首先,将目录更改为帐户有权访问的目录也将解决该问题。我所做的是指定所有用户配置文件目录。-WorkingDirectory $env:ALLUSERSPROFILE (2认同)

mkl*_*nt0 11

对问题的最佳解释隐藏在Nathan Hartley评论中,所以让我在这里总结一下:

该问题仅与文件系统权限有关,与主机环境(控制台与 ISE)无关:

  • 当您在不使用Start-Process指定目标目录的情况下使用时-WorkingDirectory,PowerShell 的当前位置(目录)也用于目标进程。

  • 由于您正在-Credential以不同的用户身份运行- 那时没有提升 -目标用户可能缺乏访问当前目录的权限,例如,如果当前目录位于当前用户的主目录子树中,就会发生这种情况。

    • 不幸的是,PowerShell 的错误消息通过误导性报告掩盖了这一原因: 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)


nim*_*zen 5

这是一个很奇怪的问题,但是我重新创建了错误并解决了这个问题。

http://support.microsoft.com/kb/832434

基本上,将Powershell_ISE(或PowerGUI!)的启动目录修改为系统范围的值。

  • 我不知道您使用的是哪个版本的PowerShell(或者是否重要),但是Start-Process cmdlet具有一个-WorkingDirectory参数。我发现如果您指定类似`-WorkingDirectory C:\`之类的东西,它将解决此问题。我相信这是因为您拥有的凭据将能够看到C:\驱动器而没有任何权限问题。 (2认同)

小智 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:windirStart-Process命令上指定解决了问题。