Most Valuable Professional
Group: Forum Members
(EDIT: Having gotten a chance to test all of this, turns out neither of the methods below works. I'm not sure why Reflect's output can't be captured through these conventional methods that work on other applications. I'm guessing it's either because the output is explicitly designated as just console output that can't be redirected to a file, rather than text output that can be, or else perhaps that Reflect.exe calls another application to actually do the work. Surprisingly, even using Start-Transcript didn't capture the output, even though Microsoft says it captures all output that appears in the console. Anyhow, I'm stuck at this point, but I'll update this post if I figure this out.)
If you just want everything you see when you run the script manually, the easiest way to send that to a file is to execute the script with syntax like this:
C:\scripts\Reflect.ps1 > C:\Logs\ReflectLog.txt
Using >> rather than just > as above will cause PowerShell to append to the output file rather than overwriting it, but either way you'd have to edit the scheduled task manually to achieve that syntax.
Alternatively, if you want to generate uniquely named log files for each job run (and/or avoid a manual scheduled task), you could leave the task alone and edit the script instead. I haven't tested this, but it should work. Find this line in the script:
$iResult = (Start-Process -FilePath $strReflectPath -ArgumentList $strArgs -PassThru -Wait).ExitCode;
and replace it with all of this:
$Date = Get-Date
$XMLFileName = (($strXmlFilePath -split '\\')[-1]).trim('.xml')
$LogFilePath = "C:\Logs\ReflectLog_$($XMLFileName)_$($Date.ToString('yyyy-MM-dd_HHmm')).txt"
$RunBackup = Start-Process -FilePath $strReflectPath -ArgumentList $strArgs -PassThru -Wait -RedirectStandardOutput $LogFilePath
$iResult = $RunBackup.ExitCode;
Note that the above design will only send the output of the call to Reflect to the log file, whereas the first option sends the output of the entire script to the log file.