Wednesday, March 30, 2011

Start/Stop a SharePoint 2010 Service Application

Sometimes, it can be useful and more efficient to handle Sharepoint Service applications using Powershell.
The following script can be used for starting or stopping a service application instance.

A Sharepoint Service application is running behind a service application instance. This instance represents the service application on the whole Sharepoint Farm.
Stopping the instance will stop the related service application on all the servers in the farm.

First of all, we need to get all the service instances running on the farm:
Get-SPServiceInstance

From the list, select your service instance GUID and copy it.
For stopping the service instance, you just need now to run the Stop-SPServiceInstance command:
Stop-SPServiceInstance -Identity "YourServiceGUID"

Type "Y" when prompted.

Start-SPServiceInstance can be used using the same manner for starting the service again.

Attach Visual Studio to an application using Powershell

This script can be used to run the Visual Studio just in time debugger for debugging your website. It gets the name of the application pool running the website and then attach the JIT to the w3wp process.

$filter = "*MyWebsite*"

$w3wp = get-WmiObject Win32_Process -Filter "Name='w3wp.exe'"
$prid = $w3wp | where { $_.CommandLine -like $filter }

c:\Windows\System32\vsjitdebugger.exe -p $prid.ProcessId