如何将错误日志记录添加到 PowerShell 脚本
How to add error logging to PowerShell script
我是 PowerShell 脚本的新手,想在下面的脚本中添加错误日志记录。该脚本正在将 *.txt 文件发送到 FTP 服务器。
如何改进此脚本中的错误记录?我想捕获任何可能触发的潜在错误,并可能将它们通过电子邮件发送到电子邮件地址。
现在,它给我以下错误“无法找到名称为 'MyTestSession' 的变量。”这是为什么?
Import-Module PSFTP
[string] $User = 'testuser'
[string] $LocalFolder ='c:\EDI\Export\'
[string] $BackupFolder = 'C:\EDI\Backup\'
[string] $RemoteFolder ='/Inbound'
[string] $FTPServ = '00.000.00.000'
[object] $objCred = $null
[System.Security.SecureString] $Pw
$Pw= ConvertTo-SecureString -String "2}bIed_d" -AsPlainText -Force
$objCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($User, $Pw)
$GetFiles=Get-ChildItem -Filter *.txt -Path $LocalFolder
if ($null -ne $GetFiles) {
Set-FTPConnection -Credentials $objCred -Server $FTPServ -Session MyTestSession -UsePassive -UseBinary
$Session = Get-FTPConnection -Session MyTestSession
foreach ($i in $GetFiles)
{
$timestamp=(Get-Date).tostring("MMddyyyyhhmmss")
$NewFile = $i.FullName -Replace ".txt", ".$timestamp.txt"
Rename-Item -Path $i.FullName -NewName $NewFile
Add-FTPItem -Session $Session -Path $RemoteFolder -Overwrite -LocalPath $NewFile
move-Item $NewFile -Destination $BackupFolder
set-content C:\EDI\FTPScripts\Errors\Export.log $error
}
}
您的问题是寻求帮助以改进脚本中的错误记录。我将为您指明正确的方向以帮助您入门。
为了记录或报告错误,您首先需要捕获错误。这叫做error handling. The most common way to handle a PowerShell error is using the try/catch blocks. The catch block is only invoked when the error is a Terminating Error. There are two types of errors in PowerShell, Terminating and Non-Terminating。两者之间的区别,简而言之,终止错误将引发错误并导致您的脚本停止 运行ning 如果不处理而 Non-Terminating 错误将引发错误但会继续 运行.
为了处理 try/catch 块中的 Non-Terminating 错误,您必须告诉 PowerShell 将其视为终止错误。有几种方法可以做到这一点,但最流行的方法是使用 common cmdlet parameter "ErrorAction". The ErrorAction
parameter only applies to Cmdlets and therefore only works for errors thrown from Cmdlets. Another common way is to use the Preference Variable "ErrorActionPreference"(例如:$ErrorActionPreference = "Stop"
)。如果您将 ErrorActionPreference 设置为在您的脚本,设置后的所有错误都将被视为终止错误。
注意:当您调用 Set-Content
时,您正在使用 $error
。 $Error is an Automatic Variable 这是一个集合,或者更具体地说,是一个包含 PowerShell 会话中最新错误的 ArrayList。由于您在 foreach
循环中调用 Set-Content
,因此您可能会多次记录完全相同的错误。我假设这不是故意的。一个好的做法是尽可能避免在脚本中使用 $Error
,而尽可能使用 try/catch 块。
此时您应该对错误以及如何处理它们有一个体面的理解,现在您需要记录或报告它们。把你学到的关于 try/catch 块的知识应用到你的脚本中。
这只是一个示例,说明如何将 try/catch 块与 Move-Item Cmdlet 一起使用:
try {
Move-Item $NewFile -Destination $BackupFolder
}
catch [UnauthorizedAccessException] {
# All errors that are of type "UnauthorizedAccessException" are captured here.
Set-Content -Path 'C:\EDI\FTPScripts\Errors\UnauthorizedAccessExceptionExport.log' -Value $_.Exception.Message
}
catch [ItemNotFoundException] {
# All errors that are of type "ItemNotFoundException" are captured here.
Set-Content -Path 'C:\EDI\FTPScripts\Errors\ItemNotFoundExceptionExport.log' -Value $_.Exception.Message
}
catch {
# All errors that are not of type UnauthorizedAccessException or ItemNotFoundException are capture here
Set-Content -Path 'C:\EDI\FTPScripts\Errors\Export.log' -Value $_.Exception.Message
}
我是 PowerShell 脚本的新手,想在下面的脚本中添加错误日志记录。该脚本正在将 *.txt 文件发送到 FTP 服务器。
如何改进此脚本中的错误记录?我想捕获任何可能触发的潜在错误,并可能将它们通过电子邮件发送到电子邮件地址。
现在,它给我以下错误“无法找到名称为 'MyTestSession' 的变量。”这是为什么?
Import-Module PSFTP
[string] $User = 'testuser'
[string] $LocalFolder ='c:\EDI\Export\'
[string] $BackupFolder = 'C:\EDI\Backup\'
[string] $RemoteFolder ='/Inbound'
[string] $FTPServ = '00.000.00.000'
[object] $objCred = $null
[System.Security.SecureString] $Pw
$Pw= ConvertTo-SecureString -String "2}bIed_d" -AsPlainText -Force
$objCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($User, $Pw)
$GetFiles=Get-ChildItem -Filter *.txt -Path $LocalFolder
if ($null -ne $GetFiles) {
Set-FTPConnection -Credentials $objCred -Server $FTPServ -Session MyTestSession -UsePassive -UseBinary
$Session = Get-FTPConnection -Session MyTestSession
foreach ($i in $GetFiles)
{
$timestamp=(Get-Date).tostring("MMddyyyyhhmmss")
$NewFile = $i.FullName -Replace ".txt", ".$timestamp.txt"
Rename-Item -Path $i.FullName -NewName $NewFile
Add-FTPItem -Session $Session -Path $RemoteFolder -Overwrite -LocalPath $NewFile
move-Item $NewFile -Destination $BackupFolder
set-content C:\EDI\FTPScripts\Errors\Export.log $error
}
}
您的问题是寻求帮助以改进脚本中的错误记录。我将为您指明正确的方向以帮助您入门。
为了记录或报告错误,您首先需要捕获错误。这叫做error handling. The most common way to handle a PowerShell error is using the try/catch blocks. The catch block is only invoked when the error is a Terminating Error. There are two types of errors in PowerShell, Terminating and Non-Terminating。两者之间的区别,简而言之,终止错误将引发错误并导致您的脚本停止 运行ning 如果不处理而 Non-Terminating 错误将引发错误但会继续 运行.
为了处理 try/catch 块中的 Non-Terminating 错误,您必须告诉 PowerShell 将其视为终止错误。有几种方法可以做到这一点,但最流行的方法是使用 common cmdlet parameter "ErrorAction". The ErrorAction
parameter only applies to Cmdlets and therefore only works for errors thrown from Cmdlets. Another common way is to use the Preference Variable "ErrorActionPreference"(例如:$ErrorActionPreference = "Stop"
)。如果您将 ErrorActionPreference 设置为在您的脚本,设置后的所有错误都将被视为终止错误。
注意:当您调用 Set-Content
时,您正在使用 $error
。 $Error is an Automatic Variable 这是一个集合,或者更具体地说,是一个包含 PowerShell 会话中最新错误的 ArrayList。由于您在 foreach
循环中调用 Set-Content
,因此您可能会多次记录完全相同的错误。我假设这不是故意的。一个好的做法是尽可能避免在脚本中使用 $Error
,而尽可能使用 try/catch 块。
此时您应该对错误以及如何处理它们有一个体面的理解,现在您需要记录或报告它们。把你学到的关于 try/catch 块的知识应用到你的脚本中。
这只是一个示例,说明如何将 try/catch 块与 Move-Item Cmdlet 一起使用:
try {
Move-Item $NewFile -Destination $BackupFolder
}
catch [UnauthorizedAccessException] {
# All errors that are of type "UnauthorizedAccessException" are captured here.
Set-Content -Path 'C:\EDI\FTPScripts\Errors\UnauthorizedAccessExceptionExport.log' -Value $_.Exception.Message
}
catch [ItemNotFoundException] {
# All errors that are of type "ItemNotFoundException" are captured here.
Set-Content -Path 'C:\EDI\FTPScripts\Errors\ItemNotFoundExceptionExport.log' -Value $_.Exception.Message
}
catch {
# All errors that are not of type UnauthorizedAccessException or ItemNotFoundException are capture here
Set-Content -Path 'C:\EDI\FTPScripts\Errors\Export.log' -Value $_.Exception.Message
}