Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:11:29+00:00 2026-05-30T03:11:29+00:00

I need to log the process in a text file and there are two

  • 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 = "sfrbControlMsg@primabanka.sk"
    $emailTo = "msic@primabanka.sk"
    $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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:11:30+00:00Added an answer on May 30, 2026 at 3:11 am
    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()): $_"
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write small a log analyzer application to process some log files
I need to process a text file one line at a time. In BASIC,
Hi I need to create and write the text file in to /data directory..
I need to read a text based log file to check for certain contents
I need a simple way to monitor multiple text log files distributed over a
Say, I have a collection of text files I need to process (e.g. search
I am trying to log a process in C and I need an simpler
I need to continuously read a log file to detect certain patterns. How can
How do I create a log file for a WiX Installer? I need to
I have a log file that continually logs short lines. I need to develop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.