ProcessStartInfo和PowerShell中的进程-身份验证错误

ProcessStartInfo和PowerShell中的进程-身份验证错误,powershell,process,invoke,processstartinfo,Powershell,Process,Invoke,Processstartinfo,我的代码使用ProcessStartInfo和Process调用另一个脚本,并返回该脚本的输出 不幸的是,我得到了错误,我不知道如何解决它们 #script1.ps1 $abc = $args $startInfo = $NULL $process = $NULL $standardOut = $NULL <#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | conve

我的代码使用ProcessStartInfo和Process调用另一个脚本,并返回该脚本的输出

不幸的是,我得到了错误,我不知道如何解决它们

#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
#script1.ps1
$abc=$args
$startInfo=$NULL
$process=$NULL
$standardOut=$NULL
$password=获取内容C:\Script\cred.txt |转换为securestring
$startInfo=新对象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=“域\用户名”
$startInfo.Password=$Password
$process=新对象System.Diagnostics.process
$process.StartInfo=$StartInfo
$process.Start()| Out Null
$standardOut=$process.StandardOutput.ReadToEnd()
$process.WaitForExit()
#$standardOut应包含“C:\script\script2.ps1”的结果
$standardOut
我得到的错误是:

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
使用“0”参数调用“Start”时出现异常:“登录失败:未知用户名或密码错误”
在C:\script\script1.ps1:46 char:15

+$process.Start这对您来说很清楚,不是吗

“登录失败:未知用户名或错误密码”

(第一个错误行)

请注意,域应在单独的属性中提供:

$startInfo.Username = "Username"
$startInfo.Domain = "DOMAIN"

我已经验证了用户名和密码是正确的,因为在其他代码中正确使用了相同的凭据将用户名分为域和用户名(请参见编辑)-这解决了问题吗?我将在返回办公室时尝试一下。祝你周末愉快!好,我评论完以后看到了。谢谢