Friday, July 20, 2012

Powershell invalid directory name

I'm still a newcomer in the world of powershell.  The following code generated this error.

Exception calling "Start" with "1" argument(s): "The directory name is invalid"
At M:\Temp\test.ps1:19 char:36
+ [System.Diagnostics.Process]::Start <<<< ($startinfo)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

$key = (3,4,2,3,56,34,254,222,1,1,2,23,42,54,33,233,1,34,2,7,6,5,35,43)
$encrypted = Get-Content encrypted.txt
$secure2 = Get-Content encrypted.txt | ConvertTo-SecureString -key $key

$startinfo = new-object System.Diagnostics.ProcessStartInfo
$startinfo.UserName = "scripter"
$startinfo.Password = $secure2
$startinfo.FileName = "notepad.exe"
$startinfo.UseShellExecute = $false
$startinfo.WorkingDirectory = "C:\"

The problem was the ommission of the highlighted line.  Not even sure why I need this as with a simplified version of the code I did not. Oh well.