Start-Job: "The directory name is invalid"

Envoyer Imprimer

The problem could occur when you try to start a background process with the "start-job" cmdlet and with the "-credential" parameter.


The job is in failure with the error message "there is an error while laucnhing the background process. Error reported: The directory name is invalid".


After a quick search, I have found this microsoft KB which is describing clearly my problem. In fact, the default working directory defined for PowerShell is pointing to an user's mapped home drive (you can verify it through the properties of the Powershell's shortcut).


So, the first thing to do is to change the parameter "Start in" (for this, you will need local administrator rights). For example, I have defined it to "%WINDIR%".


After closing and reopening the PowerShell's console, when you retry to execute the command, it's working fine!

Definitely, that was a problem of access with the working directory. So locally, you just have to change the parameter "Start in" of the PowerShell's shortcut. But, in a large environment and especially if user's home directories are pointing to a network share (or a mapped drive), you are not probably able to change this property. The solution is to use the .Net property "[environment]::Directory" which permits to set the fully qualified path for a specific working directory.

The command line would be "start-job -credential 'DOMAIN\username' -ScriptBlock { write-host 'Is it working like that?' } -ArgumentList([environment]::CurrentDirectory='c:\temp')"

Mise à jour le Jeudi, 19 Juillet 2012 21:21