2013-08-30 2 views
3

У меня есть код, который использует ProcessStartInfo и Process для вызова другого скрипта и возврата вывода этого скрипта.ProcessStartInfo и процесс в PowerShell - ошибка аутентификации

К сожалению, я получаю ошибки, и я не уверен, как их устранять.

#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 

Ошибки я получаю:

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 

Как я могу исправить эту проблему?

ответ

3

Это в значительной степени прописана к вам, не так ли ?:

«Ошибка входа: неизвестное имя пользователя или неверный пароль»

(первая линия ошибки).

Обратите внимание, что DOMAIN должна быть предоставлена ​​в обособленном имуществе:

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

Я проверил, что имя пользователя и пароль правильно, так как одни и те же учетные данные были правильно использованы в другом коде – Glowie

+0

отдельного имени в домен и имя пользователя (см редактировать) - это решение проблемы? –

+0

Я попробую это, когда вернусь в офис. Хороших выходных! – Glowie

Смежные вопросы