Internal/Generic/Convert-FromJsonObject.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#Requires -Version 5.0

function Convert-FromJsonObject {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '', Justification = 'Don''t use ShouldProcess in internal functions.')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Invoke-Expression is used to convert GB-notation to an integer-value.')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'The passwords are coming from a file; conversion is necessary.')]
    param (
        [PSCustomObject]$InputObject,
        [string]$TypeName,
        $RootObject,
        $ParentObject
    )

    if (-not $InputObject) {
        return
    }

    switch ($TypeName) {
        'LabEnvironment' {
            $environment = New-Object LabEnvironment -Property @{
                Name = $InputObject.Name
                Path = $InputObject.Path
                MachinesPath = $InputObject.MachinesPath
                FilesPath = $InputObject.FilesPath
                ConfigurationFilePath = $InputObject.ConfigurationFilePath
                ConfigurationName = $InputObject.ConfigurationName
                CertificateFilePath = $InputObject.CertificateFilePath
                CertificateThumbprint = $InputObject.CertificateThumbprint
                Properties = Convert-PSObjectToHashtable -InputObject $InputObject.Properties
            }
            if ($InputObject.Host) {
                $environment.Host = Convert-FromJsonObject -InputObject $InputObject.Host -TypeName 'LabHost'
            }
            $environment.Hardware = $InputObject.Hardware | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabHardware' -ParentObject $environment }
            $environment.OperatingSystems = $InputObject.OperatingSystems | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabOperationSystem' -ParentObject $environment }
            $environment.Domains = $InputObject.Domains | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabDomain' -ParentObject $environment }
            $environment.Networks = $InputObject.Networks | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabNetwork' -RootObject $environment -ParentObject $environment }
            $environment.Machines = $InputObject.Machines | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabMachine' -RootObject $environment -ParentObject $environment }
            
            return $environment
        }
        'LabHost' {
            $host = New-Object LabHost -Property @{
                Name = $InputObject.Name
            }
            if ($InputObject.Share) {
                $host.Share = Convert-FromJsonObject -InputObject $InputObject.Share -TypeName 'LabHostShare'
            }
            return $host
        }
        'LabHostShare' {
            $hostShare = New-Object LabHostShare -Property @{
                Name = $InputObject.Name
                Path = $InputObject.Path
                UserName = $InputObject.UserName
            }
            if ($InputObject.Password) {
                try {
                    if ($InputObject.PasswordType -eq 'PlainText') {
                        $hostShare.Password = ConvertTo-SecureString -String $InputObject.Password -AsPlainText -Force
                    }
                    else {
                        $hostShare.Password = $InputObject.Password | ConvertTo-SecureString -ErrorAction SilentlyContinue
                    }
                }
                catch {
                    Write-Warning -Message "Error reading the host-share password."
                }
            }
            return $hostShare
        }
        'LabOperationSystem' {
            return New-Object LabOperationSystem -Property @{
                Name = $InputObject.Name
                FilePath = $InputObject.FilePath
                UnattendFilePath = $InputObject.UnattendFilePath
                ProductKey = $InputObject.ProductKey
                Environment = $ParentObject
            }
        }
        'LabDomain' {
            $domain = New-Object LabDomain -Property @{
                Name = $InputObject.Name
                NetbiosName = $InputObject.NetbiosName
                Environment = $ParentObject
            }
            if ($InputObject.AdministratorPassword) {
                try {
                    if ($InputObject.AdministratorPasswordType -eq 'PlainText') {
                        $domain.AdministratorPassword = ConvertTo-SecureString -String $InputObject.AdministratorPassword -AsPlainText -Force
                    }
                    else {
                        $domain.AdministratorPassword = $InputObject.AdministratorPassword | ConvertTo-SecureString -ErrorAction SilentlyContinue
                    }
                }
                catch {
                    Write-Warning -Message "Error reading the domain administrator password."
                }
            }
            return $domain
        }
        'LabDnsServer' {
            $dnsServer = New-Object LabDnsServer -Property @{
                IPAddress = $InputObject.IPAddress
            }
            return $dnsServer
        }
        'LabDhcpServer' {
            $dhcpServer = New-Object LabDhcpServer -Property @{
                IPAddress = $InputObject.IPAddress
                ScopeName = $InputObject.ScopeName
                ScopeId = $InputObject.ScopeId
                StartRange = $InputObject.StartRange
                EndRange = $InputObject.EndRange
                SubnetMask = $InputObject.SubnetMask
                LeaseDurationDays = $InputObject.LeaseDurationDays
                DefaultGateway = $InputObject.DefaultGateway
            }
            return $dhcpServer
        }
        'LabHardware' {
            $hardware = New-Object LabHardware -Property @{
                Name = $InputObject.Name
                ProcessorCount = $InputObject.ProcessorCount
                Environment = $ParentObject
            }
            if ($InputObject.StartupMemory) {
                $hardware.StartupMemory = Invoke-expression -Command $InputObject.StartupMemory
            }
            if ($InputObject.StartupMemory) {
                $hardware.MinimumMemory = Invoke-expression -Command $InputObject.MinimumMemory
            }
            if ($InputObject.StartupMemory) {
                $hardware.MaximumMemory = Invoke-expression -Command $InputObject.MaximumMemory
            }
            return $hardware
        }
        'LabNetwork' {
            $network = New-Object LabNetwork -Property @{
                Name = $InputObject.Name
                SwitchName = $InputObject.SwitchName
                SwitchType = $InputObject.SwitchType
                SwitchNetAdapterName = $InputObject.SwitchNetAdapterName
                AddressFamily = $InputObject.AddressFamily
                PrefixLength = $InputObject.PrefixLength
                HostIPAddress = $InputObject.HostIPAddress
                Environment = $ParentObject
            }
            $network.Domain = ($RootObject.Domains | Where-Object { $_.Name -eq $InputObject.Domain } | Select-Object -First 1)
            if ($InputObject.DnsServer) {
                $network.DnsServer = Convert-FromJsonObject -InputObject $InputObject.DnsServer -TypeName 'LabDnsServer'
            }
            if ($InputObject.DhcpServer) {
                $network.DhcpServer = Convert-FromJsonObject -InputObject $InputObject.DhcpServer -TypeName 'LabDhcpServer'
            }
            return $network
        }
        'LabMachine' {
            $machine = New-Object LabMachine -Property @{
                Name = $InputObject.Name
                TimeZone = $InputObject.TimeZone
                Role = $InputObject.Role
                FilesPath = $InputObject.FilesPath
                Properties = Convert-PSObjectToHashtable -InputObject $InputObject.Properties
                Environment = $ParentObject
            }
            if ($InputObject.AdministratorPassword) {
                try {
                    if ($InputObject.AdministratorPasswordType -eq 'PlainText') {
                        $machine.AdministratorPassword = ConvertTo-SecureString -String $InputObject.AdministratorPassword -AsPlainText -Force
                    }
                    else {
                        $machine.AdministratorPassword = $InputObject.AdministratorPassword | ConvertTo-SecureString -ErrorAction SilentlyContinue
                    }
                }
                catch {
                    Write-Warning -Message "Error reading the machine administrator password."
                }
            }

            $machine.AllProperties = @{}
            if ($RootObject) {
                foreach ($propertyKey in $RootObject.Properties.Keys) {
                    $machine.AllProperties[$propertyKey] = $RootObject.Properties.$propertyKey
                }
            }
            foreach ($propertyKey in $machine.Properties.Keys) {
                $machine.AllProperties[$propertyKey] = $machine.Properties.$propertyKey
            }
            $machine.Hardware = ($RootObject.Hardware | Where-Object { $_.Name -eq $InputObject.Hardware } | Select-Object -First 1)
            $machine.Disks = ($InputObject.Disks | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabDisk' -RootObject $RootObject })
            $machine.NetworkAdapters = ($InputObject.NetworkAdapters | ForEach-Object { Convert-FromJsonObject -InputObject $_ -TypeName 'LabNetworkAdapter' -RootObject $RootObject })
            return $machine
        }
        'LabDisk' {
            $disk = New-Object LabDisk -Property @{
                DifferencingDisk = $InputObject.DifferencingDisk
                UseEnvironmentCopy = $InputObject.UseEnvironmentCopy
                Size = $(if ($InputObject.Size) { Invoke-expression -Command $InputObject.Size })
                Shared = $InputObject.Shared
                ImageFilePath = $InputObject.ImageFilePath
            }

            $disk.OperatingSystem = ($RootObject.OperatingSystems | Where-Object { $_.Name -eq $InputObject.OperatingSystem } | Select-Object -First 1)

            [LabDiskType]$type = [LabDiskType]::HardDisk
            if (-not [LabDiskType]::TryParse($InputObject.Type, [ref]$type)) {
                if ($disk.OperatingSystem) {
                    $type = [LabDiskType]::OperatingSystem
                }
                elseif ($disk.ImageFilePath) {
                    $type = [LabDiskType]::DVDDrive
                }
                else {
                    $type = [LabDiskType]::HardDisk
                }
            }
            $disk.Type = $type

            return $disk
        }
        'LabNetworkAdapter' {
            $networkAdapter = New-Object LabNetworkAdapter -Property @{
                StaticMacAddress = $InputObject.StaticMacAddress
                StaticIPAddress = $InputObject.StaticIPAddress
            }
            $networkAdapter.Network = ($RootObject.Networks | Where-Object { $_.Name -eq $InputObject.Network } | Select-Object -First 1)
            return $networkAdapter
        }
    }
}