我正在使用PowerShell启动一个名为GoodSync的应用程序来为我执行备份。在过程结束时,我会把结果发电子邮件给我。我还喜欢(有时)观看PowerShell窗口,以监视它,并在处理过程中观察备份的状态。现在,当我的代码处于位置时,stdout将用于控制台,仅此而已。
我的问题:是否可能让stdout转到控制台,并将其保存到一个变量中以供以后处理?
您可以看到,我正在尝试使用$output变量,但一无所获。下面是$output返回的错误:
You cannot call a method on a null-valued expression.
At GoodSync.ps1:119 char:42
+ $output = $proc.StandardOutput.ReadToEnd <<<< ()
+ CategoryInfo : InvalidOperation: (ReadToEnd:String) [], Runtime
Exception
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At GoodSync.ps1:120 char:42
+ $output += $proc.StandardError.ReadToEnd <<<< ()
+ CategoryInfo : InvalidOperation: (ReadToEnd:String) [], Runtime
Exception
+ FullyQualifiedErrorId : InvokeMethodOnNull这是我的密码:
(为了让所有的专家都保持积极主动,是的,我做了大量的工作,将这些代码输出到文件和stdout中,这样我就可以监视所有的事情了。这个部分只是整个文件的一个片段。)
###############################
## Call the GoodSync process ##
###############################
# This is the section taken from http://stackoverflow.com/questions/8925323
# This is a known working section. However, right now I don't know how to save the stdout to variable and insert into email log file.
$procInfo = New-Object System.Diagnostics.ProcessStartInfo
$procInfo.FileName = "C:\Program Files\Siber Systems\GoodSync\gsync.exe"
$procInfo.UseShellExecute = $false
$procInfo.Arguments = '/progress=yes /exit sync MyBackupJobName'
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $procInfo
$proc.Start() | Out-Host
(Get-Date -format T) + " - Loaded gsync.exe. Backup process running. Please stand by..." | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
$proc.WaitForExit()
(Get-Date -format T) + " - Backup complete and gsync.exe has exited." | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
# Now we take the exit code, write it to console and log file, and begin to build our email report.
if ($proc.ExitCode -eq 0) {
"Success: GoodSync Reported: Analyze or Sync Successfully Completed." | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
$subject = $RoboLog
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
} elseif ($proc.ExitCode -eq 1) {
"Failure: GoodSync Error: Analyze had Terminal Errors. Did gsync.exe close abruptly?" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
$subject = $RoboLog
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
} elseif ($proc.ExitCode -eq 2) {
"Failure: GoodSync Error: Sync had Terminal Errors. Did gsync.exe close abruptly?" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
$subject = $RoboLog
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
} else {
"Failure: GoodSync Error: General Error. Check log file." | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
$subject = $RoboLog
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
}
# Grab the stdout and stderr to a variable
$output = $proc.StandardOutput.ReadToEnd()
$output += $proc.StandardError.ReadToEnd()
# The ReadToEnd() makes everything 1 long string, so we break it up by carriage return, and filter
$output -split "`n"
# Write to log file for email capture
$output >> $myLogFile
"" | Tee-Object -Variable RoboLog
$RoboLog >> $myLogFile
#---------------------------------------------------------------------------------------------------
# GoodSync backup jobs are now complete.
#---------------------------------------------------------------------------------------------------发布于 2013-04-23 20:29:58
来自$proc.StandardOutput.ReadToEnd (You cannot call a method on a null-valued expression.)的错误是因为您需要在ProcessStartInfo对象上设置这些属性:
$procInfo.RedirectStandardError = $true
$procInfo.RedirectStandardOutput = $true注意-当设置这些时,输出将不会显示在控制台上。
下面是一种将输出存储在变量中并使用Tee-Object在控制台上显示的方法
& ping.exe localhost -n 1 | Tee-Object -Variable output
$output顺便说一句,你可以在$LASTEXITCODE上做一个开关。
switch ($LASTEXITCODE) {
0 {'good'}
1 {'bad'}
2 {'ugly'}
}发布于 2013-04-23 20:34:23
为什么不直接把它作为背景工作来运行呢?然后,您可以使用接收-作业来获取输出(如果使用-Keep,可以使用任意次数的输出,并对它做任何您想做的事情。
https://stackoverflow.com/questions/16178319
复制相似问题