Home Service Manager

Templates & Associated Activities

Using either PowerShell or SQL Server, is there a way to create a list of templates and their associated activities?

Thanks, Chris

Answers

  • Peter_MiklianPeter_Miklian Customer Advanced IT Monkey ✭✭✭

    @Christopher_Montgome you can list all templates using Get-SCSMObjectTemplate PowerShell command.

    You can export the Management Pack containing templates and look into it. Maybe playing with Get-SCSMTypeProjection could lead to related activities? I didn't manage to get this information when I needed it. Maybe someone else from community will help us :)

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭
    edited March 2020

    While I don't have the exact PowerShell you're looking for. I did some modifications to some functions originally provided by @Morten_Meisler that are now part of the SMlets Exchange Connector and have a custom function I don't believe I've previously shared anywhere.


    But @Peter_Miklian is indeed on the right path! You need a Type Projection + a Template in order to walk the template tree. But that means you'd have to first get all of the Type Projections for a specific Class of Template. So the order of operations is something to the effect of:

    1. Get all of the Type Projections for the Service Request class (many type projections)
    2. For each Service Request Type Projection, get the Service Request Templates (many templates)
    3. For each Service Request Template, walk its Activity tree (many Activity templates)


    Parts 1 and 2 above can present a potential PowerShell challenge, but I threw together this SMLets function awhile ago that does just that. This function makes its possible to pass the internal name of a class to then retrieve ALL of the Templates of that class.

    function Get-SCSMTemplateByClassName
    {
        param(
            [parameter(Mandatory=$true, Position=0)]
            [string] $ClassName,
            [parameter(Mandatory=$false, Position=1)]
            [string] $ComputerName = "localhost"
        )
    
        $class = Get-SCSMClass -name $ClassName -computername $ComputerName
        $managementGroup = New-Object Microsoft.EnterpriseManagement.EnterpriseManagementGroup $ComputerName
        $classTPcriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackTypeProjectionCriteria("Type = '$($class.Id)'")
        $classTypeProjections = $managementGroup.EntityTypes.GetTypeProjections($classTPcriteria)
    
        foreach ($classTypeProjection in $classTypeProjections)
        {
            $templateCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackObjectTemplateCriteria("TypeID = '$($classTypeProjection.Id)'")
            $managementGroup.Templates.GetObjectTemplates($templateCriteria)
        }
    }
    
    Get-SCSMTemplateByClassName -ClassName "System.WorkItem.Incident$" -computername "myMgmtServer"
    


    I'll try to get back this, but just wanted to provide some kind of starting block for now.

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭

    Actually. It looks like @Peter_Miklian was closer than he thought -

    $template = Get-SCSMObjectTemplate -DisplayName "My SR Template Name Here"
    $template.ObjectCollection.propertycollection
    

    will return the Activities of that one specific Template. What the function above helps you do is focus that at a specifically class of Templates. So with get all of the Service Request templates and their nested Activities.

    $templates = Get-SCSMTemplateByClassName -ClassName "System.WorkItem.ServiceRequest$" -computername "myMgmtServer"
    foreach ($template in $templates)
    {
        write-output "Service Request: $($template.displayname)"
        write-output "Activities:" $template.ObjectCollection.PropertyCollection
        write-output " "
    }
    


    Output could use some work though 🤔

Sign In or Register to comment.