ProcessStartInfo and Process in PowerShell - Authentication Error
Asked Answered
S

1

6

I have code that uses ProcessStartInfo and Process to invoke another script, and to return the output of that script.

Unfortunately, I am getting errors, and I am unsure how to troubleshoot them.

#script1.ps1

$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL

<#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#>
$password = get-content C:\Script\cred.txt | convertto-securestring


$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = "C:\script\script2.ps1", $abc

$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "DOMAIN\Username"
$startInfo.Password = $password

$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$standardOut = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()

# $standardOut should contain the results of "C:\script\script2.ps1"
$standardOut

The errors I get are:

Exception calling "Start" with "0" argument(s): "Logon failure: unknown user name or bad password"
At C:\script\script1.ps1:46 char:15
+ $process.Start <<<< () | Out-Null
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At C:\script\script1.ps1:47 char:49
+ $standardOut = $process.StandardOutput.ReadToEnd <<<< ()
    + CategoryInfo          : InvalidOperation: (ReadToEnd:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "WaitForExit" with "0" argument(s): "No process is associated with this object."
At C:\script\script1.ps1:48 char:21
+ $process.WaitForExit <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

How can I fix this problem?

Sympathetic answered 30/8, 2013 at 19:50 Comment(0)
C
5

It's pretty much spelled out to you, isn't it?:

"Logon failure: unknown user name or bad password"

(first error line).

Note that DOMAIN should be provided in separate property:

$startInfo.Username = "Username"
$startInfo.Domain = "DOMAIN"
Chateau answered 30/8, 2013 at 20:7 Comment(4)
I have verified that the username and password is correct because the same credentials were used correctly in other codeSympathetic
Separate Username into Domain and Username (see edit) - does that solve the problem?Chateau
I'll try this out when I return to office. Have a great weekend!Sympathetic
OK, saw that after I commented. Thanks.Chateau

© 2022 - 2024 — McMap. All rights reserved.