Start and Stop SharePoint Service Instances using PowerShell

Posted: October 6, 2014 in Service Applications

When configuring your shiny new Service Applications using PowerShell, depending on the size of your SharePoint farm. You may want to be running some services on multiple SharePoint Servers. The code bellow consists of two functions to start and stop service instances per server.

Firstly if you have no idea what the service instances are called, use the code:

Get-SPServiceInstance | select typename |  Sort-Object typename | Get-Unique -OnType | % {$_.TypeName}

Then load the two functions:

<#====================================================================
 Copyright © 2014, June. Bjørn Roalkvam
 www.SharePointbjorn.com, bjorn80@gmail.com 

 Always test all scripts you find in an test environment prior to production:-), 
 you are a responsible SharePoint Administrator afterall!
====================================================================#>


#Start Function:

Function StartServiceInstance ($Server,$Services) #$ServiceTypeName
{
        #Traverse services arrray
        write-host -f Yellow "$Server status: "
        Foreach ($ServiceTypeName in $Services)
        {
            $Service = Get-SPServiceInstance -Server $Server | ? {$_.TypeName -eq $ServiceTypeName}
            #If service is disabled, start it.
            if($Service.status -eq "Disabled") 
            {
            Start-Spserviceinstance $Service
            While ($Service.status -ne "Online") {Write-Host -f DarkGray "$ServiceTypeName on $Server is Starting.."; sleep 3; $Service = Get-SPServiceInstance -Server $Server 

| ? {$_.TypeName -eq $ServiceTypeName}}
            Write-Host -f Green "$Service is Online on $Server."
            }

        $srvStatus = $Service.status
        write-host "$ServiceTypeName is $srvStatus"        

        }#EndForeach service      
        write-host "-------------------------------------------"
} #EndFunction




#Stop Function:

Function StopServiceInstance ($Server,$Services) #$ServiceTypeName
{
        #Traverse services arrray
        write-host -f Yellow "$Server status: "
        Foreach ($ServiceTypeName in $Services)
        {
            $Service = Get-SPServiceInstance -Server $Server | ? {$_.TypeName -eq $ServiceTypeName}
            #If service is Online, stop it.
            if($Service.status -eq "Online") 
            {
            Stop-Spserviceinstance $Service
            While ($Service.status -ne "Disabled") {Write-Host -f DarkGray "$ServiceTypeName on $Server is Stopping.."; sleep 3; $Service = Get-SPServiceInstance -Server 

$Server | ? {$_.TypeName -eq $ServiceTypeName}}
            Write-Host -f Green "$Service is Disabled on $Server."
            }

        $srvStatus = $Service.status
        write-host "$ServiceTypeName is $srvStatus"        

        }#EndForeach service      
        write-host "-------------------------------------------"
} #EndFunction

#Use the function as follow, to ensure the correct service instances are Online or Disabled on chosen servers, use the two functions like this:

#To ensure services are running on servers:
StartServiceInstance -Server "App1" -Services ("Secure Store Service","Managed Metadata Web Service","User Profile Service")
StartServiceInstance -Server "App2" -Services ("Secure Store Service","Managed Metadata Web Service","User Profile Service")


#To ensure services are NOT running on servers:
StopServiceInstance -Server "WFE1" -Services ("Secure Store Service","Managed Metadata Web Service","User Profile Service")
StopServiceInstance -Server "WFE2" -Services ("Secure Store Service","Managed Metadata Web Service","User Profile Service")
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