Windows : Script PowerShell pour stopper une liste de serveurs dans un ordre spécifique - Idéal pour les fermes SharePoint
Tout fermier SharePoint est confronté un jour ou l’autre à cette question d’arrêt de serveur.
Il est évident que pour une infrastructure Stand Alone (1 serveur) ou Small farm (2-3 serveurs), cela se fera très simplement et ne posera pas de soucis particulier, tant que le serveur SQL est arrété en dernier.
Le cas devient plus délicat lorsqu’on commence à gérer plusieurs Large Farm (plus de 4 serveurs) dans un ou plusieurs DataCenter.
Le script suivant permet donc de stopper les machines, en utilisant la commande standard de Windows “Shutdown /s”, suivant l’ordre donné dans la liste des machines.
# Specific parameters
$ServersList = "SPWEBSERVER1", "SPWEBSERVER2", "SPWEBSERVER3", "SPEXCELSERVER1", "SPSEARCHSERVER2", "SPAPPSERVER2", "SPDBSERVER1", "SPDBSERVER2"
$Logfile = "C:\SHAREPOINT_SERVERS_STOP.log"# Generic parameters
[int]$WaitTime = 20
[int]$Counter = 0
[DateTime]$mydate = get-date
[string]$MyCommand = ""
[string]$feedback = ""
[string]$ProcessFileToExec = "shutdown.exe"
[string]$ProcessFileParam = "/s /f /t 3 /m \\"Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}function New-Sleep {
[cmdletbinding()]
param(
[parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, HelpMessage="No time specified")]
[int]$s
)
for ($i=1; $i -lt $s; $i++) {
[int]$TimeLeft=$s-$i
Write-Progress -Activity "Waiting $s seconds..." -PercentComplete (100/$s*$i) -CurrentOperation "$TimeLeft seconds left ($i elapsed)" -Status "Please wait"
Start-Sleep -s 1
}
Write-Progress -Completed $true -Status "Please wait"
} # end function New-SleepFunction CreateIntroLog
{
LogWrite "================================================="
LogWrite "--- SCRIPT STARTED:", $mydate.ToString(), " ---"
LogWrite "================================================="
write-host ""
write-host ""
write-host ""
write-host ""
write-host ""
write-host ""
write-host ""
write-host ""
write-host ""
write-host "================================================="
write-host -ForegroundColor Green "--- SCRIPT STARTED:", $mydate.ToString(), " ---"
write-host "================================================="
}Function ResetDNS
{
write-host "================================================="
write-host "=== Flushing DNS ==="
ipconfig /flushdns | out-null
write-host "=== Registering DNS ==="
ipconfig /registerdns | out-null
write-host "================================================="
}Function TestPingServer([string]$MyServer)
{
if(Test-Connection -Cn $MyServer -BufferSize 16 -Count 1 -ea 0 -quiet)
{
write-host " Connection OK to $MyServer"
return $true
}
ELSE
{
write-host " >>> Problem connecting to $MyServer <<< "
return $false
}
}function StartProcess()
{
# Create the stopwatch
[System.Diagnostics.Stopwatch] $sw;
$sw = New-Object System.Diagnostics.StopWatch
$sw.Start()
$Counter = 0
cls
ResetDNS
CreateIntroLog
foreach($MyServer in $ServersList)
{
$Counter ++
write-host " ----------------------------------------------------"
write-host " Server to stop:", $MyServer, "- Server Number:", $Counter
[bool]$ResultPing = TestPingServer $MyServer
if($ResultPing)
{
write-host -ForegroundColor Yellow " - STOP SERVER ", $MyServer, " - "
$MyCommand = $($ProcessFileToExec + " " + $ProcessFileParam + $MyServer)
write-host $MyCommand
try
{
invoke-expression -command " $MyCommand" -Verbose -OutVariable myOut
foreach($MyLine in $myOut)
{
write-host "Line", $MyLine
}
$feedback = [string]::join(" ", @($myOut))
write-host " Return: ", $feedback
}
catch [system.exception]
{
$feedback = $(" [Function Shutdown caught a system exception]: " + $_.Exception.Message)
write-host -ForegroundColor Red " Return ERROR: ", $feedback
}
finally
{
write-host -ForegroundColor Green " Wait for the next Server stop:", $WaitTime
LogWrite " ----------------------------------------------------"
LogWrite " Server to stop:", $MyServer, "- Server Number:", $Counter
LogWrite " Execution Time:", $mydate
LogWrite " Command:", $MyCommand
LogWrite " Return:", $feedback
LogWrite " ----------------------------------------------------"
New-Sleep -s $WaitTime
}
}
else
{
write-host -ForegroundColor Red "--- NO PING TO SERVER:", $MyServer, " --- "
write-host -ForegroundColor Red " Shutdown Impossible"
LogWrite " ----------------------------------------------------"
LogWrite " No Ping to server:", $MyServer, "- Server Number:", $Counter
LogWrite " Shutdown Impossible"
LogWrite " Execution Time:", $mydate
LogWrite " ----------------------------------------------------"
}
write-host " ----------------------------------------------------"
}
$sw.Stop()
# Write the compact output to the screen
write-host " "
write-host " "
write-host " ------------------------------------------------------------- "
write-host -ForegroundColor Green " ----", $Counter " Servers stopped in Time: ", $sw.Elapsed.ToString(), "----"
write-host " ------------------------------------------------------------- "
write-host " "
LogWrite "================================================="
LogWrite " ---", $Counter, " Servers stopped in Time: ", $sw.Elapsed.ToString(), "---"
LogWrite "================================================="
}cls
StartProcess
Une fois les deux variables de départ enrichies, il vous suffit de lancer une commande PowerShell (en mode Administrateur), avec un compte Administrateur du domaine (ou qui sera administrateur des machines à éteindre).
Romelard Fabrice [MBA Risk Mgt]
Commentaires
Enregistrer un commentaire