I have a powershell script that watches a folder for data file to be dropped into it. When it sees a file, it then begins to process it by calling my console application that is written in C#. Now, I can return an error code and determine if the job failed or not. If it fails, it sends out an email. What I am wondering, is if I can return an int error code AND the exception that was thrown on the stack, so that when powershell gets the error code, it can also add the exception that occurred n my C# application to an email it sends out. I currently get the email, and then have to go look at a log file where the C# application writes all exception handling to. IS this possible, or is my only option to write different int values that mean each exception, and then have a case select inside my powershell script to get what the actual error was that caused the fail?
Thanks in advance.
Instead of emailing "File failed....." and getting exit code 0 or greater, can I also recieve the exception that actually cause the error in the powershell script?
Brief example of the Powershell code below with some code removed for certain purposes:
==================================================================================
$emailFrom = [email protected]"
$emailTo = "[email protected]"
$emailSubject = "Overlay File Processing Failed"
$emailBody = ""
$fileCtr = 0
$logPath = "E:\path to log file"
Add-Content -Path $logPath -Value "[$([DateTime]::Now)] Checking for files."
if ($fileCtr -gt 0)
{
Add-Content -Path $logPath -Value "[$([DateTime]::Now)] Found $fileCtr files to process."
ForEach ($_Name in $pdffiles)
{
$fullfile = $pdfdir + $_Name
$StartProc = Start-Process F:\my C# .exe -ArgumentList "$fullfile $outdir" -RedirectStandardOutput "e:\my C# app log.txt"-Wait -PassThru
$exCode = $StartProc.ExitCode
if ($StartProc.ExitCode -ne 0)
{
Get-Content -Path e:\log.txt
$emailBody = "processing failed for file " + $pdfdir + $_Name
send-mailmessage -from $emailFrom -to $emailTo -subject $emailSubject -body $emailBody -smtpServer $emailServer
Add-Content -Path $logPath -Value "[$([DateTime]::Now)] Processing failed for file $fullfile. Exit Code:$exCode"
$fileparts = $_Name.ToString().split(".")
move-item $fullfile ($fileparts[0] + "{0:yyyyMMddhhmmss}.pdf" -f (get-date))
} else {
Add-Content -Path $logPath -Value "[$([DateTime]::Now)] Processing succesfull for file $fullfile"
$fileparts = $_Name.ToString().split(".")
move-item $fullfile ($fileparts[0] + "_{0:yyyyMMddhhmmss}.pdf" -f (get-date))
}
}
} else {
Add-Content -Path $logPath -Value "[$([DateTime]::Now)] No files to process."
}
I found the answer ot my question. YOU CANNOT DO WHAT I AM WISHING TO DO. The routes that can be taken are to write a log file from the console application, and then read it in from the powershell script, or just use certain int values that represent certain failures, and switch through them to report the corresponding error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With