Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Restart-RemoteComputer {
  2.     Param (
  3.         [Parameter(Mandatory=$true)]
  4.         $computerName
  5.     )
  6.     $timeOut = 300
  7.     $timeInSeconds = 0
  8.     try {
  9.         Restart-Computer -ComputerName $computerName -Force
  10.     }
  11.     catch {
  12.         Write-Host $_
  13.         return $false
  14.     }
  15.     #we just want to wait for 10 seconds before monitoring the system
  16.     Write-Host "`nWaiting while the remote computer restarts"
  17.     Start-Sleep 10 
  18.     #start the test-connection loop here
  19.     While (-not (Test-Connection -ComputerName $computerName -Quiet -Count 1)) {
  20.         #We need to time this out after a while. Otherwise, we will be looking for it forever
  21.         if ($timeInSeconds -eq $timeOut) {
  22.         Write-Error "Remote computer did not come back within the timeout period"
  23.         return $false
  24.         } else {       
  25.             $timeInSeconds += 1
  26.         }
  27.     }
  28.     return $true
  29. }
  30.  
  31. [datetime] $DatetoCheck = (Get-Date).AddHours(-1 * $hourThreshold)
  32. [int]$attempts = 0
  33. [int]$maxattempts = 15
  34. [int]$sleepInterval = 25
  35. $rebootOccurred = $false;
  36. $server = Read-Host -Prompt "Specify a server"
  37.  
  38. Write-Host "Rebooting Server $server"
  39.  
  40. if (Restart-RemoteComputer -ComputerName $server) {
  41.     try{
  42.        $query = "Select * from Win32_Service WHERE
  43.            (
  44.                (Name LIKE 'MSSQL$%') OR (Name='MSSQLSERVER') OR (Name LIKE 'SQLAGENT%')
  45.            ) AND (
  46.                (StartMode='Auto' AND State='Running')
  47.            )"
  48.         $SQLServices = Get-WmiObject -Query $Query
  49.         If ($SQLServices) {
  50.            #the above WMI Query will get all instances of running SQLServer services in Auto Startmode.
  51.            #So, there is no need for waiting in a loop again
  52.            #We can simply the output SQL Services Object
  53.            return $SQLServices    
  54.         } else {
  55.             Write-Host "No SQL Server services found"
  56.             return
  57.         }
  58.        
  59.     }
  60.     catch{
  61.        Write-Host "Error while getting service info from $($server)"
  62.        Write-host $_
  63.        return
  64.     }
  65. } else {
  66.     Write-Host "Server Did not restart properly in the timeout period"
  67.     return
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement