PowerShell脚本不会在ISE之外运行

时间:2013-10-26 02:10:33

标签: email powershell

我是PowerShell的新手,提前道歉。我从这里找到的脚本开始:Getting Event Log contents by email(非常值得信赖和感谢)并将其调整为我需要它工作的方式。基本上,当这个错误代码发生时,它每60秒发布一次,因此我只需要经常发送一封电子邮件,最近20个事件在基本表中转换为HTML。在ISE中执行脚本时,一切都很有效,但是在常规PowerShell控制台中运行时或作为计划任务时不会发送电子邮件。有一点需要注意,我可以将所有内容运行到PowerShell控制台中的电子邮件部分,并将其写入HTML文件EventID-To-HTML | Out-File -FilePath $filePath -Append。作为新人,我确信我没有完全正确处理事情,所以反馈总是受到赞赏。非常感谢

Clear-Host

# ========================
# Collection Data Section
# ========================

function EventID-To-HTML($ComputerName = $env:COMPUTERNAME) {
    $FROMTIME = (Get-Date).AddMinutes(-60)
    $Event = get-eventlog -log Application -newest 20 | where {$_.EventID -eq 7071 -AND $_.TimeGenerated -gt $FROMTIME}
    $eventstring = @()
    Foreach ($i in $Event) {
        $eventstring += $i.Message + ", " +$i.TimeGenerated
    }

    if ($eventstring -eq $NULL){exit}

    $GetEventDate = get-eventlog -log Application -newest 1 | where {$_.EventID -eq 7071 -AND $_.TimeGenerated -gt $FROMTIME}
    $EventDate = $GetEventDate.TimeGenerated

    $htmlStart = "<HTML>
                    <HEAD>
                      <style> 
                        body {background-color:rgb(238, 238, 238);}
                        body, table, td, th {font-family:Calibri; color:Black; Font-Size:11pt}
                                         th {font-weight:bold; background-color:rgb(78, 227, 48);}
                                         td {background-color:rgb(255, 190, 0);}
                      </style>
                    </HEAD>
                  <BODY><div align=center>
                  <h2><b><br><br>Security Alert: <span Style='font-style:normal; color:Blue'>**Camera(s) Down**</span></b></h2>
                  <p><b><br>This event occurred at: <span Style='font-style:italic; color:Blue'>$EventDate on $ComputerName</span></b></p>"

    $htmlEnd = ''
    $htmlStart

    $eventstring | ForEach-Object {Add-Member -InputObject $_ -Type NoteProperty -Name Camera -Value $_; $_} | ConvertTo-Html -Property Camera
    #$eventstring | %{ New-Object PsObject -Prop @{Length = $_.Length; String = $_} } | ConvertTo-HTML

    $htmlStart = ''
    $htmlStart = $htmlStart + "<br><br><br><i><span Style='color:red'>This report has been generated by software</i> <br><i>Please DO NOT reply.</i></div>"
    $htmlStart
    $htmlEnd = ''
    $htmlEnd 
}

# ======================
# Email Section
# ======================

# Make sure $eventstring has something in it first as to not send an empty message
if ($eventstring.count -ge 1) {
    $strFrom = "Sender@domain.com"
    $strTo = "recipient@domain.com"
    $strSubject = "Subject"
    $strSMTPServer = "SMPT Server name"

    $objEmailMessage = New-Object system.net.mail.mailmessage
    $objEmailMessage.From = ($strFrom)
    $objEmailMessage.To.Add($strTo)
    $objEmailMessage.Subject = $strSubject
    $objEmailMessage.IsBodyHTML = $true
    $objEmailMessage.Body = EventID-To-HTML

    $objSMTP = New-Object Net.Mail.SmtpClient($strSMTPServer)
    $objSMTP.Send($objEmailMessage)
}

1 个答案:

答案 0 :(得分:1)

我怀疑问题是$ eventstring的范围问题。它是在EventID-To-HTML功能中创建的。由于在ISE中使用作用域的方式,该函数在函数运行后仍然可用。通常,函数在其自己的作用域中运行,并且当它存在时,作用域被处理掉并且它将使用该作用域中创建的所有变量。

您可以通过点源函数对其进行测试,使其在当前范围内运行。

. EventID-To-HTML | Out-File -FilePath $filePath -Append

注意点和函数名称之间的空格。必须在那里。

避免这种情况的一种方法是使用类似数组的集合类型而不是aray。

$eventstring = new-object collections.arraylist
    Foreach ($i in $Event) {
        $eventstring.add("$($i.Message), $($i.TimeGenerated)") > $nul