In Part 4 of this series, we integrated our Calculator service application creation and provisioning experience with the SharePoint Central Administration site.
While this provides a nice experience for ad-hoc SharePoint deployments, we would also like to support scripted deployments which are completely automated.
In this article, we’ll build a PowerShell cmdlet to integrate our service application creation experience into the SharePoint 2010 Management Shell to support these scenarios.
We’ll create a cmdlet named New-CalculatorServiceApplication that will take two required parameters: the name of the new service application, and the application pool that will host it. The cmdlet will then create the new service application and provision it.
Here’s the proposed syntax:
PS> gcm New-CalculatorServiceApplication -syntax New-CalculatorServiceApplication [-Name] <String> [-ApplicationPool] <SPIisWebServiceApplicationPoolPipeBind>
The SPIisWebServiceApplicationPoolPipeBind parameter accepts a service application pool object, which can be created by the New-SPServiceApplicationPool cmdlet. Alternatively, an administration can use the Get-SPServiceApplicationPool cmdlet to select an existing service application pool to be used for the new service application.
The first thing we’ll do is create the New-CalculatorServiceApplication cmdlet by subclassing SPCmdlet:
The Cmdlet attribute (line 1) defines the cmdlet verb and noun.
The SPCmdlet attribute (line 2) ensures that the local machine is joined to a SharePoint farm and that the caller is a farm administrator before the cmdlet is executed.
The Name and ApplicationPool properties (lines 5-21) are the two cmdlet parameters. The SPIisWebServiceApplicationPoolPipeBind class is a SharePoint class that has associated cmdlets for creating and looking up application pools that can be passed to the cmdlet as the ApplicationPool parameter, i.e., New-SPServiceApplicationPool.
The InternalProcessRecord method (lines 23-76) is where the work happens.
Lines 25-50 simply validate the farm configuration.
Line 53 reads the SPIisWebServiceApplicationPool parameter from the pipebind parameter.
Lines 65-71 create and provision a Calculator service application, which should look familiar since it’s the same code we used in the Central Administration site UI.
And finally, line 74 writes the new service application object out to the PowerShell pipeline where it can be piped as input to the next cmdlet in the pipeline.
Now, we need to tell SharePoint Management Shell about our new cmdlet. This is done by first creating an xml registration file as follows:
We simply register our assembly full name (line 5), our cmdlet verb and name (line 7), the class that implements the cmdlet (line 8), and a help file (line 9). We won’t actually create the help file for now since it’s optional.
And finally, we’ll update our temporary installation batch file to copy the xml file to the appropriate location:
xcopy /y "%_SETUPPATH%\PowerShell\*Cmdlets.xml" "%_INSTALLPATH%\Config\PowerShell\registration\*.*"
Well, it’s pretty simple for SharePoint administrators to create our service application from the Management Shell now:
PS C:\> $appPool = Get-SPServiceApplicationPool CalculatorServiceAppPool PS C:\> New-CalculatorServiceApplication "Calculator" $appPool
DisplayName TypeName Id ----------- -------- -- Calculator Sample.Calculator... 9ed7060a-618a-4363-8753-6b9b4d6a5d15
The first command assigns the application pool named “CalculatorServiceAppPool” (which I previously created using the “New-SPServiceApplicationPool” cmdlet) to a variable, and the second command creates a new CalculatorServiceApplication with the name “Calculator” in the “CalculatorServiceApplicationAppPool”.
Or, I can do this on one line using the PowerShell pipeline:
PS C:\> Get-SPServiceApplicationPool CalculatorServiceAppPool | New-CalculatorServiceApplication "Calculator"
I can also use the built-in cmdlets to manage the new service application. For example:
PS C:\> Get-SPServiceApplication
DisplayName TypeName Id ----------- -------- -- Security Token Se... Security Token Se... 7d88501a-a3e0-4289-aa96-62048803b1fb Application Disco... Application Disco... e79b6d9d-f7a0-4ac9-bdda-a9df8dc5e138 WSS_UsageApplication Usage and Health ... 7f75ee92-3930-4496-b1e2-743829f74cf4 Calculator Sample.Calculator... 9ed7060a-618a-4363-8753-6b9b4d6a5d15
So there you have it—a PowerShell cmdlet that makes it easy for administrators to automate deployment of the Calculator service application.