Start, Stop and restart one or multiple Windows services on one, all and specific SharePoint Servers using PowerShell

Posted: October 15, 2014 in Windows Services

I think that title will be my longest ever:-)

Here you will find 3 Functions for starting,stopping and restarting windows services that can target specific servers, local servers and all sharePoint Servers.

Function to start and verify that a service is started:


Function VerifyStartedService ($Service, $Server)
{
    if($Server){$servername = $Server; Write-Host -f DarkGray "Running code on specified server.."}
    if(!($servername)){$servername = hostname; Write-Host -f DarkGray "Running code on local server.."}

    $Status = Get-Service -Name $Service -ComputerName $servername
    if(!($Status.status -eq "Running"))
    {
    Get-Service -Name $Service -ComputerName $servername | Set-Service -Status Running
    while($Status.status -ne "Running"){Write-Host "Waiting for $Service to Start.."; sleep 3;$Status = Get-Service -Name $Service -ComputerName $servername}
    Write-Host "OK"
    Write-Host ""
    }

    if($Status.status -eq "Running")
    {
    $serviceStatus = $Status.status
    Write-Host -f Green "$Service on $servername has status: $serviceStatus"
    }
}


Using the VerifyStartedService Function

If you want to enable services on all SharePoint servers, you can do:


#Run on all SharePoint servers.
$AllSPServers = get-spserver | ? { $_.Role -eq "Application"}

foreach ($SPServer in $AllSPServers)
{
    $servername = $SPServer.name
    write-host "----------------------"
    Write-Host -f DarkGray "Running on all SharePoint Servers, currently: $servername.."
    write-host "----------------------"
    write-host ""

    #Add the windows services you want to restart.
    VerifyStartedService -Service SPAdminV4
    VerifyStartedService -Service SPTimerV4
}

If you want enable services on the local server, use:

VerifyStartedService -Service SPAdminV4
VerifyStartedService -Service SPTimerV4

If you want start services on specific servers, use:


VerifyStartedService -Service SPAdminV4 -Server "WFE1"
VerifyStartedService -Service SPTimerV4 -Server "App3"

Function to stop and verify that services are stopped:


Function VerifyStoppedService ($Service, $Server)
{
    if($Server){$servername = $Server; Write-Host -f DarkGray "Running code on specified server.."}
    if(!($servername)){$servername = hostname; Write-Host -f DarkGray "Running code on local server.."}

    $Status = Get-Service -Name $Service -ComputerName $servername
    if($Status.status -eq "Running")
    {
    Get-Service -Name $Service -ComputerName $servername | Set-Service -Status Stopped
    while($Status.status -ne "Stopped"){Write-Host "Waiting for $Service to Stop.."; sleep 3;$Status = Get-Service -Name $Service -ComputerName $servername}
    Write-Host "OK"
    Write-Host ""
    }

    if($Status.status -eq "Stopped")
    {
    $serviceStatus = $Status.status
    Write-Host -f Green "$Service on $servername has status: $serviceStatus"
    }
}

Using the VerifyStoppedService  Function

If you want to stop services on all SharePoint servers, you can do:


#Run on all SharePoint servers.
$AllSPServers = get-spserver | ? { $_.Role -eq "Application"}

foreach ($SPServer in $AllSPServers)
{
    $servername = $SPServer.name
    write-host "----------------------"
    Write-Host -f DarkGray "Running on all SharePoint Servers, currently: $servername.."
    write-host "----------------------"
    write-host ""

    #Add the windows services you want to restart.
    VerifyStoppedService -Service SPAdminV4
    VerifyStoppedService -Service SPTimerV4
}

If you want stop services on the local server, use:


VerifyStoppedService -Service SPAdminV4
VerifyStoppedService -Service SPTimerV4

If you want stop services on specific servers, use:


VerifyStoppedService -Service SPAdminV4 -Server "WFE4"
VerifyStoppedService -Service SPTimerV4 -Server "App2"

Function to restart services:


Function RestartService ($Service, $Server)
{
    if($Server){$servername = $Server; Write-Host -f DarkGray "Running code on specified server $servername.."}
    if(!($servername)){$servername = hostname; Write-Host -f DarkGray "Running code on local server.."}

    Write-Host -f Cyan "Restarting $Service on $servername.."
    Get-Service -Name $Service -ComputerName $servername | Set-Service -Status Stopped
    $Status = Get-Service -Name $Service -ComputerName $servername
    while($Status.status -eq "Running"){Write-Host "Waiting for $Service to stop.."; sleep 3;$Status = Get-Service -Name $Service -ComputerName $servername}

    Get-Service -Name $Service -ComputerName $servername | Set-Service -Status Running
    while($Status.status -eq "Stopped"){Write-Host "Waiting for $Service to Start.."; sleep 3;$Status = Get-Service -Name $Service -ComputerName $servername}
    Write-Host -f Green "OK"
    Write-Host ""
}

Using the RestartService Function.
If you want to restart services on all SharePoint servers, you can do:

$AllSPServers = get-spserver | ? { $_.Role -eq "Application"}

#Restart services on all sharepoint servers:
foreach ($SPServer in $AllSPServers)
{
    $servername = $SPServer.name
    write-host "----------------------"
    Write-Host -f DarkGray "Running on all SharePoint Servers, currently: $servername.."
    write-host "----------------------"
    write-host ""

    #For iisreset use:
    $iis = iisreset $server

    #Add services to restart:
    RestartService -Service SPAdminV4
    RestartService -Service SPTimerV4
}

If you want restart services on the local server,use:


RestartService -Service SPAdminV4
RestartService -Service SPTimerV4

If you want restart services on specific servers, use:


RestartService -Service SPAdminV4 -Server "WFE5"
RestartService -Service SPTimerV4 -Server "WFE6"

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s