This script provides my PowerShell translation of C# code created by the authors of Microsoft SQL Server 2008 Analysis Services Unleashed.  I have tested this code on 64-bit Windows 7 running SQL Server 2008 R2 with Analysis Services, and the Windows PowerShell version 2 IDE (Integrated Development Environment).  This code requires the sample “Food Mart 2008” database available from the authors’ website at http://www.informit.com/store/product.aspx?isbn=0672330016

I shared this code as part of my data mining blog.  I have been reviewing this book, and I made comments on this code at http://www.marktab.net/datamining/2010/07/22/adomd-net-with-powershell-2-0-and-c-4-0/

Gorbach, I., Berger, A., & Melomed, E. (2009). Microsoft SQL Server 2008 Analysis Services Unleashed. Indianapolis, IN: Pearson Education Inc.

PowerShell
Edit|Remove
# 3319_AdomdIterateOlapInfo.ps1
# Mark Tabladillo
#
# Last Updated: July 22, 2010
# Program Created: July 22, 2010

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices.AdomdClient")
# [Microsoft.AnalysisServices.AdomdClient.AdomdConnection] 

	
# Open a connection to the server
$con = new-object Microsoft.AnalysisServices.AdomdClient.AdomdConnection
$con.ConnectionString = "Datasource=localhost; Initial Catalog=FoodMart 2008;"
$con.Open()

# Send a simple MDX statement
$command = $con.CreateCommand()
$command.CommandText = "SELECT Measures.Members ON COLUMNS FROM [Warehouse and Sales]"
$cellset = $command.ExecuteCellSet()

# Iterate the data on the cubes used in the request
# In the current version Microsoft SQL Server Analysis Services, only one cube can be used in the request
foreach ($cubeInfo in $cellset.OlapInfo.CubeInfo.Cubes)
{
	write-host "CubeName = " $cubeInfo.CubeName
	write-host "Cube was last updated on " $cubeInfo.LastDataUpdate
}

# Iterate the metadata for the axes 
foreach ($axisInfo in $cellset.OlapInfo.AxesInfo.Axes)
{
	# For each axis, print the name of the axis
	write-host "AxisName = " $axisInfo.Name
	# Iterate the hierarchies on the current axis.
	foreach ($hierarchyInfo in $axisInfo.Hierarchies)
	{
		write-host "HierarchyName = " $hierarchyInfo.Name
		foreach ($hierarchyPropertyInfo in $hierarchyInfo.HierarchyProperties)
		{
			write-host "HierarchyProperty = " $hierarchyPropertyInfo.Name
		}
	}
}

# Iterate the cell properties that are  used in the request
foreach ($cellPropertyInfo in $cellset.OlapInfo.CellInfo.CellProperties)
{
	write-host "CellPropertyName = " $cellPropertyInfo.Name
}

# Close the connection
$con.Close()