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
adimar54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Webinar: Miercom Evaluates Wi-Fi Security

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom how WatchGuard's Wi-Fi security stacks up against the competition in our upcoming webinar!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.