Search code examples
powershellhealth-monitoring

Output not coming on HTM,


I run the below scripts and Eventlog is blank on HTM File but on powershell

$ServerListFile = "D:\Scripts\ServerList.txt"   
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue  
$Result = @() 
ForEach($computername in $ServerList)  
 {
 Get-Eventlog -LogName Security -Newest 2000 | Where-Object {$_.EventID -eq     "4624"} | Select-Object @{Name ="Username"; Expression = {$_.ReplacementStrings[1]}} 
   $result += [PSCustomObject] @{  
    ServerName = "$computername" 
    EventLog = "$Username"
     }  
 $Outputreport = "<HTML><TITLE> Decommission Validation Report </TITLE> 
                 <BODY background-color:peachpuff> 
                 <font color =""#99000"" face=""Microsoft Tai le""> 
                 <H2> Decommission Validation Report </H2></font> 
                 <Table border=1 cellpadding=0 cellspacing=0> 
                 <TR bgcolor=gray align=center> 
                   <TD><B>Server Name</B></TD> 
                   <TD><B>EventLog</B></TD></TR>"
      Foreach($Entry in $Result)  
 {  
      if((($Entry.Servername) -or ($Entry.EventLog)) -ge 80 )  
      {  
        $Outputreport += "<TR bgcolor=red>"  
      }  
      else 
       { 
        $Outputreport += "<TR>"  
      } 
      $Outputreport += "<TD>$($Entry.Servername)</TD></TD><TD align=center>$($Entry.Username)</TD></TR>"  
    } 
 $Outputreport += "</Table></BODY></HTML>"  
    }
 $Outputreport | out-file D:\Scripts\Test.htm  
 Invoke-Expression D:\Scripts\Test.htm

I run the above scripts and Eventlog is blank on HTM File but on powershell


Solution

  • I incorporated the changes suggested by Dane Boulton and modified your code a bit to get what I believe is what you are looking for. I changed the Replacement strings entry to 5 because I believe you are looking for the user account logging in. I also modified the EventLog variable to reference $item.UserName. See how this works for you.

    $ServerListFile = "D:\Scripts\ServerList.txt"   
    $ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue  
    $Result = @() 
    ForEach($computername in $ServerList)  
      {
      $eventLog = Get-Eventlog -LogName Security -Newest 2000 | Where-Object {$_.EventID -eq "4624"} | Select-Object @{Name ="Username"; Expression = {$_.ReplacementStrings[5]}} 
    foreach($item in $eventLog)
      {
        $result += [PSCustomObject] @{  
        ServerName = $computername
        UserName = $item.Username
        }  
      }
         $Outputreport = "<HTML><TITLE> Decommission Validation Report </TITLE> 
                   <BODY background-color:peachpuff> 
                         <font color =""#99000"" face=""Microsoft Tai le""> 
                         <H2> Decommission Validation Report </H2></font> 
                         <Table border=1 cellpadding=0 cellspacing=0> 
                         <TR bgcolor=gray align=center> 
                           <TD><B>Server Name</B></TD> 
                           <TD><B>UserName</B></TD></TR>"
              Foreach($Entry in $Result)  
         {  
              if((($Entry.Servername) -or ($Entry.UserName)) -ge 80 )  
              {  
                $Outputreport += "<TR bgcolor=red>"  
              }  
              else 
               { 
                $Outputreport += "<TR>"  
              } 
              $Outputreport += "<TD>$($Entry.Servername)</TD></TD><TD align=center>$($Entry.UserName)</TD></TR>"  
            } 
         $Outputreport += "</Table></BODY></HTML>"  
            }
         $Outputreport | out-file D:\Scripts\Test.htm  
         Invoke-Expression D:\Scripts\Test.htm