DataGen/Initialize-Service.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
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
param ( [int]$count = 1, [switch]$whatif, [switch]$verbose, [switch]$nobulk )

. ./Common.ps1
$userList     = get-scsmobject (get-scsmclass Microsoft.AD.User$) -filter "LastName -like '%'"
$computerList = get-scsmobject (get-scsmclass System.Computer$) -max 60


$serviceGrouplist = get-scsmclass | ?{$_.getbasetype().name -eq "Microsoft.SystemCenter.ServiceDesigner.ServiceComponentGroup" -and ! $_.abstract}

$BigserviceGroupList = Get-RandomListFromList $serviceGroupList 3
$BigserviceGroupList += Get-RandomListFromList $serviceGroupList 4

$ServiceStatusList = get-scsmchildenumeration -en (get-scsmenumeration System.ServiceManagement.ServiceStatus$)
$ServicePriorityList = get-scsmchildenumeration -en (get-scsmenumeration System.ServiceManagement.ServicePriority$)
$ServiceClassificationList = get-scsmchildenumeration -en (get-scsmenumeration System.ServiceManagement.ServiceClassification$)

$ServiceProjectionType = "Microsoft.System.Service.FormProjectionType"

$ProjectionArgs = @{
    Type     = $ServiceProjectionType 
    whatif   = $whatif 
    verbose  = $verbose 
    bulk     = ! $nobulk
    # passthru must be TRUE
    passthru = $true
    }

$global:services = @(
    1..$count | %{
    $DisplayName = Get-Lorem 2
    Write-Progress -Status "Creating Service" -Activity $DisplayName -perc (($_/$count)*100)

    $Id = [guid]::NewGuid().toString()
    $Seed = @{
        ServiceId            = $Id
        Name                 = $Id
        OwnedByOrganization  = Get-Lorem 2
        Priority             = Get-RandomItemFromList $ServicePriorityList
        Status               = Get-RandomItemFromList $ServiceStatusList
        Classification       = Get-RandomItemFromList $ServiceClassificationList
        AvailabilitySchedule = "Always"
        DisplayName          = $DisplayName
        }
    $projectionHash = @{
        __CLASS = "Microsoft.SystemCenter.BusinessService"
        __OBJECT = $seed
        ServiceHasGroups = $BigServiceGroupList | %{
                $name = $_.name
                $dname = (Get-Lorem 3) + " " + $_.DisplayName
                    @{
                __CLASS = "$name"
                __OBJECT = @{
                    Id           = [guid]::NewGuid().ToString()
                    DisplayName  = $dname
                    Notes        = Get-Lorem 22
                    ObjectStatus = "Active"
                    AssetStatus  = "Deployed"
                    }
                }
            }
        ComponentBusinessCustomers = Get-RandomListFromList $userList 6
        ComponentImpactedByService = Get-RandomListFromList $userList 3
        ComponentServiceContacts   = Get-RandomListFromList $userList 2
        RelatedWorkItem            = get-scsmobject (get-scsmclass ^System.WorkItem.Incident$) -max 10
        # UsedBy = get-scsmobject (get-scsmclass System.ConfigItem) -max 10
        }
    $projectionHash
    } | new-scsmobjectprojection @ProjectionArgs

    )

$containsConfig = get-scsmrelationshipclass System.ConfigItemContainsConfigItem
$relationshipCollection = @()
if ( $whatif )
{
    Write-Host 'What if: Performing operation "New-SCSMRelationshipObject" on Target "IncrementalDiscoveryData"'
}
else
{
    foreach($service in $services)
    {
        foreach($serviceGroup in $service.Item("Target"))
        {
            $source = $serviceGroup.Object
            Get-RandomListFromList $computerList $RANDOM.Next(3,8) | %{
                    $relationshipCollection += new-object psobject -property @{
                    Target = $_
                    Source = $Source
                    Relationship = $containsConfig
                    }
                }
        }
    }
    Write-Progress -Status "Committing Relationships" -Activity "Bulk Operation" -perc 100
    $RelationshipArgs = @{
        whatif  = $whatif 
        verbose = $verbose 
        bulk    = ! $nobulk
        }
    $relationshipCollection | new-scsmrelationshipobject @RelationshipArgs
}