Configurations/PowerShellLab/VMValidate.test.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#requires -version 5.1

#test if VM setup is complete

$LabData = Import-PowerShellDataFile -Path $PSScriptRoot\VMConfigurationData.psd1
$Secure = ConvertTo-SecureString -String "$($labdata.allnodes.labpassword)" -AsPlainText -Force
$Domain = $labdata.allnodes.domainname
$cred = New-Object PSCredential "$Domain\Administrator", $Secure
$wgcred = New-Object PSCredential "administrator", $secure

#The prefix only changes the name of the VM not the guest computername
$prefix = $Labdata.NonNodeData.Lability.EnvironmentPrefix

#set error action preference to suppress all error messsages which would be normal while configurations are converging
#turn off progress bars
$prep = {
    $ProgressPreference = "SilentlyContinue"
    $errorActionPreference = 'SilentlyContinue'
}
#define an array to hold all of the PSSessions
$all = @()

Describe DOM1 {

    $VMName = "$($prefix)DOM1"
    Try {
        $dc = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $dc
        Invoke-Command $prep -Session $dc

        It "[DOM1] Should belong to the $domain domain" {
            $test = Invoke-Command {
                Get-CimInstance -ClassName win32_computersystem -Property domain
            } -Session $DC
            $test.domain | Should Be $domain
        }

        #test for features
        $feat = Invoke-Command { Get-WindowsFeature | Where-Object installed } -Session $dc
        $needed = 'AD-Domain-Services', 'DNS', 'RSAT-AD-Tools',
        'RSAT-AD-PowerShell'
        foreach ($item in $needed) {
            It "[DOM1] Should have feature $item installed" {
                $feat.Name -contains $item | Should Be "True"
            }
        }

        It "[DOM1] Should have an IP address of 192.168.3.10" {
            $i = Invoke-Command -ScriptBlock { Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4 } -Session $dc
            $i.ipv4Address | Should be '192.168.3.10'
        }

        It "[DOM1] Should have a domain name of $domain" {
            $r = Invoke-Command {
                Try {
                    Get-ADDomain -ErrorAction Stop
                }
                Catch {
                    #ignore the error - Domain still spinning up
                }
            } -Session $dc
            $r.dnsroot | Should Be $domain
        }

        $OUs = Invoke-Command {
            Try {
                Get-ADOrganizationalUnit -Filter * -ErrorAction Stop
            }
            Catch {
                #ignore the error - Domain still spinning up
            }
        } -Session $dc
        if ($OUS) {
            $needed = 'IT', 'Dev', 'Marketing', 'Sales', 'Accounting', 'JEA_Operators', 'Servers'
            foreach ($item in $needed) {
                It "[DOM1] Should have organizational unit $item" {
                    $OUs.name -contains $item | Should Be "True"
                }
            }
        } #if ous

        $groups = Invoke-Command {
            Try {
                Get-ADGroup -Filter * -ErrorAction Stop
            }
            Catch {
                #ignore the error - Domain still spinning up
            }
        } -Session $DC

        if ($groups) {
            $target = "IT", "Sales", "Marketing", "Accounting", "JEA Operators"
            foreach ($item in $target) {

                It "[DOM1] Should have a group called $item" {
                    $groups.Name -contains $item | Should Be "True"
                }
            }
        } #if groups

        $users = Invoke-Command {
            Try {
                Get-ADUser -Filter * -ErrorAction Stop
            }
            Catch {
                #ignore the error - Domain still spinning up
            }
        } -Session $dc
        if ($users) {
            It "[DOM1] Should have at least 15 user accounts" {
                $users.count | Should BeGreaterThan 15
            }

            $admins = Invoke-Command { Get-ADGroupMember "Domain Admins"-ErrorAction SilentlyContinue } -Session $dc
            It "[DOM1] ArtD is a member of Domain Admins" {
                $admins.name -contains 'artd' | Should Be True
            }

            It "[DOM1] AprilS is a member of Domain Admins" {
                $admins.name -contains 'aprils' | Should Be True
            }
        } #if users

        $computer = Invoke-Command {
            Try {
                Get-ADComputer -Filter * -ErrorAction SilentlyContinue
            }
            Catch {
                #ignore the error - Domain still spinning up
            }
        } -Session $dc

        if ($Computer) {
            It "[DOM1] Should have a computer account for WIN10" {
                $computer.name -contains "Win10" | Should Be "True"
            }

            It "[DOM1] Should have a computer account for SRV1" {
                $computer.name -contains "SRV1" | Should Be "True"
            }

            It "[DOM1] Should have a computer account for SRV2" {
                $computer.name -contains "SRV2" | Should Be "True"
            }
        } #if computer

        $rec = Invoke-Command { Resolve-DnsName Srv3.company.pri } -Session $DC
        It "[DOM1] Should have a DNS record for SRV3.COMPANY.PRI" {
            $rec.name | Should be 'srv3.company.pri'
            $rec.ipaddress | Should be '192.168.3.60'
        }

        It "[DOM1] Should be running Windows Server 2016" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_operatingsystem -Property caption } -Session $dc
            $test.caption | Should BeLike '*2016*'
        }

  <# It "[DOM1] Should pass Test-DSCConfiguration" {
            Invoke-Command { Test-DscConfiguration -WarningAction SilentlyContinue } -Session $dc | Should Be "True"
        } #>

    }
    Catch {
        It "[DOM1] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #DOM1


Describe SRV1 {

    $VMName = "$($prefix)SRV1"
    Try {
        $srv1 = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $srv1
        Invoke-Command $prep -Session $srv1

        It "[SRV1] Should belong to the $domain domain" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_computersystem -Property domain } -Session $SRV1
            $test.domain | Should Be $domain
        }

        It "[SRV1] Should have an IP address of 192.168.3.50" {
            $i = Invoke-Command -ScriptBlock { Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4 } -Session $SRV1
            $i.ipv4Address | Should be '192.168.3.50'
        }
        $dns = Invoke-Command { Get-DnsClientServerAddress -InterfaceAlias ethernet -AddressFamily IPv4 } -Session $SRV1
        It "[SRV1] Should have a DNS server configuration of 192.168.3.10" {
            $dns.ServerAddresses -contains '192.168.3.10' | Should Be "True"
        }

        It "[SRV1] Should be running Windows Server 2016" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_operatingsystem -Property caption } -Session $srv1
            $test.caption | Should BeLike '*2016*'
        }

<# It "[SRV1] Should pass Test-DSCConfiguration" {
            Invoke-Command { Test-DscConfiguration -WarningAction SilentlyContinue } -Session $srv1 | Should Be "True"
        } #>

    }
    Catch {
        It "[SRV1] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #SRV1

Describe SRV2 {

    $VMName = "$($prefix)SRV2"
    Try {
        $SRV2 = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $srv2
        Invoke-Command $prep -Session $srv2

        It "[SRV2] Should belong to the $domain domain" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_computersystem -Property domain } -Session $SRV2
            $test.domain | Should Be $domain
        }

        It "[SRV2] Should have an IP address of 192.168.3.51" {
            $i = Invoke-Command -ScriptBlock { Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4 } -Session $SRV2
            $i.ipv4Address | Should be '192.168.3.51'
        }
        $dns = Invoke-Command { Get-DnsClientServerAddress -InterfaceAlias ethernet -AddressFamily IPv4 } -Session $SRV2
        It "[SRV2] Should have a DNS server configuration of 192.168.3.10" {
            $dns.ServerAddresses -contains '192.168.3.10' | Should Be "True"
        }

        It "[SRV2] Should have the Web-Server feature installed" {
            $feature = Invoke-Command { Get-WindowsFeature -Name web-server } -Session $SRV2
            $feature.Installed | Should be $True
        }

        It "[SRV2] Should have a sample web service file" {
            $file = Invoke-Command { Get-Item C:\MyWebServices\firstservice.asmx } -Session $SRV2
            $file.name | Should be 'firstservice.asmx'
        }
        It "[SRV2] Should have a WebApplication called MyWebServices" {
            $app = Invoke-Command { Try { Get-WebApplication -Name MyWebServices -ErrorAction stop } Catch {} } -Session $SRV2
            $app.path | Should be "/MyWebServices"
            $app.physicalpath | Should be "c:\MyWebServices"
        }

        It "[SRV2] Should be running Windows Server 2016" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_operatingsystem -Property caption } -Session $srv2
            $test.caption | Should BeLike '*2016*'
        }

<# It "[SRV2] Should pass Test-DSCConfiguration" {
            Invoke-Command { Test-DscConfiguration -WarningAction SilentlyContinue } -Session $srv2 | Should Be "True"
        } #>

    }
    Catch {
        It "[SRV2] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #SRV2

Describe SRV3 {

    $VMName = "$($prefix)SRV3"
    Try {
        $srv3 = New-PSSession -VMName $VMName -Credential $wgCred -ErrorAction Stop
        $all += $srv3
        Invoke-Command $prep -Session $srv3

        It "[SRV3] Should respond to WSMan requests" {
            $srv3.Computername | Should Be $VMName
        }

        It "[SRV3] Should have an IP address of 192.168.3.60" {
            $r = Invoke-Command { Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 } -Session $srv3
            $r.IPv4Address | Should Be '192.168.3.60'
        }

        It "[SRV3] Should belong to a Workgroup" {
            $sys = Invoke-Command { Get-CimInstance Win32_computersystem } -Session $srv3
            $sys.Domain | Should Be "Workgroup"
        }

        It "[SRV3] Should be running Windows Server 2019" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_operatingsystem -Property caption } -Session $srv3
            $test.caption | Should BeLike '*2019*'
        }

<# It "[SRV3] Should pass Test-DSCConfiguration" {
            Invoke-Command { Test-DscConfiguration -WarningAction SilentlyContinue } -Session $srv3 | Should Be "True"
        } #>

    }
    Catch {
        It "[SRV3] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
}

Describe Win10 {

    $VMName = "$($prefix)Win10"
    $rsat =  @(
                'Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0',
                'Rsat.BitLocker.Recovery.Tools~~~~0.0.1.0',
                'Rsat.CertificateServices.Tools~~~~0.0.1.0',
                'Rsat.DHCP.Tools~~~~0.0.1.0',
                'Rsat.Dns.Tools~~~~0.0.1.0',
                'Rsat.FailoverCluster.Management.Tools~~~~0.0.1.0',
                'Rsat.FileServices.Tools~~~~0.0.1.0',
                'Rsat.GroupPolicy.Management.Tools~~~~0.0.1.0',
                'Rsat.IPAM.Client.Tools~~~~0.0.1.0',
                'Rsat.ServerManager.Tools~~~~0.0.1.0'
            )

    Try {
        $cl = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $cl
        Invoke-Command $prep -Session $cl

        It "[WIN10] Should belong to the $Domain domain" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_computersystem -Property domain } -Session $cl
            $test.domain | Should Be $Domain
        }

        It "[WIN10] Should be running Windows 10 Enterprise" {
            $test = Invoke-Command { Get-CimInstance -ClassName win32_operatingsystem -Property version, caption } -Session $cl
            $test.caption | Should BeLike "*Enterprise*"
        }

        It "[Win10] Should have an IP address of 192.168.3.100" {
            $i = Invoke-Command -ScriptBlock { Get-NetIPAddress -InterfaceAlias 'Ethernet' -AddressFamily IPv4 } -Session $cl
            $i.ipv4Address | Should be '192.168.3.100'
        }

        $dns = Invoke-Command { Get-DnsClientServerAddress -InterfaceAlias ethernet -AddressFamily IPv4 } -Session $cl
        It "[Win10] Should have a DNS server configuration of 192.168.3.10" {
            $dns.ServerAddresses -contains '192.168.3.10' | Should Be "True"
        }

        $pkg = Invoke-Command { $using:rsat | foreach-object {Get-WindowsCapability -Online -Name $_}} -Session $cl
        $rsatstatus = "{0}/{1}" -f ($pkg.where({$_.state -eq "installed"}).Name).count,$rsat.count
        It "[Win10] Should have RSAT installed [$rsatStatus]" {
            # write-host ($pkg | Select-object Name,Displayname,State | format-list | Out-String) -ForegroundColor cyan
            $pkg | Where-Object { $_.state -ne "installed" } | Should be $Null
        }

<# It "[Win10] Should pass Test-DSCConfiguration" {
            Invoke-Command { Test-DscConfiguration -WarningAction SilentlyContinue } -Session $cl | Should Be "True"
        } #>

    }
    Catch {
        It "[Win10] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #client

$all | Remove-PSSession