[英]powershell - write errors to .txt file and in one line
I need to log the process in a text file and there are two problems: 1. How to write errors in a text file? 我需要将过程记录在文本文件中,有两个问题:1.如何在文本文件中写入错误? Now when the e-mails are not sent, nothing is logged.
现在,当不发送电子邮件时,将不会记录任何内容。 2. I need to write time, date and event in a single line
2.我需要用一行写时间,日期和事件
Now I have this in log.txt: 现在我在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. 谢谢。
Make use of the inbuilt Send-MailMessage
cmdlet with parameter -ErrorAction Stop
. 使用带有参数
-ErrorAction Stop
的内置Send-MailMessage
cmdlet。
Use a try-catch around this (see about_try_catch_finally to pick up and handle any errors. 使用try-catch解决此问题(请参阅about_try_catch_finally来获取并处理任何错误。
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): 在
catch
块$_
中将是错误消息(如控制台上所示),并具有Exception
属性,该属性是(.NET) Exception
(或子类)实例(我使用$_.Exception.GetType()
下面专门报告此类型,因为它是任何诊断的重要组成部分):
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()): $_"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.