Viewing the Data Model Using PowerShell

Published Feb 15 2019 09:13 AM 291 Views
First published on TECHNET on Nov 09, 2011

Neil Lydick, one of the devs on our team, came up with this handy PowerShell script with a recursive function that displays the data model in a PowerShell window.

  • Blue classes are abstract.
  • White classes are non-abstract (aka “concrete” classes).
  • Red text are the key properties for that class.

The script takes two parameters:

1) The class name you want to start from.

2) The server name to connect to (example: localhost or scsm.contoso.com)

We will also add this to the SMLets CodePlex project .

Just for fun try passing in System.Entity as the class name.  That’s the base class.  See how many classes you come up with. I have 753 in my installation of SCSM 2012.

Here’s the script. Just copy/paste it into a notepad window and save it as PrintClasses.ps1 and then run it as describe above.

$className = $args[0]

$Host.UI.RawUI.buffersize = new-object System.Management.AUtomation.Host.Size(150,9999)
$Host.UI.RawUI.windowsize = new-object System.Management.AUtomation.Host.Size(150,80)

[reflection.assembly]::loadwithpartialname("Microsoft.EnterpriseManagement.Core") | out-null
$emg = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup $args[1]

$selectedClass = $emg.EntityTypes.GetClasses() | ?{ $_.Name -eq "$className" }

$global:countOfClasses = 0;

function PrintClasses($class, $levelString)
{
$keyProperties = @($class.GetProperties(0) | ?{$_.Key -eq $true});
$keyString = "";

if ($keyProperties.Count -gt 0)
{
$keyString = "{" + [string]::Join(",", $keyProperties) + "}"
}

if ($class.Abstract -eq $true)
{
write-host -foregroundcolor cyan $levelString $class.Name " " -nonewline
write-host -foregroundcolor red $keyString
}
else
{
write-host -foregroundcolor white $levelString $class.Name " " -nonewline
write-host -foregroundcolor red $keyString
}

$global:countOfClasses++;

$derivedFromClass = @();
$derivedFromClass += $class.GetDerivedTypes();

foreach ($derivedClass in $derivedFromClass)
{
PrintClasses $derivedClass ($levelString + "  ")
}
}

PrintClasses $selectedClass "  "
write-host "==============================";
write-host "Number of classes: " $global:countOfClasses;

Version history
Last update:
‎Mar 11 2019 08:55 AM
Updated by: