AXISHK
asked on
Powershell to send a log file
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
Can I put the powershell in a batch file and execute it after running a program ?
Tks
ASKER
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
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
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')
ASKER
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.
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.
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.
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 $_
}
ASKER
Tks. The content of the log file can't be displayed properly. Chinese chars are displayed as "????". Any idea ?
Tks
Tks
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?
ASKER
The Chinese chars can be displayed properly in the log file. However, the content in the email shows as "????" ...
Tks
Tks
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.
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.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for AXISHK's comment #a40404671
for the following reason:
Tks
Accepted answer: 0 points for AXISHK's comment #a40404671
for the following reason:
Tks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tks
Open in new window