Restart SharePoint 2010 Service through PowerShell

I wanted to restart a specific service, “Microsoft SharePoint Foundation Web Application”, at night-time so there was no conflict with connected users. Since you don’t want to get up at midnight to restart a service, the easiest way is writing a PowerShell script which stops the service and then starts it again. After the script is written add it as a timer job to run at a desired time.

The script is pretty straight forward:

$server = "win-lhg69g3kdb2"
$service = Get-SPServiceInstance -Server $server | where-object {$_.TypeName -eq "Microsoft SharePoint Foundation Web Application"}
$service | Stop-SPServiceInstance -confirm:$false > $null
while(-not ($service.Status -eq "Disabled")){
	write-host -ForegroundColor Yellow $service.Status; sleep 5;
	$service = Get-SPServiceInstance -Server $server | where-object {$_.TypeName -eq "Microsoft SharePoint Foundation Web Application"}
}
Get-SPServiceInstance -Server $server | where-object {$_.TypeName -eq "Microsoft SharePoint Foundation Web Application"} | Start-SPServiceInstance -confirm:$false > $null

You only have to replace the $server variable with the server name where you wish to restart the service. My guess is that this script works for almost every service within SharePoint 2010, but I haven’t tested it.

The next step is to schedule the script at the timer service. Make sure the credentials you use to run the script have the right to start/stop services.

Advertisements

One thought on “Restart SharePoint 2010 Service through PowerShell

  1. Pingback: Unable to STOP microsoft sharepoint foundation web application service | DL-UAT

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

w

Connecting to %s