DataGen/Initialize-ComputerProjectionPeripherals.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
param ( [switch]$whatif, [switch]$verbose, [switch]$nuke, [switch]$view , [switch]$raw)
. ./Common.ps1
# Use WMI to get mimic details about the peripherals needed
# by the ComputerProjection
# The list of devices:
# PhysicalComputer
# OperatingSystem
# NetworkAdapter
# Processor
# PhysicalDisk
# LogicalDisk
#

# NUKE - blow away any of the object we'll create
if ( $nuke -or $view )
{
    $remove = "Microsoft.Windows.OperatingSystem",
    "Microsoft.Windows.Peripheral.NetworkAdapter",
    "Microsoft.Windows.Peripheral.Processor",
    "Microsoft.Windows.Peripheral.DiskPartition",
    "Microsoft.Windows.Peripheral.PhysicalDisk",
    "Microsoft.Windows.Peripheral.LogicalDisk" | 
        get-scsmclass | 
        get-scsmobject -filter "PrincipalName -like %Computer%" 
    if ( $view )
    {
        if ( $raw )
        {
            $remove
        }
        else
        {
        $remove | ft TypeName,DisplayName,PrincipalName -au
        }
    }
    else
    {
        if ( $remove ) 
        {
            $remove | remove-scsmobject -force 
        }
    }
    exit
}

trap { write-error $error[0]; exit }

$class  = "Microsoft.Windows.Computer"
$filter = "PrincipalName -like '%Computer%'"
$ARGUMENTS = @{ 
    WhatIf = $whatif 
    Verbose = $verbose
    Bulk = $true
    }

$KV = new-object "System.Collections.Generic.Dictionary[System.String,System.String]"
$KV.Add("Microsoft.Windows.OperatingSystem",          "Get-OperatingSystem")
$KV.Add("Microsoft.Windows.Peripheral.NetworkAdapter","Get-NetworkAdapter")
$KV.Add("Microsoft.Windows.Peripheral.Processor",     "Get-Processor")
$KV.Add("Microsoft.Windows.Peripheral.PhysicalDisk",  "Get-PhysicalDisk")
$KV.Add("Microsoft.Windows.Peripheral.LogicalDisk",   "Get-LogicalDisk")
$KV.Add("Microsoft.Windows.Peripheral.DiskPartition", "Get-Partition")

$global:HASHCOLLECTION = @{}
$KV.Keys |%{
    $key = $_
    $HASHCOLLECTION["$key"] += @(& $KV[$key] $PrincipalName)
}

$computerList = get-scsmobject $class -filter $filter
$HASHCOLLECTION.Keys | %{
    $key = $_
    write-host -for red $key
    $computerList | %{
        $PrincipalName = $_.PrincipalName
        write-host -for green "creating peripherals for $PrincipalName"
        $ARGUMENTS['Name'] = $key
        $HASHCOLLECTION[$key]| %{$_.PrincipalName = $PrincipalName; $_}|new-scsmobject @ARGUMENTS
        } 
}