SharePoint Calculator Service Part 7 – Custom Admin Setting PowerShell
In Part 6 of this series, we created a custom setting for our Calculator service application and a web page to manage the setting from the SharePoint Central Administration web site.
In this article, we’ll create the accompanying PowerShell cmdlet so the setting can be scripted.
The proposed syntax of the new cmdlet is:
Set-CalculatorServiceApplication [-Identity] <SPServiceApplicationPipeBind> -Precision <Int32>
Create the Cmdlet
First, we’ll subclass SPCmdlet:
- [Cmdlet(VerbsCommon.Set, "CalculatorServiceApplication", SupportsShouldProcess = true)]
- [SPCmdlet(RequireLocalFarmExist = true, RequireUserFarmAdmin = true)]
- internal sealed class SetCalculatorServiceApplication : SPCmdlet
- {
- [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
- [ValidateNotNull]
- public SPServiceApplicationPipeBind Identity
- {
- get;
- set;
- }
- [Parameter(Mandatory = true)]
- [ValidateRange(1, 20)]
- public int Precision
- {
- get;
- set;
- }
- protected override void InternalProcessRecord()
- {
- // Read the service application identity from the pipebind parameter
- CalculatorServiceApplication serviceApplication = this.Identity.Read() as CalculatorServiceApplication;
- // Check for a valid service application
- if (null == serviceApplication)
- {
- // Write an error
- WriteError(
- new InvalidOperationException("Calculator service application not found."),
- ErrorCategory.InvalidArgument,
- this.Identity);
- // Skip this record
- SkipProcessCurrentRecord();
- }
- if (ShouldProcess(serviceApplication.Name))
- {
- // Modify service application settings
- serviceApplication.Precision = this.Precision;
- // Persiste the service application object
- serviceApplication.Update();
- // Write the object to the pipeline
- WriteObject(serviceApplication);
- }
- }
- }
This is very similar to the New-CalculatorServiceApplication cmdlet we created in Part 5 of this series, so feel free to review that article.
The interesting code here is the Identity property of type SPServiceApplicationPipeBind (lines 5-11), which allows a instance of our Calculator service application to be passed to our cmdlet. The parameter can either be a Guid identifier (as a string on the command line), a PowerShell variable that references an SPServiceApplication object, or an SPServiceApplication object on the pipeline.
For example, given the following:
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
My Calculator Ser... Sample.Calculator... 2b07ed87-e9af-40a2-8a59-8d6b746bb19b
We can then update the Precision setting of the above Calculator service application using any of the following commands:
PS C:\> Set-CalculatorServiceApplication 2b07ed87-e9af-40a2-8a59-8d6b746bb19b -Precision 3
PS C:\> Get-SPServiceApplication 2b07ed87-e9af-40a2-8a59-8d6b746bb19b | Set-CalculatorServiceApplication -Precision 3
PS C:\> $app = Get-SPServiceApplication 2b07ed87-e9af-40a2-8a59-8d6b746bb19b
PS C:\> Set-CalculatorServiceApplication $app -Precision 3
Register the Cmdlet
Don’t forget to register the cmdlet (see Part 5 for details on the registration process):
- <ps:Cmdlet>
- <ps:VerbName>Set-CalculatorServiceApplication</ps:VerbName>
- <ps:ClassName>Sample.Calculator.Service.PowerShell.SetCalculatorServiceApplication</ps:ClassName>
- <ps:HelpFile>Sample.Calculator.Service.PowerShell.dll-help.xml</ps:HelpFile>
- </ps:Cmdlet>
That’s it!
Administrators can now use our handy Set-CalculatorServiceApplication cmdlet to script the Precision setting.