Start-process raises an error when providing Credentials - possible bug

非 Y 不嫁゛ 提交于 2019-11-28 00:47:41
Dionysoos

I have the same bug.

This function is OK with Powershell ISE, but doesn't work with PowerGUI

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

It works with the WorkingDirectory parameter

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

This is a weird one but I recreated the error and this fixed it...

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

Basically, modify the start-in directory for Powershell_ISE (or PowerGUI!) to a system-wide value.

paulf

I know this is rather late, but the thread helped me (particulary the suggestion from @Dionysoos), and hope my answer might help others.

I had the same error...

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

...when running a script unattended, while it was working in the ISE.

The unattended script was using the user-specific $env:TEMP as the working directory which meant that the new process did not have access to it. Specifying -WorkingDirectory $env:windir on the Start-Process command resolved the issue.

mklement0

The best explanation of the problem is buried in a comment by Nathan Hartley, so let me summarize it here:

The issue is solely related to filesystem permissions, and has nothing to do with the host environment (console vs. ISE):

  • When you use Start-Process without specifying a target directory with -WorkingDirectory, PowerShell's current location (directory) is used for the target process as well.

  • Since you're using -Credential to run as a different user - without elevation at that point - the target user may lack permission to access the current directory, which happens if the current directory is inside the current user's home directory subtree, for instance.

    • Unfortunately, PowerShell's error message obscures this cause by misleadingly reporting: The directory name is invalid.

Fix:

  • Either make sure that the current location is accessible to the target user,
  • or, preferably, use the -WorkingDirectory parameter to explicitly set the target process's current directory.

For instance, to start the target process from the directory in which a target script is located, you could use something like:

$script = 'c:\path\to\your\script.ps1'
Start-Process -WorkingDirectory (Split-Path $script) -Credential ...

I know this might be a bit late, but are you running that command when the current directory is a network path? I have experienced this as a problem and if I run the same command from a system drive it works.

Still had issue with setting -WorkingDirectory to exe directory... found that setting -WorkingDirectory to C:\Windows\System32 and used fq path to exe worked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!