-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEphingLab.ps1
More file actions
480 lines (437 loc) · 19.4 KB
/
EphingLab.ps1
File metadata and controls
480 lines (437 loc) · 19.4 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
Function Write-EphingLog {
Param (
$Message,
$ErrorMessage
)
if ( [String]::IsNullOrEmpty($ErrorMessage) ) { Write-Host $Message }
else { Write-Host $ErrorMessage }
}
Function Mount-EphingDrive {
Param ($Path)
$DriveLetter=""
$MountedDrive = Mount-VHD -Path $Path -ErrorAction SilentlyContinue -PassThru
$Drives = (Get-Partition -DiskNumber $MountedDrive.DiskNumber).DriveLetter
If ($Drives.Count -gt 1) {
$LargestDrive = 0
For ($d = 0; $d -lt $Drives.Count; $d++) {
If (($Drives[$d] -ne [char]0) -and ((Get-Partition -DriveLetter $Drives[$d]).Size -gt $LargestDrive)) {
$DriveLetter = $Drives[$d]
$LargestDrive = (Get-Partition -DriveLetter $Drives[$d]).Size
}
}
} Else {
$DriveLetter = $Drives
}
return $DriveLetter
}
Function Create-EphingUnattend {
Param (
$ComputerName,
$FilePath,
$WindowsProductKey,
$IPMask,
$IPGateway,
$IPAddress,
$DNSAddress,
$Domain,
$JoinDomainPassword,
$JoinDomainUsername,
$JoinDomainOrganizationalUnitFull,
#$AutoLogon,
$AdministratorPassword,
$InstallerServiceAccount,
$InstallerServiceAccountPassword,
$TimeZone = 'Central Standard Time',
$InstallerServiceAccountDomain,
$InstallerServiceAccountUsername
)
@"
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="specialize">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ComputerName>$ComputerName</ComputerName>
<RegisteredOrganization></RegisteredOrganization>
<RegisteredOwner></RegisteredOwner>
"@ | Out-File $FilePath -Encoding ASCII
If (!([string]::IsNullOrEmpty($WindowsProductKey))) {
@"
<ProductKey>$WindowsProductKey</ProductKey>
"@ | Out-File $FilePath -Append -Encoding ASCII
}
@"
</component>
"@ | Out-File $FilePath -Append -Encoding ASCII
@"
<component name="Microsoft-Windows-TCPIP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Interfaces>
<Interface wcm:action="add">
<Ipv4Settings>
<DhcpEnabled>false</DhcpEnabled>
</Ipv4Settings>
<UnicastIpAddresses>
<IpAddress wcm:action="add" wcm:keyValue="1">$IPAddress/$IPMask</IpAddress>
</UnicastIpAddresses>
<Identifier>Ethernet</Identifier>
<Routes>
<Route wcm:action="add">
<Identifier>1</Identifier>
<Prefix>0.0.0.0/0</Prefix>
<NextHopAddress>$IPGateway</NextHopAddress>
</Route>
</Routes>
</Interface>
</Interfaces>
</component>
<component name="Microsoft-Windows-DNS-Client" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Interfaces>
<Interface wcm:action="add">
<DNSServerSearchOrder>
"@ | Out-File $FilePath -Append -Encoding ASCII
@"
<IpAddress wcm:action="add" wcm:keyValue="1">$DNSAddress</IpAddress>
"@ | Out-File $FilePath -Append -Encoding ASCII
@"
</DNSServerSearchOrder>
<Identifier>Ethernet</Identifier>
</Interface>
</Interfaces>
</component>
"@ | Out-File $FilePath -Append -Encoding ASCII
If (!([string]::IsNullOrEmpty($Domain))) {
$SplitDomain = $Domain.Split('.')
$SplitDomain = $SplitDomain[0]
@"
<component name="Microsoft-Windows-UnattendedJoin" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Identification>
<Credentials>
<Domain>$SplitDomain</Domain>
<Password>$JoinDomainPassword</Password>
<Username>$JoinDomainUsername</Username>
</Credentials>
<JoinDomain>$Domain</JoinDomain>
"@ | Out-File $FilePath -Append -Encoding ASCII
If (!([string]::IsNullOrEmpty($JoinDomainOrganizationalUnitFull))) {
@"
<MachineObjectOU>$JoinDomainOrganizationalUnitFull</MachineObjectOU>
"@ | Out-File $FilePath -Append -Encoding ASCII
}
@"
</Identification>
</component>
"@ | Out-File $FilePath -Append -Encoding ASCII
}
@"
<component name="Microsoft-Windows-TerminalServices-LocalSessionManager" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fDenyTSConnections>false</fDenyTSConnections>
</component>
<component name="Microsoft-Windows-TerminalServices-RDP-WinStationExtensions" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<UserAuthentication>0</UserAuthentication>
</component>
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
"@ | Out-File $FilePath -Append -Encoding ASCII
<#
If (($AutoLogon -eq $true) -and [string]::IsNullOrEmpty($InstallerServiceAccount)) {
@"
<AutoLogon>
<Password>
<Value>$AdministratorPassword</Value>
<PlainText>true</PlainText>
</Password>
<LogonCount>1</LogonCount>
<Username>Administrator</Username>
<Enabled>true</Enabled>
</AutoLogon>
"@ | Out-File $FilePath -Append -Encoding ASCII
}
<#
ElseIf (($AutoLogon -eq $true) -and !([string]::IsNullOrEmpty($InstallerServiceAccount))) {
@"
<AutoLogon>
<Password>
<Value>$InstallerServiceAccountPassword</Value>
<PlainText>true</PlainText>
</Password>
<LogonCount>1</LogonCount>
<Username>$InstallerServiceAccount</Username>
<Enabled>true</Enabled>
</AutoLogon>
"@ | Out-File $FilePath -Append -Encoding ASCII
}
#>
@"
<TimeZone>$TimeZone</TimeZone>
<UserAccounts>
<AdministratorPassword>
<Value>$AdministratorPassword</Value>
<PlainText>true</PlainText>
</AdministratorPassword>
</UserAccounts>
<RegisteredOrganization></RegisteredOrganization>
<RegisteredOwner></RegisteredOwner>
<OOBE>
<HideEULAPage>true</HideEULAPage>
<SkipMachineOOBE>true</SkipMachineOOBE>
</OOBE>
"@ | Out-File $FilePath -Append -Encoding ASCII
@"
</component>
</settings>
</unattend>
"@ | Out-File $FilePath -Append -Encoding ASCII
}
Function Create-EphingSetupComplete {
Param (
$DriveLetter,
$InstallDomain,
$AdministratorPassword,
$StartupScriptName,
$Domain
)
$ScriptName = "C:\$StartupScriptName"
If (!(Test-Path "$DriveLetter`:\Windows\Setup\Scripts")) {New-Item -Path "$DriveLetter`:\Windows\Setup\Scripts" -ItemType Directory | Out-Null}
If (!(Test-Path "$DriveLetter`:\Temp")) {New-Item -Path "$DriveLetter`:\Temp" -ItemType Directory | Out-Null}
@"
@echo off
if exist %SystemDrive%\unattend.xml del %SystemDrive%\unattend.xml
reg add HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell /v ExecutionPolicy /t REG_SZ /d "Unrestricted" /f
ipconfig.exe /registerdns
powershell.exe -command %WinDir%\Setup\Scripts\SetupComplete.ps1
"@ | Out-File "$DriveLetter`:\Windows\Setup\Scripts\SetupComplete.cmd" -Encoding ASCII
$OnSetup = 0
@"
Enable-PSRemoting -Force
Remove-Item "`$env:WinDir\Setup\Scripts\SetupComplete.ps1"
Set-NetFirewallProfile -Profile Domain -Enabled False
Set-NetFirewallProfile -Profile Public -Enabled False
Set-NetFirewallProfile -Profile Private -Enabled False
"@ | Out-File "$DriveLetter`:\Windows\Setup\Scripts\SetupComplete.ps1" -Encoding ASCII
If ($InstallDomain -eq $true) {
$DomainShort = $Domain.Split(".")[0]
@"
Install-WindowsFeature AD-Domain-Services -IncludeManagementTools
`$password = ConvertTo-SecureString -AsPlainText -String "$AdministratorPassword" -Force
Import-Module ADDSDeployment
`$DatabasePath = "C:\Windows\NTDS"
`$LogPath = "C:\Windows\NTDS"
`$SysvolPath = "C:\Windows\SYSVOL"
Set-itemproperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name "ConfigureServer" -Value "shutdown /s /t 5"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultUserName" -Value "Administrator"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultPassword" -Value "$AdministratorPassword"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoAdminLogon" -Value "1"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoLogonCount" -Value "1"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultDomainName" -Value "$Domain"
Install-ADDSForest -DomainName "$Domain" -ForestMode "Win2012" -DomainMode "Win2012" -InstallDns:`$true -SafeModeAdministratorPassword `$password -CreateDnsDelegation:`$false -DomainNetbiosName "$DomainShort" -DatabasePath `$DatabasePath -LogPath `$LogPath -SysvolPath `$SysvolPath -Force:`$true
"@ | Out-File "$DriveLetter`:\Windows\Setup\Scripts\SetupComplete.ps1" -Append -Encoding ASCII
@"
shutdown /s /t 0
"@ | Out-File "$DriveLetter`:\Windows\Setup\Scripts\SetupComplete.cmd" -Encoding ASCII -Append
}
If ($AutoLogon -eq $true) {
@"
Set-itemproperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name "ConfigureServer" -Value ('C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -executionPolicy Unrestricted -File "$ScriptName"')
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultUserName" -Value "Administrator"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultPassword" -Value "$AdministratorPassword"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoAdminLogon" -Value "1"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoLogonCount" -Value "1"
Set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultDomainName" -Value "$Domain"
"@ | Out-File "$DriveLetter`:\Windows\Setup\Scripts\SetupComplete.ps1" -Append -Encoding ASCII
}
}
Function Create-EphingLabVM {
Param (
$VHDPath,
$VHDParentPath,
$VMName,
$VMPath,
$MemoryStartupBytes = 1GB,
$SwitchName,
$Generation = 2,
$ProductKey,
$IPMask = "24",
$IPGateway = "192.168.1.1",
$IPAddress,
$DNSAddress,
$Domain,
$JoinDomainPassword = 'P@ssw0rd',
$JoinDomainUsername = 'Administrator',
$JoinDomainOrganizationalUnitFull,
$AutoLogon = $true,
$AdministratorPassword = 'P@ssw0rd',
$InstallerServiceAccount,
$InstallerServiceAccountPassword,
$InstallerServiceAccountDomain,
$InstallerServiceAccountUsername,
$InstallDomain = $false,
$SetupCompleteDomain,
$Processors,
$StartupScript
)
$NewVHDParams = @{
'Path'="$VHDPath";
'ParentPath'="$VHDParentPath";
'Differencing'=$true;
}
$NewVMParams = @{
'Name' = $VMName;
'Path' = "$VMPath";
'MemoryStartupBytes' = $MemoryStartupBytes;
'SwitchName' = "$SwitchName";
'VHDPath' = (New-VHD @NewVHDParams).Path;
'Generation' = $Generation;
}
New-VM @NewVMParams | Out-Null
Set-VM -Name $VMName -ProcessorCount $Processors -DynamicMemory:$false | Out-Null
$Drive = Mount-EphingDrive -Path $VHDPath
$DomainName = $Domain
if ($IPAddress -eq $DNSAddress) { $DomainName = "" }
$UnattendParams = @{
'ComputerName'="$VMName";
'FilePath'="$Drive`:\Unattend.xml";
'WindowsProductKey'="$ProductKey";
'IPMask'="$IPMask";
'IPGateway'="$IPGateway";
'IPAddress'="$IPAddress";
'DNSAddress'="$DNSAddress";
'Domain'="$DomainName";
'JoinDomainPassword'="$JoinDomainPassword";
'JoinDomainUsername'="$JoinDomainUsername";
'JoinDomainOrganizationalUnitFull'="$JoinDomainOrganizationalUnitFull";
'AutoLogon'=$AutoLogon;
'AdministratorPassword'="$AdministratorPassword";
}
Create-EphingUnattend @UnattendParams
$StartupScriptName = $StartupScript.Split('\')
$StartupScriptName = $StartupScriptName[$StartupScriptName.Length - 1]
$SetupCompleteParams = @{
'DriveLetter'="$Drive";
'InstallDomain'=$InstallDomain;
'AdministratorPassword'="$AdministratorPassword";
'Domain'="$SetupCompleteDomain";
'StartupScriptName'=$StartupScriptName;
}
Create-EphingSetupComplete @SetupCompleteParams
Dismount-VHD -Path $VHDPath
}
Function Create-EphingLab {
Param ( $LabXML )
cls
[xml]$XML = Get-Content $LabXML
$DomainName = $xml.Lab.General.Domain
$AdminPassword = $xml.Lab.General.AdministratorPassword
$Switch = $xml.Lab.General.Switch
$DomainController = $xml.Lab.General.DomainController
$DNSAddress = ""
Foreach ($vm in $xml.Lab.VM) {
If ($DomainController -eq $VM.VMName) { $DNSAddress = $VM.IPAddress }
}
Write-EphingLog -Message "Domain: $DomainName"
Write-EphingLog -Message "Switch: $Switch"
Write-EphingLog -Message "Domain Controller: $DomainController"
Write-EphingLog -Message "Admin Password: $AdminPassword"
Write-EphingLog -Message "DNS Address: $DNSAddress"
If (!(Get-VMSwitch -Name $Switch -ErrorAction SilentlyContinue)) {
Write-Log -Message "Switch $Switch not found - creating..."
New-VMSwitch -Name $Switch -SwitchType Internal
}
Foreach ($vm in $xml.Lab.VM) {
$VMName = $Vm.VMName
If (!(Get-VM -Name $VM.VMName -ErrorAction SilentlyContinue) -and !( Test-Path $VM.VHDPath )) {
[int64]$OneGB = 1073741824
$VMMemory = $VM.Memory
$VMMemory = [int64]$VMMemory.ToUpper().Replace("GB","")
$VMMemory = $VMMemory * $OneGB
$InstallDomain = $false
If ($DomainController -eq $VM.VMName) { $InstallDomain = $true }
$StartupScript = $vm.StartupScript
$AutoLogon = $false
if (!([String]::IsNullOrEmpty($StartupScript))) { $AutoLogon = $true }
$NewVMParams = @{
VHDPath = $VM.VHDPath
VHDParentPath = $VM.VHDParentPath
VMName = $VM.VMName
VMPath = $VM.VMPath
MemoryStartupBytes = $VMMemory
Processors = $VM.Processors
SwitchName = $Switch
Generation = $VM.Generation
ProductKey = $Vm.ProductKey
IPMask = "24"
IPGateway = "192.168.1.1"
IPAddress = $VM.IPAddress
DNSAddress = $DNSAddress
Domain = $DomainName
JoinDomainPassword = $AdminPassword
JoinDomainUsername = "Administrator"
AutoLogon = $AutoLogon
AdministratorPassword = $AdminPassword
InstallDomain = $InstallDomain
SetupCompleteDomain = $DomainName
StartupScript = $VM.StartupScript
}
Write-EphingLog -Message "$VMName - Creating"
Create-EphingLabVM @NewVMParams
Write-EphingLog -Message "$VMName - Copying additional folders if specified"
$Mounted = $false
$Drive = ""
Foreach ($instance in $VM.FolderToCopy) {
if($Mounted -eq $false) {
$Drive = Mount-EphingDrive -Path $VM.VHDPath
$Drive = $Drive + ":\"
$Mounted = $true
}
Copy-Item -Path $instance -Destination $Drive -Recurse
}
$StartScript = $VM.StartupScript
if(!([String]::IsNullOrEmpty($StartScript))) {
Write-EphingLog -Message "$VMName - Setting up startup script "
If ($Mounted -ne $true) {
$Drive = Mount-EphingDrive -Path $VM.VHDPath
$Drive = $Drive + ":\"
$Mounted = $true
}
Copy-Item -Path $StartScript -Destination $Drive
}
If ($Mounted -eq $true) {
Write-EphingLog -Message "$VMName - Dismounting drive"
Dismount-VHD -Path $VM.VHDPath
}
If ($DomainController -eq $VM.VMName) {
Write-EphingLog -Message "$VMName - VM is the DC, starting VM"
Start-VM -Name $DomainController
}
} #if get-name
elseif ( Get-VM -Name $VM.VMName -ErrorAction SilentlyContinue ) {
Write-EphingLog -Message "$VMName - Skipping - VM Exists"
}
else {
Write-EphingLog -Message "$VMName - Skipping - VHD Path " + $VM.VHDPath + " found"
}
}
Write-EphingLog -Message "`n`nFinished! If the Domain Controller was made with this script`nit will be starting now. Once the VM shuts down the domain is`ncreated. Start the DC, wait a few seconds, then start the other VMs!`nThey will domain join and run the specified startup scripts`nYou can exit the script now, or wait and it will turn on your lab once the DC turns off..."
$Count = 0
do {
if ((Get-VM -Name $DomainController).State -ne 'Running') { $count++ }
Start-Sleep 5
} while( $Count -le 1 )
Start-VM -Name $DomainController
Start-Sleep 20
Foreach ($vm in $xml.Lab.VM) {
Start-Vm -Name $VM.VMName -ErrorAction SilentlyContinue
}
}
Function Remove-EphingLab {
Param(
[string]$LabXML
)
[xml]$XML = Get-Content $LabXML
Foreach ($vm in $xml.Lab.VM) {
$VMName = $VM.VMName
$VHDPath = $VM.VHDPath
Remove-VM -Name $VMName -Force
Remove-Item -Path $VHDPath -Force
}
}