Advertisement
Guest User

Powershell Script for Collection Events Logs from multiple s

a guest
Feb 26th, 2016
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. ######################################################################################################3
  2. #See http://blogs.technet.com/b/parallel_universe_-_ms_tech_blog/archive/2011/09/29/powershell-script-for-collection-events-logs-from-multiple-servers-and-generating-a-single-html-report.aspx
  3.  
  4. cls
  5.  
  6.  
  7. #This function makes sure the computer is online before attempting to collect information from it. This saves a lot of time. The function must come first because PowerShell reads top down.
  8. Function onlineCheck {
  9. param($InputObject = $null)
  10.  
  11. BEGIN {$status = $True}
  12.  
  13. PROCESS {
  14. if ($InputObject -and $_) {
  15. throw 'ParameterBinderStrings\AmbiguousParameterSet'
  16. } elseif ($InputObject -or $_) {
  17. $processObject = $(if ($InputObject) {$InputObject} else {$_})
  18.  
  19. write-host "Ping [$processObject]"
  20.  
  21. if( (Test-Connection $processObject -Quiet -count 1)) {
  22. write-host "Ping response OK" -ForegroundColor DarkGreen
  23. }
  24. else {
  25. write-host "Ping failed - host not found" -ForegroundColor red
  26. $status = $False
  27. }
  28. }
  29. else {throw 'ParameterBinderStrings\InputObjectNotBound'}
  30.  
  31. # next processObject
  32. }
  33.  
  34. # Return True if pings to all machines succeed:
  35. END {return $status}
  36. }
  37.  
  38. $inputfilepath = "D:\EventLog"
  39. $inputfilename = "servers.txt"
  40. $serverlistinput = $inputfilepath + "\" + $inputfilename
  41.  
  42. Write-host "Check Input File" -Foregroundcolor Yellow -Backgroundcolor Black
  43. $checkinputexist = test-path $serverlistinput
  44.  
  45. if ($checkinputexist -ne $True)
  46. {
  47. Write-host "Please Generate Servers.txt on the desktop. This should contain all the servers you wish to connect to" -Foregroundcolor Red -BackgroundColor Black
  48. write-host "One Entry per line" -Foregroundcolor Red -Backgroundcolor Black
  49. Exit 1
  50. }
  51. cls
  52. write-host "Input File Exists!" -Foregroundcolor Green -backgroundcolor Black
  53.  
  54. Write-Host "`nReading in server list, Please wait..." -foregroundcolor Yellow -backgroundcolor Black
  55. $serverlist = Get-Content $serverlistinput
  56. if ($serverlist.count -gt 0)
  57. {
  58. Write-Host "`nWe have read " $serverlist.count " servers from the file" -Foregroundcolor Green -backgroundcolor Black
  59. Write-Host "The following servers will be scanned `n" $serverlist -Foregroundcolor Green -backgroundcolor Black
  60. }
  61. else
  62. {
  63. cls
  64. write-host "Servers.txt is either empty or corrupt please re-create or add server names to the list" -foregroundcolor red -backgroundcolor black
  65. exit 1
  66. }
  67.  
  68. #test for results directory if does not exist create it!
  69.  
  70. $resultsdirexist = Test-Path "D:\EventLog"
  71. $resultsdirparent = "D:\"
  72. $resultsdirname = "EventLog"
  73. $testpath = $resultsdirparent + "\" + $resultsdirname
  74. $resultsdirexist = Test-Path $testpath
  75.  
  76. if ($resultsdirexist -ne "True")
  77. {
  78. Write-Host "Directory Does not exist."
  79. Write-Host "Creating...."
  80. Set-Location $resultsdirparent
  81. New-Item -path $resultsdirparent -Name EventLog -type directory
  82. Write-Host $testpath " has been created"
  83. Write-Host "This is where all output from the files will be stored"
  84. }
  85.  
  86.  
  87.  
  88. $report = $testpath + "\reports.htm"
  89. Clear-Content $report
  90.  
  91. [array]$eventlogs = $null
  92. $eventlogs += "Application"
  93. #$eventlogs += "Security"
  94. #$Eventlogs += "System"
  95. $countarr = $eventlogs.count
  96.  
  97.  
  98.  
  99. Foreach ($s in $serverlist)
  100. {
  101. $progress = "."
  102.  
  103. Add-Content $report "<html>"
  104. Add-Content $report "<head>"
  105. Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
  106. Add-Content $report '<title>Event Log Report for Server $s</title>'
  107. add-content $report '<STYLE TYPE="text/css">'
  108. add-content $report "<!--"
  109. add-content $report "td {"
  110. add-content $report "font-family: Tahoma;"
  111. add-content $report "font-size: 11px;"
  112. add-content $report "border-top: 1px solid #999999;"
  113. add-content $report "border-right: 1px solid #999999;"
  114. add-content $report "border-bottom: 1px solid #999999;"
  115. add-content $report "border-left: 1px solid #999999;"
  116. add-content $report "padding-top: 0px;"
  117. add-content $report "padding-right: 0px;"
  118. add-content $report "padding-bottom: 0px;"
  119. add-content $report "padding-left: 0px;"
  120. add-content $report "}"
  121. add-content $report "body {"
  122. add-content $report "margin-left: 5px;"
  123. add-content $report "margin-top: 5px;"
  124. add-content $report "margin-right: 0px;"
  125. add-content $report "margin-bottom: 10px;"
  126. add-content $report ""
  127. add-content $report "table {"
  128. add-content $report "border: thin solid #000000;"
  129. add-content $report "}"
  130. add-content $report "-->"
  131. add-content $report "</style>"
  132. Add-Content $report "</head>"
  133. Add-Content $report "<body>"
  134. add-content $report "<table width='100%'>"
  135. add-content $report "<tr bgcolor='#CCCCCC'>"
  136. add-content $report "<td colspan='7' height='25' align='center'>"
  137. add-content $report "<font face='tahoma' color='#003399' size='4'><strong>Event Logs Collection From Server $s</strong></font>"
  138. add-content $report "</td>"
  139. add-content $report "</tr>"
  140. add-content $report "</table>"
  141.  
  142. add-content $report "<table width='100%'>"
  143. Add-Content $report "<tr bgcolor=#CCCCCC>"
  144. Add-Content $report "<td width='5%' align='center'>Index</td>"
  145. Add-Content $report "<td width='5%' align='center'>Time</td>"
  146. Add-Content $report "<td width='5%' align='center'>EntryType</td>"
  147. Add-Content $report "<td width='5%' align='center'>Source</td>"
  148. Add-Content $report "<td width='5%' align='center'>InstanceID</td>"
  149. Add-Content $report "<td width='5%' align='center'>TimeSpan (Days)</td>"
  150. Add-Content $report "<td width='70%' align='center'>Message</td>"
  151. Add-Content $report "</tr>"
  152.  
  153. For ($count = 0; $count -lt $countarr;$count++)
  154. {
  155. if (onlineCheck "$s"){
  156. write-host "`n`nCollection Event Logs" $eventlogs[$count] "from Computer $s" -foregroundcolor yellow -backgroundcolor black
  157. $logs = get-eventlog -logname $eventlogs[$count] -computername $s -newest 100
  158. Write-host "Processing" -foregroundcolor yellow -backgroundcolor black
  159.  
  160. Foreach ($l in $logs)
  161. {
  162. write-host $progress -nonewline -Foregroundcolor Green -backgroundcolor Black
  163. $index = $l.index
  164. $time = $l.timegenerated
  165. $Entrytype = $l.entrytype
  166. $Source = $l.source
  167. $InstanceID = $l.instanceID
  168. $mytimespan = new-timespan ($l.timegenerated) (Get-Date)
  169. $TimeSpan = [math]::Round($mytimespan.TotalDays)
  170. $Message = $l.message
  171.  
  172. #if ($entrytype -eq "Error")
  173. #{
  174.  
  175. #Add-Content $report "<tr>"
  176. #Add-Content $report "<td bgcolor='#FF0000'>$index</td>"
  177. #Add-Content $report "<td bgcolor='#FF0000' align=center>$time</td>"
  178. #Add-Content $report "<td bgcolor='#FF0000' align=center>$entrytype</td>"
  179. #Add-Content $report "<td bgcolor='#FF0000' align=center>$source</td>"
  180. #Add-Content $report "<td bgcolor='#FF0000' align=center>$InstanceID</td>"
  181. #Add-Content $report "<td bgcolor='#FF0000' align=center>$Message</td>"
  182. #Add-Content $report "</tr>"
  183. #}
  184.  
  185. if ($entrytype -eq "Warning" -and $InstanceID -eq "865" -and $TimeSpan - "7")
  186. {
  187.  
  188. Add-Content $report "<tr>"
  189. Add-Content $report "<td bgcolor='#FFFFFF'>$index</td>"
  190. Add-Content $report "<td bgcolor='#FFFFFF' align=center>$time</td>"
  191. Add-Content $report "<td bgcolor='#FFFFFF' align=center>$entrytype</td>"
  192. Add-Content $report "<td bgcolor='#FFFFFF' align=center>$source</td>"
  193. Add-Content $report "<td bgcolor='#FFFFFF' align=center>$InstanceID</td>"
  194. Add-Content $report "<td bgcolor='#FFFFFF' align=center>$TimeSpan</td>"
  195. Add-Content $report "<td bgcolor='#FFFFFF' align=center>$Message</td>"
  196. Add-Content $report "</tr>"
  197. }
  198.  
  199. #if ($entrytype -eq "Information")
  200. #{
  201. #Add-Content $report "<tr>"
  202. #Add-Content $report "<td>$index</td>"
  203. #Add-Content $report "<td>$time</td>"
  204. #Add-Content $report "<td>$entrytype</td>"
  205. #Add-Content $report "<td>$source</td>"
  206. #Add-Content $report "<td>$InstanceID</td>"
  207. #Add-Content $report "<td>$Message</td>"
  208. #Add-Content $report "</tr>"
  209. #}
  210. $progess++
  211. }
  212. Add-content $report "</table>"
  213. Add-Content $report "</body>"
  214. Add-Content $report "</html>"
  215. }
  216. write-host "`n" #I put this in here because by default there was no return after the green dots.
  217. }#end psp function
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement