<#====================================================================

Copyright © 2015, September. Michael Pomfret

Creates the PerformancePoint Service Application.ps1

This service allows you to monitor and analyze your business by using dashboards, reports or KPIs.

Read more: http://technet.microsoft.com/en-us/library/ee681490

====================================================================#>

#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window

If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)

{

Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null

}

<#====================================================================

Get SharePoint variables

====================================================================#>

Add-Module(“C:\Temp\SharePoint2013\GetVariables.ps1”)

function Add-Module($strFileName){

Write-Host “Attempting to run ” $strFileName

If (Test-Path $strFileName){

&($strFileName)

}Else{

Write-Host “Cannot locate” $strFileName

}

}

$ServiceApplicationName = “PerformancePoint Service Application”

$ServiceName = “PerformancePoint Service”

$DatabaseName = ‘PerformancePoint Service Application’

$spAppPoolName = “PerformancePoint Service Application Pool”

$spAppPoolAcc = $PerformancePointAppPoolAcc

#Get default SQL server

$DefaultDatabaseServer = (Get-SPDatabase | ? { $_.Type -eq “Configuration Database” }).NormalizedDataSource

#Get SQL server instance for the User Profile Application, Social,

$DatabaseServer = $PerformancePointServerInstance

$MachinesToActivate = $PerformancePointMachinesToActivate.Split(“,”).trim()

Write-Host “”

Write-Host “========================================================”

Write-Host “SharePoint 2013 – Module ‘$DefaultAppPoolName’…”

Write-Host “To SharePoint Servers” $MachinesToActivate

Write-Host “Application Pool Name -” $spAppPoolName

Write-Host “Application Pool Account – -” $spAppPoolAcc

Write-Host “Database Server Instance -” $DatabaseServer

Write-Host “Configuration DB -” $ConfigDB

Write-Host “========================================================”

Write-Host “”

Start-SPAssignment -Global | Out-Null

try

{

#Check for existing service application and proxy

$ExistingServiceApp = Get-SPServiceApplication | where-object {$_.Name -eq $ServiceApplicationName}

if ($ExistingServiceApp -eq $null)

{

Write-Host $strTest “- Creating ‘$ServiceApplicationName'”

#Check if managed account already exist, if not exit

$spManagedAccount = Get-SPManagedAccount -Identity $spAppPoolAcc -ErrorAction SilentlyContinue

if ($spManagedAccount -eq $null)

{

Write-Host -f Red $strTest “- ” $ServiceName ” Managed Account – Unable to retrieve managed account” $spAppPoolAcc

exit -1

}

#Check if application pool already exist, if not create it

$ApplicationPool = Get-SPServiceApplicationPool -Identity $spAppPoolName -ErrorAction SilentlyContinue

if ($ApplicationPool -eq $null)

{

New-SPServiceApplicationPool -Name $spAppPoolName -Account $spManagedAccount | Out-Null

}

else

{

Set-SPServiceApplicationPool $ApplicationPool -Account $spManagedAccount | Out-Null

write-host $ApplicationPool

}

New-SPPerformancePointServiceApplication -Name $ServiceApplicationName -ApplicationPool $spAppPoolName -DatabaseServer $DatabaseServer -DatabaseName $DatabaseName | Out-Null

#Create Service Application Proxy

Write-Host $strTest “- Creating ‘$ServiceApplicationName’ proxy”

New-SPPerformancePointServiceApplicationProxy -Default -Name “$ServiceApplicationName Proxy” -ServiceApplication $ServiceApplicationName | Out-Null

#Start service instances

Write-Host $strTest “- Starting service instance”

foreach ($machine in $MachinesToActivate)

{

#Gets the service to determine its status

$service = $(Get-SPServiceInstance | where {$_.TypeName -match $ServiceName} | where {$_.Server -match “SPServer Name=”+$machine})

If ($service.Status -eq “Disabled”)

{

Write-Host -f Green $strTest “- Starting” $service.ID “on $machine”

Start-SPServiceInstance -Identity $service.ID | Out-Null

}

}

Write-Host -f Green $strTest “- Created ‘$ServiceApplicationName’.”

}else{

Write-Host -f Red $strTest” – ServiceApplication ‘$ServiceApplicationName’ already exists.”

#Remove Application?

$RemoveApplication=read-host ‘Do you wish to remove Application? (Y/N)’

if ($RemoveApplication-eq”Y”)

{

write-host $strTest” – Removing ‘$ServiceApplicationName’…”

Remove-SPServiceApplication $ExistingServiceApp -removedata -Confirm:$false

#Proxy is NOT automatically deleted

$ExistingServiceAppProxy = Get-SPServiceApplicationProxy | where-object {$_.Name -eq “$ServiceApplicationName Proxy”}

if ($ExistingServiceAppProxy -ne $null)

{

write-host $strTest “- Removing ‘$ServiceApplicationName proxy’…”

Remove-SPServiceApplicationProxy $ExistingServiceAppProxy -Confirm:$false

}

write-host $strTest “- Stopping service instance…”

Get-SPServiceInstance | where-object {$_.TypeName -eq $ServiceName} | Stop-SPServiceInstance -Confirm:$false | Out-Null

}

}

}

catch { write-Output $_ }

Stop-SPAssignment -Global | Out-Null