Advertisement
surepointit

Server Daily Job

Feb 13th, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module $env:SyncroModule -WarningAction SilentlyContinue
  2.  
  3. $Date = Get-Date -Format "MM/dd/yyyy HH:mm:ss"  
  4. Write-Host This script ran on $Date
  5. Add-Content c:\SPI\ScriptHistory.txt "Daily Server Maintenance Script Ran on $Date"
  6.  
  7. ################################################################################################################################
  8. ################################################################################################################################
  9. #Check if server or workstation
  10. $OSType = (Get-ComputerInfo).OsProductType
  11. if ($OSType -eq 'Workstation') {
  12.     Write-Host System is a $OSType. Verify script should be running here.
  13.     Rmm-Alert -Category "Incorrect Script" -Body "Verify script should be running here."
  14.     exit 1
  15. }
  16. #Notify us what OS Type it is
  17. Write-Host System is a $OSType
  18.  
  19. ################################################################################################################################
  20. ################################################################################################################################
  21. #Purpose: Maintenance script to keep pc's running fresh
  22. #This script performs the following:
  23.  
  24. #Show Uptime Hours and Days
  25. #Pull CPU Temp
  26. #Check Dell RAID Virtual Disks
  27. #Check Dell RAID Physical Disks
  28.  
  29.  
  30. ################################################################################################################################
  31. ################################################################################################################################
  32. #Show Uptime Hours and Days
  33.  
  34. $os=Get-WmiObject win32_operatingsystem  
  35. $UptimeHours = ((get-date) - ($os.ConvertToDateTime($os.lastbootuptime))).Hours  
  36.  
  37. Write-Host "System Uptime is: $UptimeHours Hour(s)"  
  38.  
  39. Set-Asset-Field -Subdomain "surepointit" -Name "System Uptime Hours" -Value $UptimeHours
  40.  
  41. $MaxDaysUptime = "30"
  42. $Boot = Get-WmiObject Win32_OperatingSystem
  43. $LastRebootTime = $Boot.ConvertToDateTime($Boot.LastBootUpTime)
  44. $Today = Get-Date
  45. $DiffDays = $Today.Date - $LastRebootTime
  46. $UptimeDays = "$($DiffDays.Days) days since last reboot"
  47. $UptimeDays
  48.  
  49. Write-Host "Days since boot: $($DiffDays.TotalDays)"
  50.  
  51. Set-Asset-Field -Subdomain "surepointit" -Name "System Uptime Days" -Value $DiffDays.Days
  52.  
  53. if($DiffDays.TotalDays -gt $MaxDaysUptime) {
  54. Rmm-Alert -Category "Monitor - Uptime" -Body "$UptimeDays"
  55. exit
  56. }
  57. Close-Rmm-Alert -Category "Monitor - Uptime"
  58.  
  59.  
  60. ################################################################################################################################
  61. ################################################################################################################################
  62. #Pull CPU Temp
  63.  
  64. #Notify if this is a vm
  65. $Computer = Get-CimInstance -ClassName 'Win32_ComputerSystem'
  66. if ($Computer.Model -like 'Virtual*') {
  67.     Write-Host "Server is a VM"
  68.     Set-Asset-Field -Name "CPU Temp" -Value "VM"
  69. }  
  70. else {
  71.     Write-Host "Server is not a VM, continuing."
  72. }
  73.  
  74.  
  75.  
  76. $warningTemp = 180
  77. $alarmTemp = 185
  78.  
  79.  
  80. $probeInstalled = Test-Path -Path "C:\temp\temp-probe"
  81. If ($probeInstalled){
  82.     Write-Host "Installed - running the probe"    
  83.     Start-Process  -FilePath "C:\temp\temp-probe\TempProber.exe" -NoNewWindow -Wait -RedirectStandardOutput C:\temp\temp-probe\temperature.txt  -RedirectStandardError C:\temp\temp-probe\temperature_error.txt
  84. } ELSE {
  85.     Write-Host "Unzipping first.."
  86.     Expand-Archive C:\temp\temp-probe.zip -DestinationPath C:\temp\temp-probe
  87.     Write-Host "running the probe"
  88.     Start-Process  -FilePath "C:\temp\temp-probe\TempProber.exe" -NoNewWindow -Wait -RedirectStandardOutput C:\temp\temp-probe\temperature.txt  -RedirectStandardError C:\temp\temp-probe\temperature_error.txt
  89. }
  90.  
  91. $result = gc C:\temp\temp-probe\temperature.txt
  92. $cpuLine = $result -match "CPU"
  93.  
  94. IF ($cpuLine) {
  95.     $temperature = $cpuline | select-string -pattern '\d+' -allmatches  | % { $_.Matches } | % { $_.Value }
  96.    
  97. Set-Asset-Field -Name "CPU Temp" -Value $temperature
  98.    
  99.     IF ([int]$temperature -gt $alarmTemp){
  100.         write-host "Host is HOT!"  
  101.         Rmm-Alert -Category 'cpu_temp_alarm' -Body "CPU is over Alarm Temp with a value of $temperature. The Alarm temp is set to $alarmTemp"
  102.         exit
  103.     }
  104.     ELSEIF ([int]$temperature -gt $warningTemp){
  105.         write-host "Host is warming up..."    
  106.     }
  107.     ELSE {
  108.         write-host "Just chilling at $temperature currently..."
  109.         Close-Rmm-Alert -Category "'cpu_temp_alarm'"
  110.     }
  111.    
  112. } ELSE {
  113.     write-host "Could not find a CPU result from openhardwaremonitor"
  114. }
  115.  
  116. #Clean up our temp file on the way out!
  117. Remove-Item "C:\temp\temp-probe\temperature.txt"
  118.  
  119. ################################################################################################################################
  120. ################################################################################################################################
  121. #Check Dell RAID Virtual Disks
  122.  
  123. # Check to see if this server is a physical host
  124. if ((Get-WmiObject -Namespace "root\cimv2" -Class Win32_ComputerSystem).Model -like '*Virtual*') {
  125.     Write-Host "Unsupported Server. This can only run on a physical host."
  126.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  127.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  128.     Write-Host Script completed on $Date
  129.     exit 0
  130. }
  131.  
  132. # Check to see if this server is a Dell.
  133. if ((Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer -notlike 'Dell*') {
  134.     Write-Host "System Manufacturer is not Dell, exiting..."
  135.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  136.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  137.     Write-Host Script completed on $Date
  138.     exit 0
  139. }
  140.  
  141. # Check to see if this server is a PowerEdge.
  142. if ((Get-CimInstance -ClassName Win32_ComputerSystem).Model -notlike 'PowerEdge*') {
  143.     Write-Host "System Manufacturer is not a PowerEdge, exiting..."
  144.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  145.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  146.     Write-Host Script completed on $Date
  147.     exit 0
  148. }
  149.  
  150. $version = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").CurrentVersion
  151. if ($Version -lt "6.3") {
  152.     Write-Host "Unsupported OS. This component requires at least Server 2012R2"
  153.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  154.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  155.     Write-Host Script completed on $Date
  156.     exit 0
  157. }
  158. try {
  159.     omconfig preferences cdvformat delimiter=comma
  160.     $OmReport = omreport storage vdisk -fmt cdv |  select-string -SimpleMatch "ID,Status," -Context 0, 5000
  161. }
  162. catch {
  163.     throw "Error: omreport Command has Failed: $($_.Exception.Message). Check if Dell OpenManage is installed and OMReport is in the PATH variable."
  164. }
  165. $VDarray = convertfrom-csv $OmReport -Delimiter ","
  166.  
  167. foreach ($VirtualDisk in $VDarray | where-object { $_.'> ID' -in 0..1000 }) {
  168.     if ($($virtualdisk.State) -eq "Ready" -or $($virtualdisk.Status) -eq "Ok") {
  169.     }
  170.     else {
  171.         write-host "$($VirtualDisk.Name) / $($VirtualDisk.'Device Name') Has Status $($VirtualDisk.Status) / $($VirtualDisk.State)"
  172.         $RAIDStatus = "failed"
  173.     }
  174. }
  175.  
  176. if ($RAIDStatus) {
  177.     Rmm-Alert -Category 'RAID' -Body 'Potential RAID failure. Please investigate'
  178.     if($CreateTicket -eq "Yes"){
  179.     Create-Syncro-Ticket -Subject "Potential RAID failure" -IssueType "RAID" -Status "New"
  180. }
  181. }
  182.  
  183. ################################################################################################################################
  184. ################################################################################################################################
  185. #Check Dell RAID Physical Disk
  186.  
  187. # Check to see if this server is a physical host
  188. if ((Get-WmiObject -Namespace "root\cimv2" -Class Win32_ComputerSystem).Model -like '*Virtual*') {
  189.     Write-Host "Unsupported Server. This can only run on a physical host."
  190.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  191.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  192.     Write-Host Script completed on $Date
  193.     exit 0
  194. }
  195.  
  196. # Check to see if this server is a Dell.
  197. if ((Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer -notlike 'Dell*') {
  198.     Write-Host "System Manufacturer is not Dell, exiting..."
  199.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  200.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  201.     Write-Host Script completed on $Date
  202.     exit 0
  203. }
  204.  
  205. # Check to see if this server is a PowerEdge.
  206. if ((Get-CimInstance -ClassName Win32_ComputerSystem).Model -notlike 'PowerEdge*') {
  207.     Write-Host "System Manufacturer is not a PowerEdge, exiting..."
  208.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  209.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  210.     Write-Host Script completed on $Date
  211.     exit 0
  212. }
  213.  
  214.  
  215. $version = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").CurrentVersion
  216. if ($Version -lt "6.3") {
  217.     Throw "Unsupported OS. This component requires at least Server 2012R2"
  218.     Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  219.     Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  220.     Write-Host Script completed on $Date
  221. }
  222. try {
  223.     omconfig preferences cdvformat delimiter=pipe
  224.     [xml]$ControllerList = (omreport storage controller -fmt xml)
  225.     $OmReport = foreach ($Controller in $ControllerList.oma.controllers.DCStorageObject.GlobalNo.'#text') {
  226.         omreport storage pdisk controller=$Controller -fmt cdv | select-string -SimpleMatch "ID|Status" -Context 0, 5000
  227.     }
  228. }
  229. catch {
  230.     throw "Error: omreport Command has Failed: $($_.Exception.Message). Check if Dell OpenManage is installed and OMReport is in the PATH variable."
  231. }
  232. $Parray = convertfrom-csv $OmReport -Delimiter '|'
  233. $StatusList = @("Online", "OK", "Ready")
  234. $PDResults = foreach ($PhysicalDisk in $Parray | where-object { $_.status -ne 'Status' }) {
  235.     [PSCustomObject]@{
  236.         DiskName            = $PhysicalDisk.name
  237.         SerialNumber        = $($PhysicalDisk.'Serial No.')
  238.         Status              = $($PhysicalDisk.Status)
  239.         State               = $($PhysicalDisk.State)
  240.         'Part Number'       = $($physicaldisk.'Part Number')
  241.         'Failure Predicted' = $PhysicalDisk.'Failure Predicted'
  242.     }
  243. }
  244.  
  245. $PDResultsFiltered = $PDResults | Where-Object { $_.state -notin $StatusList -or $_.status -notin $StatusList -or $_.'Failure Predicted' -ne 'No' }
  246.  
  247. if (!$PDResultsFiltered) {
  248.     write-host "Healthy"
  249. }
  250. else {
  251.     Rmm-Alert -Category 'Disks' -Body 'Potential physical disk failure. Please investigate'
  252. if($CreateTicket -eq "Yes"){
  253.     Create-Syncro-Ticket -Subject "Potential disk failure" -IssueType "Disk" -Status "New"
  254.  
  255. }
  256. }
  257.  
  258.  
  259. ################################################################################################################################
  260. ################################################################################################################################
  261.  
  262. Set-Asset-Field -Name "Daily Last Ran" -Value $Date
  263. Log-Activity -Message "Daily Maintenance Script Ran" -EventName "Maintenance Script"
  264. $CompletionDate = Get-Date -Format "MM/dd/yyyy HH:mm:ss"  
  265. Write-Host Script completed on $CompletionDate
  266.  
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement