Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Restart-RemoteComputer {
- Param (
- [Parameter(Mandatory=$true)]
- $computerName
- )
- $timeOut = 300
- $timeInSeconds = 0
- try {
- Restart-Computer -ComputerName $computerName -Force
- }
- catch {
- Write-Host $_
- return $false
- }
- #we just want to wait for 10 seconds before monitoring the system
- Write-Host "`nWaiting while the remote computer restarts"
- Start-Sleep 10
- #start the test-connection loop here
- While (-not (Test-Connection -ComputerName $computerName -Quiet -Count 1)) {
- #We need to time this out after a while. Otherwise, we will be looking for it forever
- if ($timeInSeconds -eq $timeOut) {
- Write-Error "Remote computer did not come back within the timeout period"
- return $false
- } else {
- $timeInSeconds += 1
- }
- }
- return $true
- }
- [datetime] $DatetoCheck = (Get-Date).AddHours(-1 * $hourThreshold)
- [int]$attempts = 0
- [int]$maxattempts = 15
- [int]$sleepInterval = 25
- $rebootOccurred = $false;
- $server = Read-Host -Prompt "Specify a server"
- Write-Host "Rebooting Server $server"
- if (Restart-RemoteComputer -ComputerName $server) {
- try{
- $query = "Select * from Win32_Service WHERE
- (
- (Name LIKE 'MSSQL$%') OR (Name='MSSQLSERVER') OR (Name LIKE 'SQLAGENT%')
- ) AND (
- (StartMode='Auto' AND State='Running')
- )"
- $SQLServices = Get-WmiObject -Query $Query
- If ($SQLServices) {
- #the above WMI Query will get all instances of running SQLServer services in Auto Startmode.
- #So, there is no need for waiting in a loop again
- #We can simply the output SQL Services Object
- return $SQLServices
- } else {
- Write-Host "No SQL Server services found"
- return
- }
- }
- catch{
- Write-Host "Error while getting service info from $($server)"
- Write-host $_
- return
- }
- } else {
- Write-Host "Server Did not restart properly in the timeout period"
- return
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement