How to create a KPI using PowerShell

This code is a translation of C# code retrieved July 27, 2010 from http://social.msdn.microsoft.com/forums/en-US/sqlanalysisservices/thread/5be9da70-279d-4fb9-9006-f6fcd1f8dea0/

Though I changed the logic a little:  1) I disconnected the server conditionally, and 2) I removed the reference to the "F" object

PowerShell
Edit|Remove
# 3416_AMOCreateKPI.ps1
# Mark Tabladillo
#
# Last Updated: July 27, 2010
# Program Created: July 27, 2010
#
# How to create a KPI using PowerShell
# This code is a translation of C# code retrieved July 27, 2010 from http://social.msdn.microsoft.com/forums/en-US/sqlanalysisservices/thread/5be9da70-279d-4fb9-9006-f6fcd1f8dea0/
# Though I changed the logic a little:  1) I disconnected the server conditionally, and 2) I removed the reference to the "F" object

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices")
$SSAS_Svr = new-object Microsoft.AnalysisServices.Server
$SSAS_Svr.Connect("Data Source=localhost; Initial Catalog=DB1;Integrated Security=SSPI;")
if ($SSAS_Svr -ne $null -and $SSAS_Svr.Connected)
{
	$DB = $SSAS_Svr.Databases.FindByName("DB1")
	if ($DB -ne $null)
	{
		$Cube = $DB.Cubes.FindByName("Cube1")
		if ($Cube -ne $null)
		{
			$KC = $Cube.Kpis
			$K = $KC.Add("KPI1")
			$K.Value = "[Measures].[Measure1]"
			$K.Goal = "[Measures].[Measure2]*1.2"
			$K.StatusGraphic = "Gauge"
			$K.Status = "case when [Measures].[Measure1]>[Measures].[Measure2]*1.18 then 1 when [Measures].[Measure1]>[Measures].[Measure2]*1.177 then 0.5 when [Measures].[Measure1]>[Measures].[Measure2]*1.173 then 0 when [Measures].[Measure1]>[Measures].[Measure2]*1.17 then -0.5 else -1 end"
			$K.TrendGraphic = "Standard arrow"
			$K.Trend = "case when isempty([Date].[Month].PrevMember) then 0 when [Measures].[Measure1] > ([Date].[Month].PrevMember, [Measures].[Measure1]) then 1 when [Measures].[Measure1] > ([Date].[Month].PrevMember, [Measures].[Measure1])*0.94 then 0 else -1 end"
			$Cube.Update()
		}
	}
	$DB.Process()
}
if ($SSAS_Svr.Connected)
{
	$SSAS_Svr.Disconnect()
}



<#

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.AnalysisServices;

using System.IO;

namespace KPI

{

  class Program

  {

    static void Main(string[] args)

    {

      Server SSAS_Svr = new Server();

      SSAS_Svr.Connect("Data Source=localhost; Initial Catalog=DB1;Integrated Security=SSPI;");

      if (SSAS_Svr != null && SSAS_Svr.Connected)

      {

        Database DB = SSAS_Svr.Databases.FindByName("DB1");

        if (DB != null)

        {

          Cube Cube = DB.Cubes.FindByName("Cube1");

          if (Cube != null)

          {

            KpiCollection KC = Cube.Kpis;

            Kpi K = KC.Add("KPI1");

            K.Value = "[Measures].[Measure1]";

            K.Goal = "[Measures].[Measure2]*1.2";

            K.StatusGraphic = "Gauge";

            K.Status = "case when [Measures].[Measure1]>[Measures].[Measure2]*1.18 then 1 when [Measures].[Measure1]>[Measures].[Measure2]*1.177 then 0.5 when [Measures].[Measure1]>[Measures].[Measure2]*1.173 then 0 when [Measures].[Measure1]>[Measures].[Measure2]*1.17 then -0.5 else -1 end";

            K.TrendGraphic = "Standard arrow";

            K.Trend = "case when isempty([Date].[Month].PrevMember) then 0 when [Measures].[Measure1] > ([Date].[Month].PrevMember, [Measures].[Measure1]) then 1 when [Measures].[Measure1] > ([Date].[Month].PrevMember, [Measures].[Measure1])*0.94 then 0 else -1 end";

            Cube.Update();

          }

        }

        DB.Process();

        SSAS_Svr.Disconnect();

      }

      F.Close();

    }

  }

}

#>