-
Notifications
You must be signed in to change notification settings - Fork 11
/
Install-VMTools.ps1
225 lines (150 loc) · 7.69 KB
/
Install-VMTools.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
<#
.FUNCTIONALITY
This is a VMWare tools install script that re-attempts the install if it finds that the VMWARE tools service has failed to install on the first attempt 1
.SYNOPSIS
- This script can be used as part of automating windows 10/2019 builds via autounattend.xml with packer.io based builds
- The Packer instance requires the VMWare tools service to be running at the end of the build, else, it will fail
- Due to an issue Windows "VMware tools service" failing to install on the first attempt, the code in this script compltes a re-install of the VMWARE tools package
- The below code is mostly based on the script within the following blog post:
- https://scriptech.io/automatically-reinstalling-vmware-tools-on-server2016-after-the-first-attempt-fails-to-install-the-vmtools-service/
.NOTES
Change log
July 24, 2020
- Initial version
Nov 28, 2021
-Added Write-CustomLog function
Nov 30, 2021
-Updated code to ID correct log name
-Added Show-Status function
Feb 13, 2022
-Logging changed to new file, rather than adding to existing WinPackerBuild-date.txt file
.DESCRIPTION
Author [email protected] and Tim from the scriptech.io blog
https://scriptech.io/automatically-reinstalling-vmware-tools-on-server2016-after-the-first-attempt-fails-to-install-the-vmtools-service/
.EXAMPLE
./Install-VMTools.ps1
.NOTES
.Link
https://scriptech.io/automatically-reinstalling-vmware-tools-on-server2016-after-the-first-attempt-fails-to-install-the-vmtools-service/
https://github.com/getvpro/Build-Packer
#>
Function Get-VMToolsInstalled {
IF (((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") | Where-Object { $_.GetValue( "DisplayName" ) -like "*VMware Tools*" } ).Length -gt 0) {
[int]$Version = "32"
}
IF (((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") | Where-Object { $_.GetValue( "DisplayName" ) -like "*VMware Tools*" } ).Length -gt 0) {
[int]$Version = "64"
}
return $Version
}
Function Write-CustomLog {
Param(
[String]$ScriptLog,
[String]$Message,
[String]$Level
)
switch ($Level) {
'Error'
{
$LevelText = 'ERROR:'
$Message = "$(Get-Date): $LevelText Ran from $Env:computername by $($Env:Username): $Message"
Write-host $Message -ForegroundColor RED
}
'Warn'
{
$LevelText = 'WARNING:'
$Message = "$(Get-Date): $LevelText Ran from $Env:computername by $($Env:Username): $Message"
Write-host $Message -ForegroundColor YELLOW
}
'Info'
{
$LevelText = 'INFO:'
$Message = "$(Get-Date): $LevelText Ran from $Env:computername by $($Env:Username): $Message"
Write-host $Message -ForegroundColor GREEN
}
}
Add-content -value "$Message" -Path $ScriptLog
}
Function Show-Status {
import-module PSADT -Force
Show-InstallationProgress -StatusMessage "VMware tools is installed `n
The remote packer instance will power off the VM to complete phase 1 of the build process `n
The PowerCLI script running on the same machine running packer will then power the VM back on `n
With the VM powered back on, phase 2 will start, where a custom Windows Update task / script will be downloaded/imported/ran to fully patch the system"
Start-Sleep -Seconds 5
Close-InstallationProgress
}
### End functions
### Create directory structure as required
If (-not(test-path c:\admin -ErrorAction SilentlyContinue)) {
new-item -ItemType Directory -Path "c:\Admin\Scripts"
new-item -ItemType Directory -Path "C:\Admin\Build"
new-item -ItemType Directory -Path "C:\Admin\Language Pack"
}
$LogTimeStamp = (Get-Date).ToString('MM-dd-yyyy-hhmm-tt')
$ScriptLog = "c:\Admin\Build\WinPackerBuild-VMwareTools-$LogTimeStamp.txt"
### 1 - Set the current working directory to whichever drive corresponds to the mounted VMWare Tools installation ISO
Set-Location e:
### 2 - Install attempt #1
Write-CustomLog -ScriptLog $ScriptLog -Message "Starting VMware tools install first attempt 1" -Level INFO
Start-Process "setup64.exe" -ArgumentList '/s /v "/qb REBOOT=R"' -Wait
### 3 - After the installation is finished, check to see if the 'VMTools' service enters the 'Running' state every 2 seconds for 10 seconds
$Running = $false
$iRepeat = 0
while (-not$Running -and $iRepeat -lt 5) {
write-host "Pause for 2 seconds to check running state on VMware tools service" -ForegroundColor cyan
Start-Sleep -s 2
$Service = Get-Service "VMTools" -ErrorAction SilentlyContinue
$Servicestatus = $Service.Status
if ($ServiceStatus -notlike "Running") {
$iRepeat++
}
else {
$Running = $true
Write-CustomLog -ScriptLog $ScriptLog -Message "VMware tools service found to be running state after first install attempt" -Level INFO
}
}
### 4 - If the service never enters the 'Running' state, re-install VMWare Tools
if (-not$Running) {
#Uninstall VMWare Tools
Write-CustomLog -ScriptLog $ScriptLog -Message "Running un-install on first attempt of VMware tools install" -Level WARN
IF (Get-VMToolsInstalled -eq "32") {
$GUID = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -Like '*VMWARE Tools*' }).PSChildName
}
Else {
$GUID = (Get-ItemProperty HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -Like '*VMWARE Tools*' }).PSChildName
}
### 5 - Un-install VMWARe tools based on 32-bit/64-bit install GUIDs captured via Get-VMToolsIsInstalled function
Start-Process -FilePath msiexec.exe -ArgumentList "/X $GUID /quiet /norestart" -Wait
Write-CustomLog -ScriptLog $ScriptLog -Message "Running re-install of VMware tools install" -Level INFO
#Install VMWare Tools
Start-Process "setup64.exe" -ArgumentList '/s /v "/qb REBOOT=R"' -Wait
### 6 - Re-check again if VMTools service has been installed and is started
Write-CustomLog -ScriptLog $ScriptLog -Message "Re-checking if VMTools service has been installed and is started" -Level INFO
$iRepeat = 0
while (-not$Running -and $iRepeat -lt 5) {
Start-Sleep -s 2
$Service = Get-Service "VMTools" -ErrorAction SilentlyContinue
$ServiceStatus = $Service.Status
If ($ServiceStatus -notlike "Running") {
$iRepeat++
}
Else {
$Running = $true
Write-CustomLog -ScriptLog $ScriptLog -Message "VMware tools service found to be running state after SECOND install attempt" -Level INFO
Show-Status
}
}
### 7 If after the reinstall, the service is still not running, this is a failed deployment
IF (-not$Running) {
Write-CustomLog -ScriptLog $ScriptLog -Message "VMWare Tools is still not installed correctly. The packer automated deployment will not process any further until VMWare Tools is installed" -Level ERROR
Show-InstallationProgress -StatusMessage "VMWare Tools is still NOT installed correctly `n
The packer automated deployment will not process any further until the VMTools Windows service is started, check under services.msc `n
Please troubleshoot `n
This progress window will remain up for 5 minutes, then auto-close `n
If you want to close this progress window now, run 'close-InstallationProgress from the parent Powershell process"
Start-Sleep -Seconds 300
Close-InstallationProgress
EXIT
}
}