16

您可能知道为什么响应下面的代码会引发此错误。用户名和密码已被验证为正确。

$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

错误;

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

但是,这很好用。

Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait

注意:这是在 PowerGUI 或 ISE ide 中执行时。文件 fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1 确实存在于路径位置,因此出于某种原因,ide 对此有困难。然而,当直接在 power shell 命令提示符/shell 中运行时,它确实有效。我使用以本地管理员身份运行的本地计算机帐户登录,该脚本将执行定向到没有管理员权限且仅使用用户权限运行的域帐户。

这是一个错误吗,因为作为开发人员,IDE 不应该被它绊倒,因为当我在 powershell 命令提示符窗口中运行该块时它可以工作?

4

7 回答 7

16

我有同样的错误。

此功能适用于 Powershell ISE,但不适用于 PowerGUI

Start-Process -FilePath "C:\WINDOWS\System32\cmd.exe" -Credential $credential -ArgumentList ("/c $sFileExecutable")

它与WorkingDirectory参数一起使用

Start-Process -FilePath 'cmd.exe' -Credential $credential -ArgumentList ("/c $sFileExecutable") -WorkingDirectory 'C:\Windows\System32'
于 2014-06-06T08:00:54.760 回答
12

对此问题的最佳解释隐藏在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 ...
于 2017-02-11T19:35:34.937 回答
5

这是一个奇怪的错误,但我重新创建了错误并修复了它......

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

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

于 2011-09-06T12:42:45.650 回答
5

我知道这已经很晚了,但是该线程帮助了我(特别是来自@Dionysoos 的建议),并希望我的回答可能对其他人有所帮助。

我有同样的错误......

Start-Process : This command cannot be executed due to the error: The directory name is invalid.

...在无人看管的情况下运行脚本时,而它在 ISE 中工作。

无人参与的脚本使用特定于用户的$env:TEMP作为工作目录,这意味着新进程无权访问它。指定命令解决了这个问题-WorkingDirectory $env:windirStart-Process

于 2016-11-21T00:33:17.793 回答
1

我知道这可能有点晚了,但是当当前目录是网络路径时,您是否正在运行该命令?我遇到过这个问题,如果我从系统驱动器运行相同的命令,它就可以工作。

于 2011-11-21T16:28:58.927 回答
0

就我而言,原因是 Windows 10 保存以在我期望的不同方向上创建一个新文件,我尝试将profile.ps1文件添加到C:\Users\Username\Documents\WindowsPowerShell但它应该C:\Users\Username\**OneDrive**\Documents\WindowsPowerShell 只是尝试按照这些步骤操作

RUN:
Test-Path C:\Users\User\Documents\WindowsPowerShell\
IF FALSE
New-Item -Path C:\Users\User\Documents\WindowsPowerShell\ -ItemType Directory
THEN
New-Item -Path $profile.CurrentUserAllHosts -Type File
THEN
start $profile.CurrentUserAllHosts
于 2020-03-22T17:36:23.300 回答
-1

将 -WorkingDirectory 设置为 exe 目录仍有问题...发现将 -WorkingDirectory 设置为 C:\Windows\System32 并使用 fq 路径到 exe 有效。

于 2017-07-20T15:40:44.823 回答