PowerShell - 上次Tomcat在过去24小时内启动

时间:2015-01-30 17:39:12

标签: apache tomcat powershell event-log

这是有效的..但是,它会给出以下错误消息:

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not nul
l or empty and then try the command again.
At D:\WORK\ps\Tomcat1.ps1:116 char:67
+         $TomcatEvents=Get-WmiObject Win32_NTLogEvent -ComputerName <<<<  $servern -Filter "(Message like '%Tomcat%') AND (TimeWritten
 >= '$time')" |
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

我怀疑问题出在“服务器名称”列中,并注意到最后4行:

Server Name
SERVER (1.1.1.1) 
SERVER (1.1.1.1) 
SERVER (1.1.1.1) 
SERVER (1.1.1.1) 
() 
() 
() 
() 

以下是代码:

$fileName= "D:\WORK\ps\Tomcat.htm"
$serverlist = "D:\WORK\ps\test_computers.txt"
# Lets create our variables
$time = [System.Management.ManagementDateTImeConverter]::ToDmtfDateTime((Get-Date).AddHours(-24))

New-Item -ItemType file $fileName-Force
# Function to write the HTML Header to the file

Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('MM/dd/yyyy')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>Servers_Tomcat_Restarts</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 11px;"
add-content $fileName  "border-"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-"
add-content $fileName  "padding-"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-"
add-content $fileName  "margin-"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
add-content $fileName  "<table width='100%' border='1'>"
add-content $fileName  "<tr bgcolor='#CCCCCC'>"
add-content $fileName  "<td colspan='8' height='25' align='center'>"
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>Tomcat Restart Report - $date</strong></font>"
add-content $fileName  "</td>"
add-content $fileName  "</tr>"
}

# Function to write the HTML Footer to the file
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

Function sendEmail
{param($htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "user<usera@company.com>"
$to= New-Object System.Net.Mail.MailAddress "username@company.com"
$subject="Servers Tomcat Restarts Report - $Date" 
$smtphost="SMTP-SERVER"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}


# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName "</table>"
}


# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td align='center'>Server Name</td>"
Add-Content $fileName "<td align='center'>Logfile</td>"
Add-Content $fileName "<td align='center'>Type</td>"
Add-Content $fileName "<td align='center'>User</td>"
<# Add-Content $fileName "<td align='center'>Regular Name</td>" #>
Add-Content $fileName "<td align='center'>TimeWritten (Central)</td>"
Add-Content $fileName "<td align='center'>SourceName</td>"
Add-Content $fileName "<td align='center'>Message</td>"
Add-Content $fileName "<td align='center'>EventCode</td>"
Add-Content $fileName "</tr>"
}
##########################################################################################################
### SCRIPT
remove-item $fileName
writeHtmlHeader $fileName
writeTableHeader $fileName
#writeTableFooter $fileName
#writeHtmlFooter $fileName

foreach ($computer in Get-Content $serverlist) {
    $servern=$computer.split(",")[0]
    $ip=$computer.split(",")[1]
    IF ( $Computer -notmatch 'DB' ) {
        # Lets get our stats
        # Lets create a re-usable WMI method for CPU stats

        $TomcatEvents=Get-WmiObject Win32_NTLogEvent -ComputerName $servern -Filter "(Message like '%Tomcat%') AND (TimeWritten >= '$time')" |
        select @{n="Server";e={$servern}},Logfile,Type,User,@{name='TimeWritten';Expression={$_.ConvertToDateTime($_.TimeWritten)}},SourceName,Message,EventCode | sort-object TimeWritten -desc
        foreach ($item in $TomcatEvents) {
            $Logfile = $item.Logfile
            $Type = $item.Type
            $UserName = $item.User
            $TimeWritten = $item.TimeWritten
            $SourceName = $item.SourceName
            $Message = $item.Message
            $Category = $item.Category
            $EventCode = $item.EventCode

            If ($EventCode -gt 0) {

                Add-Content $fileName "<tr>"
                Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
                Add-Content $fileName "<td align='center'> $Logfile </td>"
                Add-Content $fileName "<td align='center'> $Type </td>"
                Add-Content $fileName "<td align='center'> $UserName </td>"
                Add-Content $fileName "<td align='center'> $TimeWritten </td>"
                Add-Content $fileName "<td align='center'> $SourceName </td>"
                Add-Content $fileName "<td align='center'> $Message </td>"
                Add-Content $fileName "<td align='center'> $EventCode </td>"
                Add-Content $fileName "</tr>"
                }
        }
    }
}
                writeHtmlFooter $fileName
                $date = ( get-date ).ToString('MM/dd/yyyy')
                #System 
                $file = Get-Content $fileName
                $containsWord = $file | %{$_ -match 'System'}
                If($containsWord -contains $true)
                {
                    sendEmail $fileName
                }

1 个答案:

答案 0 :(得分:0)

在test_computers.txt

我有一条额外的空(或空白)线导致错误。