简体   繁体   English

powershell-将错误写入.txt文件并一行

[英]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. 谢谢。

  1. Make use of the inbuilt Send-MailMessage cmdlet with parameter -ErrorAction Stop . 使用带有参数-ErrorAction Stop的内置Send-MailMessage cmdlet。

  2. 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.

相关问题 Powershell尝试捕获对外部txt文件的写入错误 - Powershell Try catch write errors to an external txt file 如何从 powershell 中捕获错误并将它们写入 .txt 文件? - How to catch errors from powershell and write them on a .txt file? Powershell:.txt文件中的插件行 - Powershell: addin line into the .txt file 使用PowerShell驱动YUI Compressor并将语法错误作为Visual Studios预生成脚本写入.txt文件 - Drive YUI Compressor with PowerShell and Write Syntax Errors to .txt file as a Visual Studios Pre Build Script 如何在使用 Regex 和 PowerShell 拆分大 txt 文件期间在关键字行上方包含一行 - How to include one line above of the keyword line during SPLITING a big txt file using Regex and PowerShell 从文件名写入部分,包括使用Powershell合并的txt / csv -file中的第一行 - Write parts from filename including the first line in merged txt / csv -file with powershell Powershell,多行txt文件变量 - Powershell, multi line txt file variables 带有 >> 的 powershell 写入输出到 file.txt -Append - powershell Write-Output with >> to a file.txt -Append 如何将powershell查询的输出写入txt或csv文件? - How to write the output of powershell query to txt or csv file? 如何在后台一行使用powershell编写FTP文件上传脚本 - How to write a FTP File Upload script using powershell in background in one line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM