DataGen/Initialize-RequestFromRO.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
param ( $count = 2, [switch]$whatif, [switch]$verbose, $requestOfferingName = "*" )

# in this case, it is count per RequestOffering

Write-Progress -Status Retrieving -Activity "Request Offerings"
$ros = get-scsmrequestoffering -published -displayname $requestOfferingName -verbose:$verbose
$random = new-object system.random
$total = @($ros).count * $count
$current = 0
foreach ( $myRO in $ros )
{
    for($i = 0; $i -lt $count; $i++)
    {
        Write-Progress -Activity $myRo.Title -Status "Creating $i of $count" -perc ($current/$total * 100)
        $current++
        $qs = $myRO| get-scsmrequestofferingquestion
        foreach ( $q in $qs  )
        {
            switch ( $q.SourceControlType )
            {
                "string" 
                {
                    $q.answer = get-lorem ($random.Next(5,15))
                }
                "list"
                {
                    $enumlist = get-scsmchildenumeration -en ( get-scsmenumeration -id $q.ListId)
                    $q.Answer = $enumlist[$random.Next(0,$enumlist.count)]
                }
                "double"
                {
                    $q.Answer = $random.Next(60,100)/17
                }
                "Integer"
                {
                    $q.Answer = $random.Next(1,100);
                }
                "DateTime"
                {
                    $q.Answer = [datetime]::Now.AddDays($random.next(0,30)).AddHours($random.Next(0,24)).AddMinutes($random.next(0,60))
                }
                "Boolean"
                {
                    $q.Answer = [bool]$random.next(0,2)
                }
                "InlineList" 
                { 
                    $allowed = $q.InlineListElements
                    $q.Answer = $allowed[$random.next(0,$allowed.count)]
                }
                "FileAttachment"
                {
                    $tfile = �[io.path]::GetTempFileName().split("\\")[-1]
                    "Temporary data" > $tfile
                    $q.Answer = $tfile
                }
                default 
                {
                    # write-host $q.SourceControlType
                }
            }
        }
        New-SCSMServiceRequest $myRO -Question $qs -verbose:$verbose -whatif:$whatif
        if ( $tfile -and (test-path $tfile) ) { remove-item $tfile }
    }
}