Scripts/Get-SCSMWorkflow.ps1

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
param ( $RuleName = ".*" , [switch]$status )

$asm = [reflection.assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.Core")
if ( ! $asm ) { throw "Could not load SM CORE dll, exiting" }
$EMGTYPE = "microsoft.enterprisemanagement.enterprisemanagementgroup"
if ( $emg -isnot $EMGTYPE ) { $emg = new-object $EMGTYPE localhost }
if ( ! $emg.IsConnected ) { $emg.Reconnect() }
        #?{$_.GetCategories()}|
$rules = $emg.ManagementPacks.GetManagementPacks()|
        %{$_.getrules()}|
        ?{$_.DisplayName -match $ruleName}|
        %{ $_ | add-member NoteProperty ManagementPack $_.GetManagementPack() -pass }
$rules | %{
    if ( $status )
    {
        $emg.Subscription.GetSubscriptionStatusById($_.id) |
            add-member -pass NoteProperty Rule $_
    }
    else
    {
        [bool]$IsVisible = @($_ | %{ $_.GetCategories()|%{ get-scsmenumeration -id $_.value.id}}|?{$_.name -match "WorkflowSubscriptions"}).Count
        $_ | add-member -pass ScriptMethod GetStatus {
            $this.managementgroup.Subscription.GetSubscriptionStatusById($this.id) |
            add-member -pass NoteProperty Rule $this | 
            add-member -pass ScriptMethod GetLog {
                ([xml]$this.output).dataitem.workflowinstances.workflowinstance.trackingrecords.activitytrackingrecord | 
                    %{ 
                        $_.psobject.typenames[0] = "ActivityTrackingRecord"
                        $time = [datetime]$_.EventDateTime
                        $_ | add-member -pass NoteProperty DateTime $time
                    }
                }
            } |
            add-member -pass ScriptMethod GetLog {
                $offset = $args[0]
                write-host -for red "offset: $offset"
                $status = $this.GetStatus()
                if ( "" -ne $offset )
                {
                    [xml]$output = $status[$offset].output
                    $output.dataitem.workflowinstances.workflowinstance.trackingrecords.activitytrackingrecord
                }
                else
                {
                    $status| %{ 
                        [xml]$output = $_.output
                        $output.dataitem.workflowinstances.workflowinstance.trackingrecords.activitytrackingrecord 
                        }
                }
                } |
        add-member -pass NoteProperty IsVisible $IsVisible |
        }
    }