DataGen/initialize-SoftwareItem.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
param ( $count = 100, [switch]$whatif, [switch]$verbose )

. ./Common.ps1

$SoftwareItem = get-scsmclass System.SoftwareItem
$computerClass = get-scsmclass ^System.Computer$

# Write-Progress -Activity "Querying WMI for Win32_Product" -Status "Get-WMIObject Win32_Product"
# WMI Takes too long to get the data, we'll simplify to use get-lorem
#$items = get-wmiobject win32_product | sort -uniq Name | Select-Object -first $count
#$count = $items.count
#$items | %{ $current = 0 } {
$PublisherList = 1..9 | %{ get-lorem 3; start-sleep -m 15 }
$ProductHash = @{}
$SoftwareItemCollection = @()
while($SoftwareItemCollection.Count -lt $count )
{
    $name = Get-Lorem 3
    $DisplayName = $name.trim(".") + " " + (Get-Lorem 3)
    $Version = Get-RandomVersion
    $Publisher = Get-RandomItemFromList $publisherList
    $key = "${name}${Publisher}${Version}"
    if ( $productHash.ContainsKey($key) )
    {
        continue
    }
    Write-Progress -Activity "Creating SoftwareItem" -Status $Name -Perc ($current++/$count*100)
    $SoftwareItemCollection += @{
        AssetStatus           = "Deployed"
        DisplayName           = $DisplayName
        IsVirtualApplication  = $false
        LocaleID              = 1033
        MajorVersion          = $Version.Major
        MinorVersion          = $Version.Minor
        Notes                 = Get-Lorem 12 -sent
        ObjectStatus          = "Active"
        ProductName           = $Name
        Publisher             = $Publisher
        VersionString         = "$Version"
    }
} 
$softwareItemCollection | new-scsmobject $SoftwareItem -bulk -whatif:$whatif -verbose:$verbose
$softwareList = get-scsmobject $softwareitem
$Relationship = get-scsmrelationshipclass ^System.DeviceHasSoftwareItemInstalled$
$SLcount = [int]($softwarelist.count * 2 / 3) + 1
$DirList = get-childitem $env:programfiles | ?{$_.psiscontainer}|%{$_.fullname}
$global:InstalledSoftwareItems = get-scsmobject $computerClass | %{
    $computer = $_
    Write-Progress -Activity "Creating SoftwareItem Relationship" -Status $computer
    get-randomlistfromlist $SoftwareList $SLCount | %{
        $softwareItem = $_
        new-object psobject -prop @{ 
            Relationship = $relationship
            Target = $softwareItem
            Source = $computer
            Properties = @{
                InstalledPath = Get-RandomItemFromList $DirList
                SerialNumber  = New-SN
                InstalledDate = [datetime]::Now.AddDays(-$RANDOM.Next(30,365))
                }
            }
        }
    }