20

Would you possibly know why this error is being raised in response to the code below. User-name and password have been verified as correct.

$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

the error;

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

however, this works just fine.

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

NOTE: this is when executing from within PowerGUI or the ISE ide's The file fb2956d7-87fc-4235-9f3c-742698cafe9f.ps1 does exist at the path location, so for some reason the ide is having dificulty with this. Yet it DOES work when ran directly within the power shell command prompt/shell. I was logged in with a local machine account that is running as local admin, the script directs execution to a domain account which does not have admin rights and would run with just user permissions.

Is this a bug, since as a developer the IDE should not be tripped up by this as it works when i run the block in the powershell command prompt window??

chris
  • 233
  • 2
  • 3
  • 6
  • Tested with my domain user credential and it works! – CB. Sep 06 '11 at 12:19
  • note that this error occurred when using Power GUI or the ISE – chris Sep 06 '11 at 12:22
  • Tested on ISE, it works. – CB. Sep 06 '11 at 12:23
  • might be that you need to test logged in as a local account, then direct the script to your domain account – chris Sep 06 '11 at 12:26
  • Tested on ISE with local account passing domain user credential. It works! – CB. Sep 06 '11 at 12:30
  • As SharePoint\SP_Farm was not a local administrator on the machine running the IDE, When I added the account to the machines local admin group it worked. this must be down to the IDE debugger attempting to attach itself to the process that is not running with permissions to access internal behind the scenes functionality. May be a bug. – chris Sep 06 '11 at 12:35

7 Answers7

19

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'
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
Dionysoos
  • 191
  • 1
  • 2
  • 2
    It isn't an ISE vs console issue. Start-Process seems to be defaulting the Working Directory to the current (or starting) directory. First, changing the directory to one that the account has access to will also resolve the issue. What I did was specify the All Users Profile directory. -WorkingDirectory $env:ALLUSERSPROFILE – Nathan Hartley Jan 19 '16 at 22:19
  • Setting the WorkingDirectory worked for me. The script was running into weird errors when I tried to run it off of a mapped driver. It would only work when I'd execute the script from "\\myRemoteHost\folder\" instead of "S:\folder" – Lucas Pottersky Apr 26 '16 at 21:44
16

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 ...
mklement0
  • 382,024
  • 64
  • 607
  • 775
5

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.

Community
  • 1
  • 1
paulf
  • 121
  • 2
  • 2
5

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.

Community
  • 1
  • 1
nimizen
  • 3,345
  • 2
  • 23
  • 34
  • tl;dr: Modify the shortcut that starts Powershell ISE, update the 'Start in' field to be %WINDIR% instead of %HOMEDRIVE%%HOMEDIR% I can confirm that this fixed powershell ISE for me on windows xp – Ben Sep 19 '13 at 07:34
  • 2
    I don't know what version of PowerShell you're using (or if it matters), but the `Start-Process` cmdlet has a `-WorkingDirectory` parameter. I've found that if you specify something like `-WorkingDirectory C:\ `, it fixes the problem. I believe it's because the credentials you have will be able to see the C:\ drive without any permissions issues. – Phil Feb 17 '14 at 15:50
1

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.

TroyBramley
  • 584
  • 4
  • 7
  • 1
    This comment has been marked 'low-quality' because of its length and content. Perhaps it should have been a comment? – Bill Bell Nov 21 '16 at 15:51
0

In my case, the reason was that Windows ten saved to create a new file in a different direction which I expected, I try to add profile.ps1 file to C:\Users\Username\Documents\WindowsPowerShell but it should be C:\Users\Username\**OneDrive**\Documents\WindowsPowerShell so just try to follow these steps

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
chavy
  • 841
  • 10
  • 20
-1

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

Ryan
  • 1
  • Hi, and Welcome to Stack Overflow! Maybe you could enhance your answer a little bit for user to understand your point. – Ulysse BN Jul 20 '17 at 16:14