• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 50
  • Last Modified:

need a script to close multiply servers from one server at different times

need a script to close multiply servers from one server at different times
0
adimar54
Asked:
adimar54
  • 5
  • 2
1 Solution
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
What do you mean by close?
0
 
adimar54Author Commented:
sorry actually meant restart
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
From a list/file?
Restart-Computer -ComputerName "Server01" -Credential (Get-Credential) -Force

Open in new window

0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
oBdACommented:
This is in test mode and will only pretend to reboot the machines. Remove the -WhatIf in line 13 to run it for real.
Just define the server name and the restart time in the $Shutdown hashtable; server order doesn't matter.
Actual reboot time might be up to 10 seconds later. Adjust the Start-Sleep in line 28 if you want a shorter test interval.
The script will just keep running until all the servers were restarted (and retry servers where the restart command failed).
$Shutdown = @{
	'ServerA' = [DateTime]'11:11'
	'ServerB' = [DateTime]'12:34'
	'ServerC' = [DateTime]'2018-02-06 01:00'
}
$DTFormat = 'HH:mm:ss'
Do {
	$Changes = $false
	ForEach ($Server In @($Shutdown.Keys)) {
		If ((Get-Date) -gt $Shutdown[$Server]) {
			Try {
				Write-Host "[$((Get-Date -Format $DTFormat))] Restarting $($Server) ..." -ForegroundColor White -NoNewline
				Restart-Computer -ComputerName $Server -Force -ErrorAction Stop -WhatIf
				Write-Host " OK" -ForegroundColor Green
				$Shutdown.Remove($Server)
				$Changes = $true
			} Catch {
				Write-Host " ERROR: $($_.Exception.Message)" -ForegroundColor Red
			}
		}
	}
	If ($Shutdown.Count -gt 0) {
		If ($Changes) {
			$Next = $Shutdown.GetEnumerator() | Sort-Object -Property Value | Select-Object -First 1
			Write-Host "[$((Get-Date -Format $DTFormat))] Still waiting for $($Shutdown.Keys -join ', ')" -ForegroundColor Yellow
			Write-Host "[$((Get-Date -Format $DTFormat))] Next restart: $($Next.Name) at $($Next.Value)" -ForegroundColor Yellow
		}
		Start-Sleep -Seconds 10
	}
} Until ($Shutdown.Count -eq 0)

Open in new window

1
 
adimar54Author Commented:
thanks will give it a test run
0
 
adimar54Author Commented:
Thanks this works great . trying to get it to run with task scheduler . think i need to add an augment will let u know
0
 
adimar54Author Commented:
thanks this is great
0
 
adimar54Author Commented:
Thanks works great .  Got it to work with task schedule
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now