PowerShell for SharePoint Admins–The REAL Primer–Series #6

This is the sixth entry of my PowerShell for SharePoint Admins series. In this session, we will configure Service Applications with corresponding databases.  I am basing this series on what SharePoint Administrators have said are the most important tasks to accomplish in PowerShell. Here is the list:


  1. Create/Delete Sites, Webs and Web Applications
  2. Backup and Restore Sites, Webs and Web Applications
  3. Deploy Solutions and Features
  4. Add/Delete Users
  5. Logs
  6. Configure Service Applications
  7. Manage Content Databases
  8. Manage Timer Jobs
  9. Manage Content Deployment


SharePoint service applications

From MSDN: The Service Application Framework provides over 20 services that are built into the core product. For example, SharePoint Search is implemented by Service Application Framework. The Service Application Framework is an API that is provided to build services that are hosted by back-end application servers and consumed by front-end Web servers. The Service Application Framework replaces the Shared Services Provider in Microsoft Office SharePoint Server 2007. The Service Application Framework model is much more flexible than the Shared Services Provider model. Service applications can be used by a single server farm or shared across farms, allowing centralized shared computing investments. Service Application Framework applications are easily scaled out for load balancing high-demand service applications.  

A good write up on them by Spence Harbar is here; or you could also watch my video on them HERE.

Although neither of the previous links go into the installation and configuration at all, they do give a good overview of what they are and how they are implemented.  In this session we will install and configure them using PowerShell.

Scripting Hygiene

So, when I was learning scripting for the first time WAY WAY back in the 80s…the thing that helped me most was understanding what it was that I was trying to do and then mapping out the steps to get there.  The class I had asked us to write a script that had a figure get in and out of a car.  It taught us how to think methodically.  So in this session, we will look at the step by step process and write the corresponding script to get ‘er done.  Sound like a plan?  Let’s start:

When you start out writing PowerShell script (or anything) you should know that documenting is IMPORTANT.  Making comments in your code will not only help others who are reading your script, but also YOU who will eventually update it and wonder what you were thinking when you wrote a certain line or section.  So what I would suggest is start out in the PowerShell_ISE (or whatever IDE editor you want – I like NotePad++) and use the comment symbol ‘#’ to comment about what you are about to do.

So let’s start off with stating what this script does.  type the following into your editor.

# This script configures the Microsoft Managed Metadata Service Application

The absolute next thing you want to do is make sure that the PowerShell snapin is loaded (unless you are using the PowerShell_ISE that we configured to automatically load it—but we’ll handle it even if it is).  So skip a couple of lines down in your script and type the following:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

We added the parameter that will allow PowerShell to ignore the issue if the snapin is already loaded (-erroraction SilentlyContinue)

Now we want to add our variables for PowerShell to refer to in our script.  These variables are things such as server names, database names, application pool names, etc. so that we don’t have to keep typing them in over and over again.  Type the following a couple of lines below the last line you typed:


$databaseServerName = SQL1
$AppPoolName = “AppPoolName”
$AppPoolUserAcct = “domain\appPoolAcct”
$ManagedMetadataName = “Managed Metadata Service Application”

OK, so now we’ve defined the variables.  Now we start writing the cmdlets that will configure the Service app.  We will group some of the cmdlets in between the  { } symbols.  Here are the next lines:


 $AppPool = Get-SPServiceApplicationPool –Identity $AppPoolName<br>{<br>     $AppPoolAcct = Get-SPManagedAccount –Identity $AppPoolUserAcct<br>{ <br>      Write-Host "Please supply the password for the Service Account..."<br>      $appPoolCred = Get-Credential $appPoolUserName<br>      $appPoolAccount = New-SPManagedAccount -Credential $appPoolCred  <br>  }         $appPoolAccount = Get-SPManagedAccount -Identity $appPoolUserName         if($appPoolAccount -eq $null) <br>      { <br>          Write-Host "Cannot create or find the managed account 
$appPoolUserName, please ensure the account exists."<br>    Exit -1 <br>  }     New-SPServiceApplicationPool -Name $saAppPoolName -Account 
$appPoolAccount       <br>} 



WHOA!  I threw a couple of new things in here.  Let’s talk about them.  First I threw the Write-Host cmdlet.  All that does is put whatever follows in quotes on the screen; kinda like ECHO.   The other thing I threw at you was the IF statement.  The way IF statements work is when you want the script to do something on the condition that something else is in place (or not).  In this script, we are telling the script that “If the application Pool account is blank….” (-eq $null means “Blank”) then what you do is to open a { symbol and put in what you want to happen.  In this case we want PoSh to print out on screen “Cannot create or find …..” and then to exit out.  That’s it.

OK, so what’s next?  We have set our variables, told PoSh to create an application pool for the service using a managed account and now its time to set the Proxy.

 Write-Host "Creating Metadata Service and Proxy..."
New-SPMetadataServiceApplication -Name $ManagedMetadataName -ApplicationPool $saAppPoolName -DatabaseServer $databaseServerName -DatabaseName"MetadataDB"

New-SPMetadataServiceApplicationProxy -Name "$ManagedMetadataName Proxy"-DefaultProxyGroup -ServiceApplication $ManagedMetadataName

Get-SPServiceInstance | where-object {$_.TypeName -eq "Managed Metadata Web Service"} | Start-SPServiceInstance

Next, you save the file as ManagedMetadata.ps1 and you’re done!

In our next session, we will get down and do some security stuff – Adding/Editing Users and Groups!