Configurations/Jason-DSC-Env/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
#requires -version 5.1

#test if VM setup is complete

#The password will be passed by the control script WaitforVM.ps1
#You can manually set it while developing this Pester test
$LabData = Import-PowerShellDataFile -Path $PSScriptRoot\VMConfigurationData.psd1
$Secure = ConvertTo-SecureString -String "$($labdata.allnodes.labpassword)" -AsPlainText -Force
$Domain = "company"
$cred = New-Object PSCredential "Company\Administrator", $Secure

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


$all = @()
Describe DC1 {
    $VMName = "$($prefix)DC1"
    Try {
        $dc = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $dc
        #set error action preference to suppress all error messsages
        Invoke-Command { $errorActionPreference = 'silentlyContinue'} -session $dc

        It "[DC1] Should accept domain admin credential" {
            $dc.Count | Should Be 1
        }

        #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 "[DC1] Should have feature $item installed" {
                $feat.Name -contains $item | Should Be "True"
            }
        }

        It "[DC1] 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 "[DC1] Should have a domain name of $domain" {
            $r = Invoke-Command {
                Try {
                    Get-ADDomain -ErrorAction Stop
                }
                Catch {
                    #ignore error - still getting ready
                }
            } -session $dc
            $r.name | Should Be $domain
        }

        $OUs = Invoke-Command {
            Try {
                Get-ADOrganizationalUnit -filter * -ErrorAction Stop
            }
            Catch {
                #ignore error
            }
        } -session $dc

        $needed = 'IT', 'Dev', 'Marketing', 'Sales', 'Accounting', 'JEA_Operators'
        foreach ($item in $needed) {
            It "[DC1] Should have organizational unit $item" {
                $OUs.name -contains $item | Should Be "True"
            }
        }
        $groups = Invoke-Command {
            Try {
                Get-ADGroup -filter * -ErrorAction Stop
            }
            Catch {
                #ignore error
            }
        } -session $DC
        $target = "IT", "Sales", "Marketing", "Accounting", "JEA Operators"
        foreach ($item in $target) {
            It "[DC1] Should have a group called $item" {
                $groups.Name -contains $item | Should Be "True"
            }
        }

        $users = Invoke-Command {
            Try {
                Get-ADUser -filter * -ErrorAction stop
            }
            Catch {
                #ignore error
            }
        } -session $dc
        It "[DC1] Should have at least 15 user accounts" {
            $users.count | Should BeGreaterThan 15
        }

        $computer = Invoke-Command {
            Try {
                Get-ADComputer -filter * -ErrorAction stop
            }
            catch {
                #ignore error
            }
        } -session $dc
        It "[DC1] Should have a computer account for Client" {
            $computer.name -contains "cli1" | Should Be "True"
        }

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

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

        It "[DC1] Should have a computer account for PullServer" {
            $computer.name -contains "PullServer" | Should Be "True"
        }
    }
    Catch {
        It "[DC1] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #DC

Describe S1 {

    $VMName = "$($prefix)S1"

    Try {
        $s1 = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $s1

        #set error action preference to suppress all error messsages
        Invoke-Command { $errorActionPreference = 'silentlyContinue'} -session $s1
        It "[S1] Should accept domain admin credential" {
            $s1.Count | Should Be 1
        }

        It "[S1] Should have an IP address of 192.168.3.50" {
            $i = Invoke-Command -ScriptBlock { Get-NetIPAddress -interfacealias 'Ethernet' -AddressFamily IPv4} -Session $S1
            $i.ipv4Address | Should be '192.168.3.50'
        }
        $dns = Invoke-Command {Get-DnsClientServerAddress -InterfaceAlias ethernet -AddressFamily IPv4} -session $s1
        It "[S1] Should have a DNS server configuration of 192.168.3.10" {
            $dns.ServerAddresses -contains '192.168.3.10' | Should Be "True"
        }
    }
    Catch {
        It "[S1] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #S1

Describe S2 {

    $VMName = "$($prefix)S2"
    Try {
        $s2 = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $s2
        #set error action preference to suppress all error messsages
        Invoke-Command { $errorActionPreference = 'silentlyContinue'} -session $s2
        It "[S2] Should accept domain admin credential" {
            $s2.Count | Should Be 1
        }

        It "[S2] Should have an IP address of 192.168.3.51" {
            $i = Invoke-Command -ScriptBlock { Get-NetIPAddress -interfacealias 'Ethernet' -AddressFamily IPv4} -Session $S2
            $i.ipv4Address | Should be '192.168.3.51'
        }
        $dns = Invoke-Command {Get-DnsClientServerAddress -InterfaceAlias ethernet -AddressFamily IPv4} -session $s2
        It "[S2] Should have a DNS server configuration of 192.168.3.10" {
            $dns.ServerAddresses -contains '192.168.3.10' | Should Be "True"
        }
    }
    Catch {
        It "[S2] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #S2

Describe PullServer {

    $VMName = "$($prefix)PullServer"
    Try {
        $PullServer = New-PSSession -VMName $VMName -Credential $cred -ErrorAction Stop
        $all += $PullServer
        #set error action preference to suppress all error messsages
        Invoke-Command { $errorActionPreference = 'silentlyContinue'} -session $pullserver
        It "[PullServer] Should accept domain admin credential" {
            $PullServer.Count | Should Be 1
        }

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

Describe Cli1 {

    $VMName = "$($prefix)Cli1"
    $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
        #set error action preference to suppress all error messsages
        Invoke-Command { $errorActionPreference = 'silentlyContinue'} -session $cl
        It "[CLI] Should accept domain admin credential" {
            $cl.Count | Should Be 1
        }

        It "[CLI1] 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'
        }

        $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 "[CLI1] 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
        }

        $dns = Invoke-Command {Get-DnsClientServerAddress -InterfaceAlias ethernet -AddressFamily IPv4} -session $cl
        It "[CLI1] Should have a DNS server configuration of 192.168.3.10" {
            $dns.ServerAddresses -contains '192.168.3.10' | Should Be "True"
        }
    }
    Catch {
        It "[CLI1] Should allow a PSSession but got error: $($_.exception.message)" {
            $false | Should Be $True
        }
    }
} #client

$all | Remove-PSSession