Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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??

like image 404
chris Avatar asked Sep 06 '11 12:09

chris


2 Answers

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'
like image 118
Dionysoos Avatar answered Oct 08 '22 00:10

Dionysoos


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 ...
like image 12
mklement0 Avatar answered Oct 08 '22 01:10

mklement0