我想编写一个脚本来检查服务器上的4个服务,如果它们按顺序停止它们的状态并且另一方面有延迟启动它们。
第一项服务必须检查它是否正在运行,然后从其他服务开始,因此我们使用延迟。
$serverlist = Get-Content “.\server.txt”
$servicename4 = "Orbit Utveckling Consul"
$servicename3 = "Orbit Utveckling Identity"
$servicename2 = "Orbit Utveckling Service"
$servicename1 = "Orbit Utveckling Integration"
$delay = 60 # Ange i sekunder.
foreach ($server in $serverlist) {
$getservicestatus1 = Get-Service -Name $servicename1 -ComputerName $server
if ($getservicestatus1 -eq "Running") {
Set-Service -Name $servicename1 -ComputerName $server -Status stopped
Write-Host "$server $servicename1 Stoppat!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan $servicename2 stoppar..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
$getservicestatus2 = (Get-Service -Name $servicename2 -ComputerName $server).status
if ($getservicestatus2 -eq "Running") {
Set-Service -Name $servicename2 -ComputerName $server -Status stopped
Write-Host "$server $servicename2 Stoppat!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server stoppar..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
$getservicestatus3 = (Get-Service -Name $servicename3 -ComputerName $server).status
if ($getservicestatus3 -eq "Running") {
Set-Service -Name $servicename3 -ComputerName $server -Status stopped
Write-Host "$server $servicename3 Stoppat!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server stoppar..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
$getservicestatus4 = (Get-Service -Name $servicename4 -ComputerName $server).status
if ($getservicestatus4 -eq "Running") {
Set-Service -Name $servicename4 -ComputerName $server -Status stopped
Write-Host "$server $servicename4 Stoppat!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server stoppar..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
}
# Filen startar tjänsten Drift_Cosmic på varje IX3a i txt-filen.
# Det görs med en delay på ett antal sekunder mellan varje start.
# Detta är en testfil tills dess att en riktig testkörning
# har gjorts i Drift.
Write-Host "$servicename4, $servicename3, $servicename2, $servicename1 startas med $delay sekunders mellanrum."
foreach ($server in $serverlist) {
# Skriv ut status på tjänsten i passande färg.
$getservicestatus4 = (Get-Service -Name $servicename4 -ComputerName $server).status
if ($getservicestatus4 -eq "Stopped") {
Set-Service -Name $servicename4 -ComputerName $server -Status Running
Write-Host "$server $servicename4 Startad!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server startas..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
$getservicestatus3 = (Get-Service -Name $servicename3 -ComputerName $server).status
if ($getservicestatus3 -eq "Stopped") {
Set-Service -Name $servicename3 -ComputerName $server -Status Running
Write-Host "$server $servicename3 Startad!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server startas..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
$getservicestatus2 = (Get-Service -Name $servicename2 -ComputerName $server).status
if ($getservicestatus2 -eq "Stopped") {
Set-Service -Name $servicename2 -ComputerName $server -Status Running
Write-Host "$server $servicename2 Startad!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server startas..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
$getservicestatus1 = (Get-Service -Name $servicename1 -ComputerName $server).status
if ($getservicestatus1 -eq "Stopped") {
Set-Service -Name $servicename1 -ComputerName $server -Status Running
Write-Host "$server $servicename1 Startad!" -ForegroundColor Green
Write-Host "Väntar i $delay sekunder innan nästa server startas..."
$nexttime = (Get-Date).AddSeconds($delay)
Write-Host "Nästa start sker $nexttime"
Start-Sleep -Seconds $delay
}
}
Write-Host "." -ForegroundColor DarkBlue
Read-Host "Tryck ENTER för att avsluta"
答案 0 :(得分:0)
您可以配置服务依赖项,以便服务仅在其依赖服务也启动/停止后才会启动/停止。
完成此操作后,您只需要停止service1
,Windows将按照其家属的顺序处理停止服务依赖关系。
您可以在“服务”中看到其他具有依赖关系的服务。例如,Fax
服务取决于Print Spooler
,RPC
本身取决于DCOM
,然后取决于Fax
如果手动停止sc config "service1" depend= "service2"
sc config "service2" depend= "service3"
sc config "service3" depend= "service4"
etc...
服务,您将看到GUI停止(按顺序)DCOM,RPC,假脱机程序然后传真。
配置依赖关系只需要完成一次(每台服务器):
sc.exe
(据我所知,没有本地Powershell命令执行此操作,我只找到sc
并使用wmi,$serverlist = Get-Content ".\server.txt"
foreach ($server in $serverlist) {
Get-Service -Name "service1" -ComputerName $server | Restart-Service
}
是更简单的命令。)
设置完成后,您只需要一小段代码就可以按顺序重新启动所有服务:
req.body.payload