如果脚本运行时没有与ForEach-Object
循环相关的任何错误,则会按预期生成一条消息。但是,如果出现catch
,则$body += "$intelError `r`n`r`n"
似乎会遇到某种字符限制或无声地创建错误。
最后,我得到了太多的收件人"来自office365。没有人使用分发列表知道解决这个问题的方法吗?
$clientID = "Blargh Inc"
$intelDir = "C:\Threat Intelligence"
$webclient = New-Object System.Net.WebClient
$intelSource = "$($intelDir)\sources.csv"
# Begin Email Options
$body = "Threat Intelligence Feed ran for $clientID - Errors listed below (if applicable) `r`n`r`n"
$email = @{
From = "me@mycompany.com"
To = "me@mycompany.com","notme@mycompany.com"
Subject = "$($clientID) - Threat Intelligence - Summary"
SMTPServer = "mail-server.office365.com"
Body = $body
}
# End Email Options
import-Csv $intelSource | ForEach-Object {
$storageDir = "$($intelDir)\$($_.threatType) $($_.threatSubtype)"
$threat = $_.threatType + "_" + $_.threatSubtype # Set this variable to save headaches passing it later
$storageFile = "$($storageDir)\$($threat)_$($(get-date -f MM-dd-yy)).csv" # Filename specified by sources.csv fields and today's date
$url = $_.threatLocation
# Begin Error Handling
try {
$webclient.DownloadFile($url,$storageFile)
}
catch {
$intelError = $_.Exception|format-list -force
$body += "$intelError `r`n`r`n"
echo $intelError | Out-File $intelDir\$threat"_ErrorLog_"$(get-date -f MM-dd-yy).txt -Append
Continue
}
# End Error Handling
# Cleanup
finally {
$webclient.Dispose()
}
# Throttling (mainly future use with multiple sources)
Start-Sleep 5
}
Send-MailMessage @email
$clientID = "Blargh Inc"
$intelDir = "C:\Threat Intelligence"
$webclient = New-Object System.Net.WebClient
$intelSource = "$($intelDir)\sources.csv"
$intelCount = 0
$intelError = 0
# Begin Email Options
$body = "Threat Intelligence Feed ran for $clientID - "
$subject = "$($clientID) - Threat Intelligence - "
$email = @{
From = "me@mycompany.com"
To = "me@mycompany.com","notme@mycompany.com"
Subject = $subject
SMTPServer = "mail-server.office365.com"
Body = $body
}
# End Email Options
import-Csv $intelSource -ErrorAction SilentlyContinue | ForEach-Object {
$storageDir = "$($intelDir)\$($_.threatType) $($_.threatSubtype)"
$threat = $_.threatType + "_" + $_.threatSubtype # Set this variable to save headaches passing it later
$storageFile = "$($storageDir)\$($threat)_$($(get-date -f MM-dd-yy)).csv" # Filename specified by sources.csv fields and today's date
$url = $_.threatLocation
# Begin Error Logging
try {
$intelCount++
$webclient.DownloadFile($url,$storageFile)
}
catch {
$intelError++
$intelErrorMessage = $_.Exception|format-list -force
echo $intelErrorMessage | Out-File $intelDir\$threat"_ErrorLog_"$(get-date -f MM-dd-yy).txt -Append
Continue
}
# End Error Logging
# Cleanup
finally {
$webclient.Dispose()
}
# Throttling (mainly future use with multiple sources)
Start-Sleep 5
}
if ($intelError -gt 0) {
$email.Item('Subject') += "$($intelError) Error(s)"
$email.Item('Body') += "$($intelError) of $($intelCount) sources failed. See attached error log(s)"
Get-ChildItem -Path $intelDir | Where {$_.Name -match "$($(get-date -f MM-dd-yy))"} | foreach{$_.fullname} | Send-MailMessage @email
}
else {
$email.Item('Subject') += "Success"
$email.Item('Body') += "No errors"
Send-MailMessage @email
}
如果源发生故障,则会发生此错误(因此,如果未发送电子邮件,则会出现这种情况)。
ErrorRecord : Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
StackTrace : at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] originalArguments)
at System.Management.Automation.ParserOps.CallMethod(Token token, Object target, String methodName, Object[] paramArray, Boolean callStatic, Object valueToSet)
at System.Management.Automation.MethodCallNode.InvokeMethod(Object target, Object[] arguments, Object value)
at System.Management.Automation.MethodCallNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
WasThrownFromThrowStatement : False
Message : Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
Data : {}
InnerException : System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Threat Intelligence\zeus test\zeus_test_11-12-15.csv'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromPr
oxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
--- End of inner exception stack trace ---
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at DownloadFile(Object , Object[] )
at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] originalArguments)
TargetSite : Void ExecuteStatement(System.Management.Automation.ParseTreeNode, System.Array, System.Management.Automation.Internal.Pipe, System.Collections.ArrayList ByRef, System.Management.Automation.ExecutionContext)
HelpLink :
Source : System.Management.Automation