0

I need to log the process in a text file and there are two problems: 1. How to write errors in a text file? Now when the e-mails are not sent, nothing is logged. 2. I need to write time, date and event in a single line

Now I have this in log.txt:

17. feb. 2012 10:47:34
Chyba: .gpg neexistuje
17. feb. 2012 10:57:28
Test.gpg existuje.

This is my code:

function write-log{
param(
[string]$mytext,
[string]$fgc
)
if(!$fgc){$fgc="Black"}
write-host $mytext -foregroundcolor $fgc
$myfile = "c:\gnupg\subor\log.txt"
Get-Date | Out-File $myfile -append
$mytext | Out-File $myfile -append
}

if(test-path "d:\vm\shared pre ws08R2+SQL05\SFRB\*.chk") {
echo "Subor .chk existuje a jeho nazov je " 
get-childitem "d:\vm\shared pre ws08R2+SQL05\SFRB\*.chk" -Name
$a = get-childitem "d:\vm\shared pre ws08R2+SQL05\SFRB\*" -include *.chk -name | Foreach-Object {$a -replace ".chk", ""}

if(test-path d:\vm\"shared pre ws08R2+SQL05"\SFRB\$a.gpg) {
    Write-Log $a".gpg existuje." Green

    Write-Log "Presuvam do banky subor $a.gpg.." Green

    Move-Item "d:\vm\shared pre ws08R2+SQL05\SFRB\$a.gpg" c:\gnupg\subor\$a.gpg
    Write-Log "Presun ukonceny." Green

    Write-Log "Presuvam do banky subor $a.chk.." Green
    Move-Item "d:\vm\shared pre ws08R2+SQL05\SFRB\$a.chk" c:\gnupg\subor\$a.chk
    Write-Log "Presun ukonceny. Subor je pripraveny na spracovanie." Green

    Write-Log "Posielam notifikacne maily.." Green
    $emailFrom = "[email protected]"
    $emailTo = "[email protected]"
    $subject = "subject"
    $body = "subor presunuty"
    $smtpServer = "87.244.217.54"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($emailFrom, $emailTo, $subject, $body)
    # Write-Log "Maily odoslane." Green

        }
        else {
                Write-Log " Chyba: $a.gpg neexistuje" Magenta
        }
} else {
Write-log "V cielovom adresari neexistuje ziadny subor .chk."
}

Thank you.

1 Answer 1

1
  1. Make use of the inbuilt Send-MailMessage cmdlet with parameter -ErrorAction Stop.

  2. Use a try-catch around this (see about_try_catch_finally to pick up and handle any errors.

    In the catch block $_ will be the error message (as would be shown on the console), and has an Exception property which is the (.NET) Exception (or subclass) instance (I use $_.Exception.GetType() below to specifically report this type as it is an important part of any diagnostics):

Eg. in a script I have:

    try {
        $trans = (MakeSummaryMessage) + "`r`n`r`n" + $trans
        Send-MailMessage -From $emailFrom `
                        -To $emailTo `
                        -SmtpServer $emailServer `
                        -Subject $subject" `
                        -Body $trans `
                        -Encoding ([System.Text.Encoding]::UTF8) `
                        -ErrorAction Stop
        $emailSent = $true
    } catch {
        
        Write-EventLog -LogName $eventLog -Source $eventSource -EntryType "Error" `
            -EventId 100 -Message "Failed to send email: $($_.Exception.GetType()): $_"
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.