Site Tools


restart_service_on_remote_server

Mit diesem Script, könnt ihr Dienste auf Servern von der Ferne aus neustarten, auch wenn ihr den Dienstnamen nicht ganz kennt oder mehrere Deinste gestartet werden müssen, die ähnlich heißen, was ja öfter auf Anwendungsservern der Fall ist.

Das Script sucht dann nach Deinsten die eigentlich automatisch starten sollten, aber nicht im Status “Gestartet” stehen, diese werden dann gestartet und das Ergebnis wird euch dann noch angezeigt.

restartserviceremote.ps1
Write-Host 
Write-Host "Restart Service on single Server" -foreground "yellow"
Write-Host
 
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$ScriptPath = Get-ScriptDirectory
 
#Write-Host "Press any key to continue ..." -foreground "yellow"
 
#$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
 
$cmdlet = "ActiveDirectory" 
if ( (get-command -Name $cmdlet -ErrorAction SilentlyContinue) -eq $null ) {Import-module ActiveDirectory} 
 
$ExecUser = ([Environment]::UserName) 
 
$ServiceName = Read-Host "ServiceName or Part of Service Name to be started"
$ServerName = Read-Host "ServerName"
 
Write-Host "Search for Services" -foreground "yellow"
 
Get-Service -DisplayName "*$ServiceName*" -ComputerName $ServerName | 
Select-Object -Property MachineName,Name,DisplayName,Status,StartType | 
where-object {$_.Status -eq 'stopped' -and $_.StartType -eq 'Automatic'} | 
Format-Table -auto
 
$stopped = Get-Service -DisplayName "*$ServiceName*" -ComputerName $ServerName | Select-Object -Property MachineName,Name,DisplayName,Status,StartType | where-object {$_.Status -eq 'stopped' -and $_.StartType -eq 'Automatic'}
 
if ($stopped) {Write-Host
            Write-Host "Restart Services when not running but Startuptype set to Automatic" -foreground "yellow"
            Write-Host
			Get-Service -DisplayName "*$ServiceName*" -ComputerName $ServerName | 
			Select-Object -Property * | 
			where-object {$_.Status -eq 'stopped' -and $_.StartType -eq 'Automatic'} | foreach {set-service -Computername $_.MachineName -Name $_.Name -Status Running}} 
else {
    Write-Host
    Write-Host "******** No Service Restart needed ********" -foreground "yellow"
    Write-Host} 
 
Write-Host "Search for Services and show Status" -foreground "yellow"
 
Get-Service -DisplayName "*$ServiceName*" -ComputerName $ServerName | 
Select-Object -Property MachineName,Name,DisplayName,Status,StartType | 
where-object {$_.StartType -eq 'Automatic'} | 
Format-Table -auto
 
Write-Host "Done! " -foreground "yellow"
Write-Host "Press any key..." -foreground "yellow"
restart_service_on_remote_server.txt · Last modified: 2018/07/11 13:07 by 80.70.169.36