Solved

Powershell to send a log file

Posted on 2014-10-21
14
137 Views
Last Modified: 2014-11-12
Is it possible to use powershell to read a log file and send out the content to administator ?

Can I put the powershell in a batch file and execute it after running a program ?


Tks
0
Comment
Question by:AXISHK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
14 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40394836
Yes, you can do that. But we will need some more background info: Is the log file a simple text file? And do you mean to send it as email attachment? If both answers are yes:
@echo off
Rem you commands here
powershell -command "send-MailMessage -From me@here.com -To You@There.Com -SmtpServer mx.there.com -Subject 'Log file' -Attachments 'C:\Logfiles\ThisLogfile.txt'"

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40402174
Tks.

A log file will be generated when the program completes. I need to dump the content and send to administrator, rather than attach it as a attachment.

Afterwards, the log will be renamed to , say yyyymmdd.log and create a new one, mylog.txt.

Is it too complicated for powershell to archive this ?  Tks
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40402221
That's a perfect task for running PowerShell. I would recommend to run only PS here, even the program call.
# PowerShell-only code
push-location C:\Path\To\Your\Logfile\

# this creates mylog.txt in the folder noted above
& "C:\Path\To\Your\Application\Something.exe"

Send-MailMessage -From me@here.com -To You@There.Com -SmtpServer mx.there.com -Subject 'Log file' -BodyAsHTML -Body ((get-content mylog.txt) -join '<br>')

Rename-Item mylog.txt ((get-date -format yyyymmdd)+'.log')

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:AXISHK
ID: 40404040
Tks. Get a bit complicated for the log file.

After running the program, the name of the log file is myLog 20141025 233211.

How to modify the powershell such that
1. it will loop through all log file starting with "myLog".
2. For each loop, read the content of the log file and send it out through email.
3. Delete the log fiel.

Thanks again.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40404057
I'm not sure I understand you correctly, but I assume there is a "mylog.txt" and optionally several "mylog 2014*.txt".
The script just uses the trailing text to add it to the subject line, so you'll not loose date/time info. The current log will be posted with the current date and time.
# PowerShell-only code
push-location C:\Path\To\Your\Logfile\

# this creates mylog*.txt in the folder noted above
& "C:\Path\To\Your\Application\Something.exe"

get-childitem 'mylog*.txt' | % {
  $date = $_.BaseName -replace 'mylog'
  if ($date -eq "") { $date = get-date -format 'yyyymmdd hhMMss' }
  Send-MailMessage -From me@here.com -To You@There.Com -SmtpServer mx.there.com -Subject "Log file of $date"' -BodyAsHTML -Body ((get-content $_) -join '<br>')
  Remove-Item $_
}

Open in new window

0
 

Author Comment

by:AXISHK
ID: 40404671
Tks. The content of the log file can't be displayed properly. Chinese chars are displayed as "????". Any idea ?


Tks
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40404680
You should be more precise. Is the log file in Chinese, and the email can't display that, or is the log file standard text and displaying in strange languages?
0
 

Author Comment

by:AXISHK
ID: 40404808
The Chinese chars can be displayed properly in the log file. However, the content in the email shows as "????" ...

Tks
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40405500
I didn't expect to see any text file requiring Unicode encoding. In future, whenever you ask a question here involving to treat text files, emphasize the need to keep Chinese characters.

I'm not experienced with HTML code and Chinese characters, I guess we need to add Encoding tags in the generated HTML body for being able to properly display them. I'll add more Topic Areas to reach Experts more versed in that.
0
 

Author Comment

by:AXISHK
ID: 40436998
I've requested that this question be closed as follows:

Accepted answer: 0 points for AXISHK's comment #a40404671

for the following reason:

Tks
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40437488
This works for me with a mock-up Simplified Chinese logfile:
# PowerShell-only code
push-location C:\Path\To\Your\Logfile\

# this creates mylog*.txt in the folder noted above
& "C:\Path\To\Your\Application\Something.exe"

get-childitem 'mylog*.txt' | % {
  $date = $_.BaseName -replace 'mylog'
  if ($date -eq "") { $date = get-date -format 'yyyymmdd hhMMss' }
  Send-MailMessage -From me@here.com -To You@There.Com -SmtpServer mx.there.com -Subject "Log file of $date"' -BodyAsHTML -Body ((get-content $_) -join '<br>') -Encoding ([System.Text.Encoding]::Unicode)
  Remove-Item $_
}

Open in new window

You might have to change Unicode to UTF8 or UTF32, depending on the log file encoding.
0
 

Author Closing Comment

by:AXISHK
ID: 40439041
Tks
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question