Social

tirsdag den 13. januar 2015

Scripting Service Manager Documentation - Views and Request Offerings

No one likes writing documentation. Even less so updating existing documentation. But it is nice to have when you need it. Also don't just write documentation for the sake of documentation. Write it down if you need it later and is not inherently obvious from the code (or whatever). But good documentation practice is a can of worms I am not going to open here.

In the following I will present a script that helps with something as tedious as documenting views in Service Manager. Are you going to do that manually?


Also views change, and you would then have to update your documentation. You really need this script!


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Author: Anders Spælling, spaelling@gmail.com
#
# This script can assist in documenting Service Manager views. Extracts SCSM view information to csv and html
# View description should include what the view is supposed to show (that is not obvious from the view title)
#
# Requirements: SMLets installed

# enter your service manager management server below
$SCSM_MANAGEMENT_SERVER =  "SM1"

# start of script

# remove this module if loaded
$SMModule = Get-Module -Name System.Center.Service.Manager
if($SMModule)
{
    Remove-Module $SMModule
}

Import-Module 'SMLets'
if(-not (Get-Module -Name SMLets))
{
    Write-Host "Unable to import module SMLets"
    Exit
}

# try and get it from global variable - SMLets will look for this if ComputerName is not supplied
$ComputerName = $Global:smdefaultcomputer
if(-not $ComputerName)
{
    $ComputerName = $SCSM_MANAGEMENT_SERVER
}

#get views and folders
$Views = Get-SCSMView -ComputerName $ComputerName
$Folders = Get-SCSMFolder -ComputerName $ComputerName

# target only subclasses of this class
$BaseClass = Get-SCSMClass -ComputerName $ComputerName -Name System.WorkItem$

# store output in list
$Out = @()
# progress counter
$i = 1;
# iterate over all views
foreach($View in $Views)
{

    Write-Progress -Activity "Processing views..." -Status "Processing ($i of $($Views.Count)): `"$($View.DisplayName)`"" -PercentComplete (100*$i/($Views.Count))

    if($View.DisplayName -eq $null -or $View.DisplayName.Length -eq 0)
    {
        # not sure what these are...
        # Write-Host $View.Name, has no displayname
        continue;
    }

    $ManagementPack = $View.GetManagementPack()
    $TargetClass = Get-SCSMClass -ComputerName $ComputerName -Id ($View.Target.Id)
    
    # we only want targetclasses that inherits specific baseclass
    if($TargetClass.IsSubClassOf($BaseClass))
    {
        $ParentFolders = $Folders | ? {$_.Id -in ($View.ParentFolderIds| select -ExpandProperty Guid)} | select -ExpandProperty DisplayName
        # if based on a combination class we want to know which
        # first convert to xml so that we can easily traverse the xml-string
        [xml]$Configuration = [xml]("<xmlroot>$($View.Configuration)</xmlroot>")
        # traverse...
        $Value = $Configuration.xmlroot.Data.ItemsSource.AdvancedListSupportClass.'AdvancedListSupportClass.Parameters'.QueryParameter.Value
        $TypeProjectionName = $Value.Replace('$MPElement[Name=','').Replace(']$','').Replace("'","")
        # if defined in another MP we must remove the alias
        if($TypeProjectionName.Contains('!'))
        {
            $TypeProjectionName = $TypeProjectionName.Split('!')[1]
        }

        # we now have the Id of the type projection
        $TypeProjection = Get-SCSMTypeProjection -ComputerName $ComputerName -Name $TypeProjectionName

        # if based on a basic class
        $TypeProjectionDisplayName = 'N/A'

        if($TypeProjection)
        {

            # typeprojections can share the same name. If more than one is found we use the one with a displayname
            if(([array]$TypeProjection | ? {$_.DisplayName}).Count -gt 0)
            { # select first one with a displayname
                $TypeProjection = [array]$TypeProjection | ? {$_.DisplayName} | select -First 1
            }
            else 
            { # no displaynames, select first 1
                $TypeProjection = [array]$TypeProjection | select -First 1
            }

            $TypeProjectionDisplayName = $TypeProjection.DisplayName

            # if there was no displayname we use the name
            if(-not $TypeProjectionDisplayName)
            {
                $TypeProjectionDisplayName = $TypeProjection.Name
            }
        }

        $Out += New-Object PSObject -Property @{
            Title = $View.DisplayName;
            Description = $View.Description;
            TargetClass = $TargetClass.DisplayName;
            TypeProjection = $TypeProjectionDisplayName;
            ManagementPack = $ManagementPack.DisplayName;
            ParentFolder = $ParentFolders;
            VisibleInUI = $View.Visible;
        };
    }
    
    # update progress counter - used in Write-Progress
    $i++
}

# adding some style to the table
$head = @'
<style>
TABLE{border-width: 1px; border-style: solid; border-color: black;}
TH{border-width: 1px; border-style: solid; border-color: black; padding: 1px;}
TD{border-width: 1px; border-style: solid; border-color: black; padding: 1px;}
</style>
'@
# adding a title
$body = '<H2>Request Offerings</H2>'

# sort using this order
$SortOrder = 'ParentFolder', 'Title'
# select the order of properties (to output)
$Properties = 'Title', 'Description', 'TargetClass', 'TypeProjection', 'ManagementPack', 'ParentFolder', 'VisibleInUI'
# convert to html and csv
$Out | Sort-Object $SortOrder | Select-Object -Property $Properties | ConvertTo-Html -Head $head -Body $body > C:\temp\viewdoc.html
$Out | Sort-Object $SortOrder | Select-Object -Property $Properties | Export-Csv -Path C:\temp\viewdoc.csv -NoTypeInformation

You can download the code from Technet gallery. It also includes code on how to do the same with Request Offerings.

I also found this: http://www.buchatech.com/2015/03/service-manager-discovery-report/ - check it out.

1 kommentar:


  1. Very Useful information Thank you!!
    #TWB_ #Certifications are the #largest #communication #training #certifications in #India today. TWB Certifications™ that teach corporate workforces using GRAPE™ Content Authoring Framework™ have more than 20,000 certified professionals as of today, making TWB Certifications™ arguably the world’s largest technology content certifications. TWB_ Certifications | Technical documentation services

    SvarSlet

Søg i denne blog