Skip Ribbon Commands
Skip to main content
Sign In
SharePoint Program Manager, Infrastructure
Zach Rosenfield's SharePoint Blog > Posts > SP+PS 2010: PowerShell to Create a Service Application
November 18
SP+PS 2010: PowerShell to Create a Service Application

One of the principles that PowerShell offers is the ability to “discover” what commands you need to run to do your tasks.  This principle mostly holds up—with on caveat: you have to “Know” the concepts about the task upfront for the commands to make sense.  I’m going to both demonstrate what this discovery means at the same time as showing how to create a service application.  The idea behind the discovery concept is that you should be able to take the knowledge behind creating 1 specific type of service application and re-use it to create any other type of service application*.   Note that Search can be a bit more difficult because of all the flexibility built into 2010—the more flexible the system gets the more detailed setup can be.

First, you need to understand the concept of what is required to create a Service Application (I’m only going to discuss non-federated, so running on the same farm that is using it).  In the most simplified form: the following items are required for every service application:

1.       The SPServiceApplication: this is the actual definition in the configuration database that will contain information like related databases and settings.  Note that this requires providing a web service Application Pool (More on this later).

2.       The SPServiceApplicationProxy: This is the “service connection” that is used for Web Applications to “talk” to the Service Application

3.       Start at least one SPServiceInstance: This is the actual “process” that will be used to run the Service Application code.  This can run on multiple servers to share load and provide high-availability—but only 1 is “required”.

4.       Attach the service to a SPServiceApplicationProxyGroup: This is the ‘group’ of service applications that a web application can be set to use.  There is a “Default” group that you can add services to that all web apps will be associated with by default.

Each service obviously has it’s own requirements (for example, you need to create databases for the User Profile service), but PowerShell will actually Prompt you for the “required” parameters and will give defaults for as many as it can.  Feel free to provide alternates to the defaults.

So, with that, let’s use the four requirements above to create a service app!  Metadata Service is a good starting point…

1.       Start one or more service instances.  Note that these exist regardless of whether they have any service applications to actually run.  We also want to wait for this to fully provision before continuing.

$MetadataServiceInstance = (Get-SPServiceInstance |Where{$_.TypeName -eq "Managed Metadata Web Service"})   #Get a pointer to the Service Instance

      if($MetadataserviceInstance.Status -eq "Disabled"){  $MetadataserviceInstance | Start-SPServiceInstance  }  #Start the Service Instance

      while(-not ($MetadataServiceInstance.Status -eq "Online")){                                                 #wait for provisioning to finish

            write-host -ForegroundColor Yellow "Waiting for Metadata service to provision"; sleep 5;

}

     

Add-SPServiceApplicationProxyGroupMember -Identity $ProxyGroup -Member $MetaDataServiceAppProxy

2.       Get or Create an Application Pool.  If you already have one running other services you can get an existing one using the GET command, or create a new one like this.

$ManagedAccount = Get-SPManagedAccount | select -First 1  #Get a managed account. I RECOMMEND YOU CHANGE THIS LINE TO GET THE RIGHT ACCOUNT FOR YOU!
$ApplicationPool = New-SPIisWebServiceApplicationPool "SharePoint Hosted Services" -account $ManagedAccount

 

*Note: this is NOT a Web Application “app pool”.  Do NOT create one of these for a web application –it will NOT work.

3.       Create the Service Application

$MetaDataServiceApp = New-SPMetadataServiceApplication -Name "Metadata Service Application" -ApplicationPool $ApplicationPool

 

4.       Create the proxy.  I also “attach it to the default proxy group using the -DefaultProxyGroup flag.

   $MetaDataServiceAppProxy = New-SPMetadataServiceApplicationProxy -Name "Metadata Service Application Proxy" -ServiceApplication $MetaDataServiceApp –DefaultProxyGroup

 

That’s it!  It’s a little basic, and can’t be part of a larger script that is going to be “re-run” very easily, so here’s a full script with some error handling to wrap the action…

try{

      #Managed Account

      $ManagedAccount = Get-SPManagedAccount | select -First 1

      if ($ManagedAccount -eq $NULL) { throw "No Managed Accounts" }

     

      #App Pool

      $ApplicationPool = Get-SPIisWebServiceApplicationPool "SharePoint Hosted Services" -ea SilentlyContinue

      if($ApplicationPool -eq $null){

            $ApplicationPool = New-SPIisWebServiceApplicationPool "SharePoint Hosted Services" -account $ManagedAccount

            if (-not $?) { throw "Failed to create an application pool" }

      }

     

      #Create a Taxonomy Service Application

      if((Get-SPServiceApplication |?{$_.TypeName -eq "Managed Metadata Service"})-eq $null){     

            Write-Progress "Creating Taxonomy Service Application" -Status "Please Wait..."

            #get the service instance

            $MetadataServiceInstance = (Get-SPServiceInstance |?{$_.TypeName -eq "Managed Metadata Web Service"})

            if (-not $?) { throw "Failed to find Metadata service instance" }

           

             #Start Service instance

            if($MetadataserviceInstance.Status -eq "Disabled"){ 

                  $MetadataserviceInstance | Start-SPServiceInstance 

                  if (-not $?) { throw "Failed to start Metadata service instance" }

            }

            #Wait

            while(-not ($MetadataServiceInstance.Status -eq "Online")){ #wait for provisioning

                  write-host -ForegroundColor Yellow "Waiting for Metadata service to provision"; sleep 5;

            }

            #Create Service App

            $MetaDataServiceApp  = New-SPMetadataServiceApplication -Name "Metadata Service Application" -ApplicationPool $ApplicationPool

            if (-not $?) { throw "Failed to create Metadata Service Application" }

           

            #create proxy

            $MetaDataServiceAppProxy  = New-SPMetadataServiceApplicationProxy -Name "Metadata Service Application Proxy" -ServiceApplication $MetaDataServiceApp -DefaultProxyGroup

            if (-not $?) { throw "Failed to create Metadata Service Application Proxy" }

      }

}

catch

    {

            Write-Output $_

      }

 

Comments

Service App Databases

If I want to create the databases ahead of configuring the service app, what commands should I use?

I am trying to build an entire farm from the ground up with PS. I have gotten up to the service apps and while this help it does not cover pointing the service app to a database.

We have a strict naming convention for databases and can not have any database names that have a GUID in the name.
 on 12/11/2009 9:27 AM

re: Service App Databases

You can provide the database name directly to the commands.  If you provide a database name that does not exist, a new database will be created with that name.  If you provide a name that already exists, that database will be used.

See the parameter list on all New-SP<service>ServiceApplication commands for the different databases you can provide names for (as well as alternate SQL Servers).
Zach RosenfieldNo presence information on 12/28/2009 1:39 PM

API Updated...

It looks like the Get-SPIisWebServiceApplicationPool and New-SPIisWebServiceApplicationPool calls have been replaced with Get/New-SPServiceApplicationPool in RC.
 on 2/22/2010 3:57 PM

Setting up Service Applications

The example for Managed Metadata is extremely clear and understandable; the exposed principles (Starting the Service, creating the Serv.app pool, creating the Service Application and her Proxy is generally applicable to any Service Application ?
I presume that Search service requires something more complex and I've found some example, but for Profile, Web Analytics and so on?
Thanks in advance
 on 3/16/2010 12:57 AM

What is a web service application pool?

So I discovered (the hard way- couple of hours of errors ;) that the app pool in the New-SPWebAnalyticsServiceApplication is not an IIS app pool.

What is a SP service application pool?
 on 9/23/2010 10:32 AM

Issue with code

Zach,

I have noticed an issue with your above code. The problem I see is with the while block where you are waiting on the service status to become "online" before proceeding. The way it is now your while block will never exit and will cause an infinite loop because your code is checking the snapshot property $MetadataserviceInstance.status, which will never evaluate to anything other that "Provisioning". I think you need to reassign the metadata service to $MetadataserviceInstance inside the while block in order to get the updated status property.
 on 12/23/2010 3:18 PM

Cannot find the New-SPMetaDataServiceApplication commandlet

I cannot find the "New-SPMetadataServiceApplication" command let on my sharepoint server. I managed to get beginning of your code working but I am stopped when creating the Service Application. I have Sharepoint foundation 2010. Do I need the commercial verison of Sharepoint to recreate my application pool ?
 on 8/18/2011 6:50 AM

Content Source Error

When i am using the above script, search service application is created successfully but when i click on the Default content source it says following error, Appreciate your quick response.

I am not able to edit the Default content source

Error
Specified cast is not valid.

Troubleshoot issues with Microsoft SharePoint Foundation.

Correlation ID: f6f2cb3d-f0ed-44e8-bb12-44926d419dbe

Date and Time: 5/3/2012 11:42:51 AM

Go back to site

 on 5/2/2012 11:27 PM

How to use a database created ahead of time

I got an error using -Database with New-SPStateServiceApplication.

So to specify the database, I used Mount-SPStateServiceDatabase in place of New-SPStateServiceDatabase.
 on 6/27/2012 7:38 AM

Re: SP+PS 2010: PowerShell to Create a Service Application

This is still one of the top google results for provisioning services using powershell, but it was based on the 2010 Beta and hasn't been updated.

In the official release, Get-SPIISWebServiceApplicationPool is renamed to Get-SPServiceApplicationPool.
 on 6/27/2012 10:42 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Comments *


Name (required) *


Human Test


Checking if you're human: enter "1234" (no quotes)

Attachments