SCSM PowerShell: Get List Items | Quisitive
SCSM PowerShell: Get List Items
August 4, 2015
Matthew Dowst
See more

This post is part of a series on Service Manager PowerShell. Be sure to check out the Overview Page for all posts in this series.

In the overview article for this series I stated, with regards to the Service Manager PowerShell Module, “There is almost nothing you can’t do using the provided Cmdlets.” Working with enumeration lists is a reason I said “almost nothing” in that post. This is because, there are no cmdlets in the Service Manager PowerShell Module that are equivalent to the SMLet cmdlets Get-SCSMEnumeration or Get-SCSMChildEnumeration. However, that does not mean you cannot return list item using the built-in PowerShell cmdlets, it just means we have to go about it a different way.

PowerShell is designed to allow you load .NET Framework and COM interfaces, so that you are not limited to tasks that can be performed strictly by cmdlets. Therefore, to return list items you will need to use the Service Manager .NET Framework. To do this you will need to use the PowerShell cmdlet New-Object. Using the New-Object cmdlet, you can load the Service Manager .NET Framework, which will then give you full access to the Service Manager API.

To do this you need to load the Microsoft.EnterpriseManagement.EnterpriseManagementGroup class library. From there you can load all of the enumeration lists into a single variable. Then I will show, how you can then use the standard PowerShell filters and functions to get the exact data you want to return.

In the first example below I am using the New-Object cmdlet to load the Enterprise Management Group class and saving it to the variable EMG. Then using the GetEnumerations method of the EntityTypes property, and saving it to the Enums variables. This will load all of the enumeration items into the Enums variable.

12$EMG = New-Object Microsoft.EnterpriseManagement.EnterpriseManagementGroup “localhost”$Enums = $EMG.EntityTypes.GetEnumerations()

If you notice in the example above localhost is listed at the end of the New-Object cmdlet. This is because the Enterprise Management Group class needs to know the name of your Service Manager Management server. If you are running the above command from the management server, then you can enter localhost. If you are running it from a remote machine, then you need to specify the management server name.

*Service Manager Tip – Refer to MSDN, for more information on Enterprise Management Group class properties and methods.

Now that you have the enumeration items loaded into the Enums variable you can use it to find any list value you want by using a standard PowerShell where-object statements. Using the first example, below, you can display all list items with the word “Closed” in its display name.  You can use the second example to return all top level lists.

12345# Get all list items with Closed in the Display Name$Enums | ?{$_.DisplayName -like “*Closed*”} # Get all enumeration lists$Enums | ?{$_.Parent -eq $null} | sort DisplayName | FT DisplayName, Name -au

Now that you have all of the top level list items, you can use a function to parse through the list and display all items. The function in the example below will do this, and output the results to the screen. It will also include all child items if they exist.

123456789101112131415161718$EMG = New-Object Microsoft.EnterpriseManagement.EnterpriseManagementGroup “localhost”$Enums = $EMG.EntityTypes.GetEnumerations()function getChildren([Microsoft.EnterpriseManagement.Configuration.ManagementPackEnumeration]$parent){    $outPut = “$outPut – ” + $parent.DisplayName    Write-host $outPut    Foreach ($child in $EMG.EntityTypes.GetEnumerations() |?{$_.Parent -eq $parent})    {        getChildren -parent $child -child $true    }}$list = ‘IncidentClassificationEnum’$parent = $Enums |?{$_.Name -eq $list}getChildren $parent

The function above is good for outputting your list items to the screen for reviewing, but does not serve much purpose for other things. So using the same technique as above, the function below will load all of the list items into an array with the Name, Display Name, Ordinal (order), and the Parent item.

12345678910111213141516Function GetEnum($parent){    $collection = @()    $enum = $EMG.EntityTypes.GetEnumerations() |?{$_.Parent -eq $parent}    Foreach($item in $enum)    {        $collection += New-Object PSObject -Property @{Name=$item.Name; DisplayName=$item.DisplayName; Ordinal=$item.Ordinal; Parent=$parent.Name}        GetEnum $item    }    return $collection}$IncClass = GetEnum $($EMG.EntityTypes.GetEnumerations() |?{$_.Name -eq ‘IncidentClassificationEnum’})$IncClass

You can then use this information to get the internal names for list items to use when creating or updating work and configuration items. You will see an example of this in the upcoming post on creating work items. Be sure to check the post for more content in this series.