Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. Write-Output " `n Start of Hal0 `n";
  2.  
  3. #Start of Server Connection
  4. $connectionString = "Server=KINGDB01;Database=Hal0Test;Integrated Security=True;"
  5. $connection = New-Object System.Data.SqlClient.SqlConnection
  6. $connection.ConnectionString = $connectionString
  7. $connection.Open()
  8. $command = $connection.CreateCommand()
  9.  
  10. $ServerArray = [System.Collections.ArrayList]@()
  11. $query = "SELECT ServerName FROM ServerList"
  12. $command.CommandText = $query
  13. $ServerNames = $command.ExecuteReader()
  14.  
  15. $table = new-object “System.Data.DataTable”
  16. $table.Load($ServerNames)
  17.  
  18. $ServerArray = $table | select -Expand ServerName
  19.  
  20.  
  21. #Start of HTML Code
  22. $Outputreport =
  23. "<HTML><TITLE> IDI Server Report </TITLE>
  24. <BODY background-color:peachpuff>
  25. <font color =""#99000"" face=""Microsoft Tai le"">
  26. <H2> IDI Server Report </H2></font>
  27. <Table border=1 cellpadding=0 cellspacing=0>
  28. <TR bgcolor=gray align=center>
  29. <TD><B>Server Name</B></TD>
  30. <TD><B>FQDN</B></TD>
  31. <TD><B>CPU Usage</B></TD>
  32. <TD><B>RAM Usage</B></TD>
  33. <TD><B>RAM Specs.</B></TD>
  34. <TD><B>Disk Space</B></TD>
  35. <TD><B>Operating System</B></TD></TR>"
  36.  
  37. $ServerArray | ForEach-Object {
  38. # $ServerArray returns each server name
  39.  
  40. # collection code re-inserted
  41. #Operating System
  42. $SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $_
  43. $os = Get-WmiObject -Class Win32_OperatingSystem -Computer $_
  44. #Server's Memory (RAM) Usage Average
  45. $memAvg = gwmi -Class win32_operatingsystem -computername $_ | Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
  46. #Server's CPU (Proccess) Usage Average
  47. $TotalRAM = [math]::round($SystemInfo.TotalVisibleMemorySize/1MB,4)
  48. $FreeRAM = [math]::round($SystemInfo.FreePhysicalMemory/1MB,3)
  49. $UsedRAM = $TotalRAM - $FreeRAM
  50. $RAMPercentFree = [math]::round(($FreeRAM / $TotalRAM) * 100,3)
  51. $RAMPercentUsed = [math]::round(($UsedRAM / $TotalRAM) * 100,3)
  52. $cpuAVG = Get-WmiObject -computername $_ win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
  53. #Server's Hard Drives (MB) Free/Used
  54. $disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $_ |
  55. Where-Object {$_.DriveType -eq 3} |
  56. ForEach-Object {
  57. '{0} {1:D} MB Free/{2:D} MB Used' -f $_.DeviceID,
  58. [int]($_.FreeSpace/1MB), [int]($_.Size/1MB)
  59. }
  60. $command.CommandText = "UPDATE ServerList SET FQDN = '$_',
  61. TotalRAM= '$TotalRAM' ,
  62. FreeRAM= '$FreeRAM' ,
  63. UsedRAM= '$UsedRAM' ,
  64. RAMPercentFree = '$RAMPercentFree' ,
  65. RAMPercentUsed= '$RAMPercentUsed' ,
  66. OS = '$($os.Caption)' WHERE ServerName LIKE '$($os.PSComputerName)%';"
  67. $result = $command.ExecuteNonQuery()
  68.  
  69. $command.CommandText = "UPDATE ServerDrives SET
  70. DriveLetter = '$TotalRAM' ;"
  71.  
  72. if ($cpuAVG.Average -ge "20")
  73.  
  74. {
  75. $Outputreport += "<TR bgcolor=#FF0000>"
  76. }
  77. else
  78. {
  79. $Outputreport += "<TR bgcolor=#E6E6FA>"
  80. }
  81.  
  82. $Outputreport +=
  83. "<TD>$($os.PSComputerName)</TD>
  84. <TD align=center>$($_)</TD>
  85. <TD align=center>$("$($cpuAVG.Average)%")</TD>
  86. <TD align=center>$("Free: $RAMPercentFree %")`n$("Used: $RAMPercentUsed %")</TD>
  87. <TD align=center>$("Total: $TotalRAM MB")`n$("Free: $FreeRAM MB")`n$("Used: $UsedRAM MB")</TD>
  88. <TD align=center>$($disks)</TD>
  89. <TD align=center>$($os.Caption)</TD></TR>"
  90. }
  91. $Outputreport += "</Table></BODY></HTML>"
  92. $Outputreport | out-file C:UsersKINGDesktopHalOServerReport.html
  93. Invoke-Expression C:UsersKINGDesktopHalOServerReport.html
  94.  
  95. Write-Output "`n End of Hal0";
  96. #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement