-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build Rebuild KDS VM.ps1
259 lines (208 loc) · 11.2 KB
/
Build Rebuild KDS VM.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
Import-Module $env:SyncroModule
$BUILDorREBUILD = "Build"
$KDSVMName = "WSKDSDP13PIZZA"
$GoldImagePath = "D:\Hyper-V\Images\MicrosGoldEnterprise64bitV3.vhdx"
$VirtualDisksPath = "D:\Hyper-V\Virtual Disks\"
$Computername = hostname
$SiteNumber = [int]$Computername.ToUpper().split('P').split('H')[1]
$Timezone = (Get-TimeZone).Id
$Username = "user"
$Password = "user" | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
$Name = $KDSVMName #(Get-VM $KDSVMName).name
$VirtualSwitch = [STRING](Get-VMNetworkAdapter -VM (Get-VM | select -first 1)).SwitchName
$VLAN = (Get-VMNetworkAdapterVLAN -VM (Get-VM | select -first 1)).AccessVlanId
$VirtualDisks = [STRING](get-item 'D:\Hyper-V\Virtual Disks*').FullName
$RAM = 4GB
$VMDiskPath = "$VirtualDisks\$Name.vhdx"
$KDSNames = @(
"WSKDSDP${SiteNumber}EXPO",
"WSKDSDP${SiteNumber}CALL",
"WSKDSDP${SiteNumber}GRILL",
"WSKDSDP${SiteNumber}APP",
"WSKDSDP${SiteNumber}PIZZA",
"WSKDSDP${SiteNumber}SPECIA",
"WSKDSDP${SiteNumber}BBAREX",
"WSKDSDP${SiteNumber}BBAR",
"WSKDSDP${SiteNumber}FBAREX",
"WSKDSDP${SiteNumber}FBAR"
"WSKDSDP${SiteNumber}UBAREX",
"WSKDSDP${SiteNumber}UBAR",
"WSKDSDP${SiteNumber}DBAREX",
"WSKDSDP${SiteNumber}DBAR"
)
$Date = Get-Date -Format "yyyyMMdd_HHmmss"
$LogFileFolder = "C:\CBH-IT\Scripts\Logs\"
$LogFile = $LogFileFolder + "BuildRebuildKDSVM-$Date.txt"
If( !(test-path $LogFileFolder)){Mkdir $LogFileFolder}
Function Write-Log {
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet('INFO','WARN','ERROR')]
[string]$Severity = 'INFO'
)
[pscustomobject]@{
Time = (Get-Date -f g)
Severity = $Severity
Message = $Message
} | Export-Csv -Path $Logfile -Append -NoTypeInformation
}
Function CreateVirtualMachine {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$Name,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path $_})]
[String]$VirtualDisksPath,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path $_})]
[String]$GoldImagePath,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$MemoryStartupBytes,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Int32]$CPUCores,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({Get-VMSwitch -Name $_})]
[String]$VirtualSwitchName
)
Write-Log "Attempting to create Virtual Machine '$Name'" -Severity INFO
# Create Virtual Disk
if (!(Test-Path "$VirtualDisksPath\$Name.vhdx")) {
Write-Host "Creating $Name's Virtual Disk..."
Write-Log "Creating $Name's Virtual Disk..." -Severity INFO
Copy-Item $GoldImagePath -Destination "$VirtualDisksPath\$Name.vhdx"
}
else {
Write-Host -ForegroundColor Yellow "$VirtualDisksPath\$Name.vhdx already exists. Skipping creation."
Write-Log "$VirtualDisksPath\$Name.vhdx already exists. Skipping creation." -Severity WARN
}
# Create Virtual Machine
if(!(get-vm $Name -erroraction silentlycontinue)){
Write-Host "Creating $Name's Virtual Machine..."
Write-Log "Creating $Name's Virtual Machine..." -Severity INFO
New-VM -Name $Name -SwitchName $VirtualSwitchName -VHDPath "$VirtualDisksPath\$Name.vhdx" -Generation 2 -MemoryStartupBytes $MemoryStartupBytes
Set-VMProcessor -VMName $Name -Count $CPUCores
Set-VM -VMName $Name -CheckpointType Disabled -AutomaticStopAction ShutDown
Enable-VMIntegrationService -Name 'Guest Service Interface' -VMName $Name
}
else{
Write-Host -ForegroundColor Yellow "$Name's Virtual Machine already exists. Skipping creation."
Write-Log "$Name's Virtual Machine already exists. Skipping creation." -Severity WARN
}
# Confirm Configuration
If((Get-Item "$VirtualDisksPath\$Name.vhdx") -and (Get-VM $Name)){ Write-Log "Virtual Machine $Name creation completed successfully." -Severity INFO }
If(!(Get-Item "$VirtualDisksPath\$Name.vhdx")){Write-Log "$Name's disk failed to create." -Severity ERROR}
If(!(Get-VM $Name)){Write-Log "$Name's virtual machine failed to create." -Severity ERROR}
}
Function ConfigureVirtualMachine{
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$Name,
[Parameter()]
[ValidateNotNullOrEmpty()]
[IPAddress]$IP
)
$ScriptBlock = {
param($Name,$IP,$SiteSubnet,$SiteNumber,$DefaultGateway,$DNS1,$DNS2,$Timezone)
# Remove existing IP address
$interfaceIndex = (Get-NetIPInterface -AddressFamily IPv4 | Select -First 1).InterfaceIndex
Get-NetIPAddress -InterfaceIndex $interfaceIndex -AddressFamily IPv4 | Remove-NetIPAddress -Confirm:$false -erroraction silentlycontinue
get-NetRoute -InterfaceIndex $interfaceIndex | Remove-NetRoute -Confirm:$false -erroraction silentlycontinue
# Set autologin,timezone,ip address,firewall,disable ipv6,remove temp user
Set-ItemProperty –Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' –Name AutoAdminLogon -Value "1" 2>&1 | out-null
New-ItemProperty –Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' –Name DefaultUserName -Value "user" 2>&1 | out-null
New-ItemProperty –Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' –Name DefaultPassword -Value 'user' 2>&1 | out-null
Set-ItemProperty –Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' –Name DefaultUserName -Value "user" 2>&1 | out-null
Set-ItemProperty –Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' –Name DefaultPassword -Value 'user' 2>&1 | out-null
Set-TimeZone -ID $Timezone 2>&1 | out-null
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private 2>&1 | out-null
netsh advfirewall firewall set rule group="Network Discovery" new enable=Yes 2>&1 | out-null
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False 2>&1 | out-null
Set-ItemProperty -Path REGISTRY::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin -Value 0 2>&1 | out-null
Get-NetAdapterBinding | Where-Object ComponentID -EQ 'ms_tcpip6' | Disable-NetAdapterBinding -ComponentID 'ms_tcpip6' 2>&1 | out-null
New-NetIPAddress -IPAddress ($IP) -DefaultGateway $DefaultGateway -AddressFamily IPv4 -PrefixLength 24 -InterfaceIndex (Get-NetIPInterface -AddressFamily IPv4 | Select -first 1 |select InterfaceIndex).InterfaceIndex | out-null
Set-DnsClientServerAddress -InterfaceIndex (Get-NetIPInterface -AddressFamily IPv4 | Select -first 1 |select InterfaceIndex).InterfaceIndex -ServerAddresses ($DNS1,$DNS2) 2>&1 | out-null
Remove-LocalUser temp -erroraction silentlycontinue
# Write to log file that configuration has been run previously
$Date = Get-Date -Format "yyyyMMdd_HHmmss"
$LogFileFolder = "C:\CBH-IT\Scripts\Logs\"
$LogFile = $LogFileFolder + "CreateAndConfigureVirtualMachines-$Date.txt"
If( !(test-path $LogFileFolder)){Mkdir $LogFileFolder}
"Configured=True" > $LogFile
If ($Name -ne $env:COMPUTERNAME){
Rename-Computer -NewName $Name -Confirm:$False -force 2>&1 | out-null
shutdown /r /t 0
}
}
If((Invoke-Command -VMName $Name -ErrorAction SilentlyContinue -Credential $credential -ScriptBlock{Get-Content C:\CBH-IT\Scripts\Logs\CreateAndConfigureVirtualMachines*}) -eq "Configured=True"){
Write-Host "$Name has already been configured. Skipping."
Write-Log "$Name has already been configured. Skipping." -Severity WARN
}
else{
Write-Host "Configuring $Name's Virtual Machine..."
Write-Log "Configuring $Name's Virtual Machine..." -Severity INFO
Invoke-Command -VMName $Name -ScriptBlock $ScriptBlock -ArgumentList $Name,$IP,$SiteSubnet,$SiteNumber,$DefaultGateway,$DNS1,$DNS2,$Timezone -Credential $credential
}
}
# Script start
Write-Log "Confirming all user-entered variables are valid..." -Severity INFO
Try {
If (!(Test-Path -Path $GoldImagePath)) {Throw "GoldImagePath $GoldImagePath was not found. Please download the file to this folder on the Hyper-V"}
If (!(Test-Path -Path $VirtualDisksPath)) {Throw "VirtualDisksPath $VirtualDisksPath was not found. Please ensure the correct path is entered and the folder exists on the server."}
If ($BUILDorREBUILD -eq "Rebuild" -and !(Get-VM $KDSVMName)) {Throw "VM $KDSVMName was not found. Please ensure the name is correct."}
} Catch {
Write-Host "Variable confirmation failed. Refer to the log for more information - $LogFile" -ForegroundColor Red
Write-Log "Variable confirmation failed. Refer to the log for more information - $LogFile" -Severity ERROR
Write-Log $_ -Severity ERROR
Throw $_
}
# Get IP Info from server nic
If ((Get-NetIPAddress -InterfaceAlias NIC1 -AddressFamily IPv4 -ErrorAction SilentlyContinue).IPAddress){
$SiteIP = ((Get-NetIPAddress -InterfaceAlias NIC1 -AddressFamily IPv4).IPAddress).split('.')[2]
$BrandIP = ((Get-NetIPAddress -InterfaceAlias NIC1 -AddressFamily IPv4).IPAddress).split('.')[1]
}
Else{
$SiteIP = ((Get-NetIPAddress -InterfaceAlias NIC2 -AddressFamily IPv4).IPAddress).split('.')[2]
$BrandIP = ((Get-NetIPAddress -InterfaceAlias NIC2 -AddressFamily IPv4).IPAddress).split('.')[1]
}
# Remove old VM
if($BUILDorREBUILD -eq "Rebuild"){
Get-VM $Name | Stop-VM -Force -Turnoff
Get-VM $Name | Remove-VM -Force
Remove-item $VMDiskPath -force
}
# Build VHD from gold image.
CreateVirtualMachine -Name $Name -MemoryStartupBytes $RAM -CPUCores 4 -VirtualDisksPath $VirtualDisksPath -GoldImagePath $GoldImagePath -VirtualSwitchName $VirtualSwitch
# Wait for VM to start
Write-Log "Virtual Machine creation completed. Moving on to configuration." -Severity INFO
Write-Log "Attempting to start all virtual machines..." -Severity INFO
Get-VM $name | Start-VM
Sleep 60
# Start Configuration
$subnetfull = "10."+$BrandIP+"."+ $siteIP + "."
$Index = $KDSNames.IndexOf($Name)
$IP = $subnetfull+($Index+101)
$DefaultGateway = $subnetfull+"126"
$DNS1 = $subnetfull+"126"
$DNS2 = "1.1.1.1"
ConfigureVirtualMachine -Name $name -IP $IP
# Wait for Syncros Install
Sleep 40
# Install Syncros
Copy-VMFile -VMName $Name -SourcePath "C:\syncroinstaller.exe" -DestinationPath "C:\Temp\syncroinstaller.exe" -CreateFullPath -FileSource Host
Invoke-Command -VMName $Name -ScriptBlock {start c:\Temp\syncroinstaller.exe} -Credential $credential
Display-Alert -Message "Completed Automation [Rebuild Workstation (Simphony)] on $WorkstationRange. Once the devices are displaying the desktop, please run the CAL script in syncros to complete the process and delete the old device out of syncros. The device should appear in syncros under the Unassigned site."