Script a Metadata Service Application using PowerShell

Scripting the deployment of service applications is a good idea; it is predictable, repeatable and gets you control over database names. I found a good script for the Metadata Service Application at Zach Rosenfield’s blog post SP+PS 2010: PowerShell to Create a Service Application.  Read Zach’s blog post for a detailed description of the script.

Here is the script, adjusted with RTM changes:

001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 Set-ExecutionPolicy unrestricted # ----------------------------------------------------------------------------- # Script for setup managed metadata service # ----------------------------------------------------------------------------- write-host -ForegroundColor Green "setup managed metadata service..."; $FarmName = "Lab" $DatabaseServer = "LabDB" $DatabaseUser = "lab\spfarmservice" $DatabaseUserPassword = (ConvertTo-SecureString "pass@word1" -AsPlainText -force) $DatabaseCredentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $DatabaseUser, $DatabaseUserPassword  $ServiceApplicationUser = "lab\SpServiceApp" $ServiceApplicationUserPassword = (ConvertTo-SecureString "pass@word1" -AsPlainText -force) $ServiceApplicationCredentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $ServiceApplicationUser, $ServiceApplicationUserPassword  $ServiceApplicationPool = "SecurityTokenServiceApplicationPool" $MetadataDatabaseName = ("{0}_Metadata" -f $FarmName) $MetadataServiceName = "Managed Metadata Service" try{       #Managed Account       write-host -ForegroundColor Green "Managed account...";       $ManagedAccount = Get-SPManagedAccount $ServiceApplicationUser       if ($ManagedAccount -eq $NULL)        {             write-host  -ForegroundColor Green "Create new managed account"            $ManagedAccount = New-SPManagedAccount -Credential $ServiceApplicationCredentials       }             #App Pool       write-host -ForegroundColor Green "App pool...";       $ApplicationPool = Get-SPServiceApplicationPool  "SharePoint Hosted Services" -ea SilentlyContinue       if($ApplicationPool -eq $null){              write-host  -ForegroundColor Green "Create new app pool"             $ApplicationPool = New-SPServiceApplicationPool "SharePoint Hosted Services" -account $ManagedAccount              if (-not $?) { throw "Failed to create an application pool" }       }             #Create a Taxonomy Service Application       write-host -ForegroundColor Green "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             Write-Progress "Start Service instance" -Status "Please Wait..."             if($MetadataserviceInstance.Status -eq "Disabled"){                    $MetadataserviceInstance | Start-SPServiceInstance                    if (-not $?) { throw "Failed to start Metadata service instance" }             }              #Wait             write-host -ForegroundColor Yellow "Waiting for Metadata service to provision";             while(-not ($MetadataServiceInstance.Status -eq "Online")){ #wait for provisioning                   # write status                   write-host  -ForegroundColor Yellow -NoNewline $MetadataServiceInstance.Status; sleep 5;                   write-host  -ForegroundColor Yellow -NoNewline "."; sleep 5;                   # Update the object                   $MetadataServiceInstance = (Get-SPServiceInstance |?{$_.TypeName -eq "Managed Metadata Web Service"})             }             #Create Service App             $MetaDataServiceApp  = New-SPMetadataServiceApplication -Name "Metadata Service Application" -ApplicationPool $ApplicationPool  -DatabaseServer $DatabaseServer -DatabaseName $MetadataDatabaseName             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 $_  }