Powershell 未捕获抄本中的错误

Powershell 未捕获抄本中的错误,powershell,powershell-2.0,error-log,Powershell,Powershell 2.0,Error Log,我故意在下面写了一个错误: Try { Start-Transcript -Path $Errorlog #$TermRep = Import-Csv $TermReport #$Donna = Import-Csv $HRReport $TermRep = Import-Csv $Path\TestFileTerm2.csv $Donna = Import-Csv $Path\TestFileDonna.csv #Job to match users between CSVs $Job

我故意在下面写了一个错误:

Try
{
Start-Transcript -Path $Errorlog

#$TermRep = Import-Csv $TermReport
#$Donna = Import-Csv $HRReport

$TermRep = Import-Csv $Path\TestFileTerm2.csv
$Donna = Import-Csv $Path\TestFileDonna.csv

#Job to match users between CSVs
$Job = ForEach($i in $TermRep){
$TID = $($i.'Person ID')
ForEach($u in $Donna){
$DID = $($u.UserID)
If($TID -eq $DID){
"Move-ADObject -Identity $TID -TargetPath 'PATH' `r`n"
            }
    }
}
Stop-Transcript
#Send email of results or statement of no results
$smtpServer = "blah.net"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
#$msg.To.Add($EmailTo1)
#$msg.To.Add($EmailTo2)
$msg.Priority = "High"
If($Job -notlike ""){
$msg.Subject = "Open a ticket for Terminated Exception Users for Lit-hold changes."
$msg.Body = "Fix these users"
}
Else{
$msg.Subject = "Terminated Exception Users Script was run for Lit-holds"
$msg.Body = "No users need fixing."
}
$smtp.Send($msg)
}
Catch [system.exception]
{
#If the script errors (e.g. it cannot find one of the lists) send the following email.
$smtpServer = "blah.net"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
#$msg.To.Add($EmailTo1)
#$msg.To.Add($EmailTo2)
$msg.Priority = "High"
$msg.Subject = "Open a ticket for Terminated Exception Users - Script Failure"
$msg.Body = "Script error."
$smtp.Send($msg)
}
**********************
Windows PowerShell Transcript Start
Start time: 20151223165030
Username  : DOMAIN\Username 
Machine   : WKSTNName (Microsoft Windows NT 6.1.7601 Service Pack 1) 
**********************
Transcript started, output file is D:\DIR\LogFile2015-12-23.log
**********************
Windows PowerShell Transcript End
End time: 20151223165030
**********************
Start-Transcript log.txt
dotnet.exe asdf 2>&1
Stop-Transcript
具体而言,
$TermRep=Import Csv$Path\TestFileTerm2.Csv
不存在。然而,我的发言记录如下:

Try
{
Start-Transcript -Path $Errorlog

#$TermRep = Import-Csv $TermReport
#$Donna = Import-Csv $HRReport

$TermRep = Import-Csv $Path\TestFileTerm2.csv
$Donna = Import-Csv $Path\TestFileDonna.csv

#Job to match users between CSVs
$Job = ForEach($i in $TermRep){
$TID = $($i.'Person ID')
ForEach($u in $Donna){
$DID = $($u.UserID)
If($TID -eq $DID){
"Move-ADObject -Identity $TID -TargetPath 'PATH' `r`n"
            }
    }
}
Stop-Transcript
#Send email of results or statement of no results
$smtpServer = "blah.net"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
#$msg.To.Add($EmailTo1)
#$msg.To.Add($EmailTo2)
$msg.Priority = "High"
If($Job -notlike ""){
$msg.Subject = "Open a ticket for Terminated Exception Users for Lit-hold changes."
$msg.Body = "Fix these users"
}
Else{
$msg.Subject = "Terminated Exception Users Script was run for Lit-holds"
$msg.Body = "No users need fixing."
}
$smtp.Send($msg)
}
Catch [system.exception]
{
#If the script errors (e.g. it cannot find one of the lists) send the following email.
$smtpServer = "blah.net"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
#$msg.To.Add($EmailTo1)
#$msg.To.Add($EmailTo2)
$msg.Priority = "High"
$msg.Subject = "Open a ticket for Terminated Exception Users - Script Failure"
$msg.Body = "Script error."
$smtp.Send($msg)
}
**********************
Windows PowerShell Transcript Start
Start time: 20151223165030
Username  : DOMAIN\Username 
Machine   : WKSTNName (Microsoft Windows NT 6.1.7601 Service Pack 1) 
**********************
Transcript started, output file is D:\DIR\LogFile2015-12-23.log
**********************
Windows PowerShell Transcript End
End time: 20151223165030
**********************
Start-Transcript log.txt
dotnet.exe asdf 2>&1
Stop-Transcript
谁能告诉我我错过了什么?我不明白
Start Transcript
是如何工作的吗

如果有人能帮我,我要做的就是把错误日志归档

更新的尝试 我尝试将其更改为:

Try{$TermRep = Import-Csv $Path\TestFileTerm.csv -verbose -EA stop}
Catch { Write "$DateTime Error: Import-Csv: $_" >>$Errorlog}
如果我将每个命令设置为
try catch
,它会将错误打印到日志文件中,但即使在我尝试导入不存在的CSV时,它也不再运行错误电子邮件功能(第二封电子邮件)。相反,它在第一封电子邮件上运行
Else
,这意味着找不到任何用户

此外,Matt的建议在尝试时将错误打印到日志文件中,但会导致它始终发送错误电子邮件(第二封电子邮件),即使没有错误:

try{
    Start-Transcript -Path c:\temp\text.txt
    $TermRep = Import-Csv $Path\TestFileTerm2.csv
} catch {
    $Error[0]
}

你遗漏的重要部分很可能包含你的答案。您可以使用try块捕获错误。如果您没有在catch块内输出,那么您将看到显示的输出

try{
    Start-Transcript -Path c:\temp\text.txt
    $TermRep = Import-Csv $Path\TestFileTerm2.csv
} catch {
}

Stop-Transcript
上面不会显示错误记录或否。如果您要在catch块中输出错误(类似于您在comments中所做的),那么您将看到错误。如果你做过类似的事情,你就不会表现出来

try{
    Start-Transcript -Path c:\temp\text.txt
    $TermRep = Import-Csv $Path\TestFileTerm2.csv
} catch {
    $Error[0]
}

Stop-Transcript
请记住,这只会显示最后一个错误。不确定要如何播放多个错误。try-catch旨在捕获一个您希望失败的对象,而不是几次


我的
$ErrorActionPreference
已设置为继续执行

PS C:\temp> $ErrorActionPreference
Continue

我在调用外部应用程序(如dotnet.exe)时也看到了这一点,控制台中会显示.NET异常,但不会记录在转录本文件中

我找到的修复方法是在调用外部命令时重定向错误流,如下所示:

Try
{
Start-Transcript -Path $Errorlog

#$TermRep = Import-Csv $TermReport
#$Donna = Import-Csv $HRReport

$TermRep = Import-Csv $Path\TestFileTerm2.csv
$Donna = Import-Csv $Path\TestFileDonna.csv

#Job to match users between CSVs
$Job = ForEach($i in $TermRep){
$TID = $($i.'Person ID')
ForEach($u in $Donna){
$DID = $($u.UserID)
If($TID -eq $DID){
"Move-ADObject -Identity $TID -TargetPath 'PATH' `r`n"
            }
    }
}
Stop-Transcript
#Send email of results or statement of no results
$smtpServer = "blah.net"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
#$msg.To.Add($EmailTo1)
#$msg.To.Add($EmailTo2)
$msg.Priority = "High"
If($Job -notlike ""){
$msg.Subject = "Open a ticket for Terminated Exception Users for Lit-hold changes."
$msg.Body = "Fix these users"
}
Else{
$msg.Subject = "Terminated Exception Users Script was run for Lit-holds"
$msg.Body = "No users need fixing."
}
$smtp.Send($msg)
}
Catch [system.exception]
{
#If the script errors (e.g. it cannot find one of the lists) send the following email.
$smtpServer = "blah.net"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
#$msg.To.Add($EmailTo1)
#$msg.To.Add($EmailTo2)
$msg.Priority = "High"
$msg.Subject = "Open a ticket for Terminated Exception Users - Script Failure"
$msg.Body = "Script error."
$smtp.Send($msg)
}
**********************
Windows PowerShell Transcript Start
Start time: 20151223165030
Username  : DOMAIN\Username 
Machine   : WKSTNName (Microsoft Windows NT 6.1.7601 Service Pack 1) 
**********************
Transcript started, output file is D:\DIR\LogFile2015-12-23.log
**********************
Windows PowerShell Transcript End
End time: 20151223165030
**********************
Start-Transcript log.txt
dotnet.exe asdf 2>&1
Stop-Transcript

通过重定向,来自dotnet.exe的错误将包含在成绩单中-如果没有重定向,“asdf是未知命令”错误只会显示在控制台中。

您希望看到哪些您没有看到的内容?@dfundako我希望看到类似的错误消息,告诉我它无法导入csv,因为该文件不存在。
$ErrorActionPreference
的状态是什么?@Eris的可能重复我不认为这是重复的,接受的解决方案是指向未找到的页面的链接,并且问题中的输出比我得到的多。我添加了我的整个代码sans
$msg.body
。如果我尝试捕获任何特定错误并输出到文件,它不会发送错误电子邮件。它发送的电子邮件表示无需处理—第一封电子邮件的
Else
版本,而不是第二封脚本电子邮件。我想这是有道理的,因为它取代了错误?这就是为什么我最初只是在寻找一种方法来编写它为那个小部分所做的一切,不管它是失败还是成功。