-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsNGTInstall.ps1
171 lines (151 loc) · 5.81 KB
/
WindowsNGTInstall.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
###############################################################################
# Version 1.1 created by Mike Rudyi
# NutanixCMDletsPSSnapin required
#
# Usage >WindowsNGTInstall.ps1 -vmname "<vmname or list in file>" -clusterServer "<cluster dns name or ip>" -errorlogpath "<optional paramater to save errors to a file>"
#
# Note: The user running this script must winrm access to the vms that are installing ngt. i.e you should launch this script with a domain account and then provide
# the cluster credentials when prompted.
###############################################################################
param (
[Parameter(Mandatory=$true)][string]$vmname,
[Parameter(Mandatory=$true)][string]$clusterServer,
[bool]$showInfoMessages = $true,
[string]$errorlogpath
)
Add-PSSnapin NutanixCMDletsPSSnapin
$ngtInstallParameters = {
$cdRom = (Get-WmiObject Win32_LogicalDisk -Filter "VolumeName='NUTANIX_TOOLS'").DeviceID
$ntnxsetupPath = Join-Path $cdRom '\setup.exe'
$setupArgs = '/quiet ACCEPTEULA=yes /norestart'
Start-Process -FilePath $ntnxsetupPath -ArgumentList $setupArgs -Wait
}
###############################################################################
#
# Functions
#
###############################################################################
function connectToCluster {
$clusterCreds = Get-Credential -Message "Provide credentials to the $clusterServer cluster in the <user>@yourDomain.org format"
Connect-NutanixCluster -Server $clusterServer -Password $clustercreds.GetNetworkCredential().SecurePassword -UserName $clustercreds.username -AcceptInvalidSSLCerts -ForcedConnection
$clusterCreds = $null
}
function messenger ($message) {
if ($showInfoMessages){
write-output $message
}
}
function errorLogger ($errormsg) {
if ($errorlogpath){
Add-Content -Value "$(Get-Date -Format o) $errormsg" -Path $errorlogpath
write-output $errormsg
}
else {
write-output $errormsg
}
}
function enableNGT ($d){
messenger "Enabling NGT post install."
$Timeout2 = 60
$timer2 = [Diagnostics.Stopwatch]::StartNew()
while (($timer2.Elapsed.TotalSeconds -lt $Timeout2) -and (!($d |Get-NTNXGuestTool).installedVersion)) {
Start-Sleep -Seconds 1
}
if (!($d |Get-NTNXGuestTool).installedVersion) {
$timer2.Stop()
errorLogger "Could not detect NGT status within $timeout seconds on $($d.vmName). Aborting the enabling task. Verify install and enable in Prism."
}
else {
$timer2.Stop()
$d | Set-NTNXGuestTool -Enabled $true
}
}
function installNGT ($b) {
messenger "Waiting for install disk to become available on $($b.vmName)."
$Timeout = 60
$timer = [Diagnostics.Stopwatch]::StartNew()
while (($timer.Elapsed.TotalSeconds -lt $Timeout) -and (!(Invoke-Command -ComputerName $b.vmName -ScriptBlock {(Get-WmiObject Win32_LogicalDisk -Filter "VolumeName='NUTANIX_TOOLS'").DeviceID}))) {
Start-Sleep -Seconds 1
}
if (!(Invoke-Command -ComputerName $b.vmName -ScriptBlock {(Get-WmiObject Win32_LogicalDisk -Filter "VolumeName='NUTANIX_TOOLS'").DeviceID})) {
$timer.Stop()
errorLogger "NGT Disk failed to mount within $timeout seconds. Aborting install on $($b.vmName)."
}
else {
$timer.Stop()
messenger "Installing NGT on $($b.vmName). Please wait."
Invoke-Command -ComputerName $b.vmName -ScriptBlock $ngtInstallParameters
enableNGT $b
}
}
function mountTools ($vmObject) {
if ($vmObject.powerstate -eq "on") {
try{
$vmObject | Mount-NTNXGuestTool
installNGT $vmObject
}
catch {
errorLogger "Could not mount Nutanix guest tools on $($vmObject.vmname). Verify no other image is mounted on the CDRom drive."
}
}
else {
errorLogger "$($vmObject.vmname) appears to be off. Aborting install!"
}
}
function checkNGTMount ($vmguest) {
try {
$vmGuestToolInfo = $vmguest | Get-NTNXGuestTool
if (!$vmGuestToolInfo.installedVersion){
messenger "Nutanix Guest Tools not Installed on $($vmguest.vmName). Locating install disk."
if ($vmGuestToolInfo.toolsMounted) {
installNGT $vmguest
}
else {
messenger "Guest tools disk not mounted on $($vmguest.vmName). Attempting to mount now."
mountTools $vmguest
}
}
else {
messenger "Nutanix guest tools version $($vmGuestoolinfo.installedversion) installed on $($vmguest.vmName). Nothing for me to do here."
}
}
catch {
messenger "Nutanix guest tools not enabled on $($vmguest.vmName). Attempting to enable and mount install disk."
mountTools $vmguest
}
}
function checkVmname ($nameTocheck) {
$namecheck = Get-NTNXVM | Where-Object vmName -eq $nameTocheck
if ($namecheck) {
checkNGTMount $namecheck
}
else {
errorLogger "$nameTocheck could not be found on cluster $clusterServer"
}
}
function listOrNot ($inpt) {
if (Test-Path $inpt){
$vmList = Get-Content -Path $inpt
foreach ($objName in $vmList){
checkVmname $objName
}
}
else {
checkVmname $inpt
}
}
###############################################################################
#
# Program Loop
#
###############################################################################
#Ensures the script doesn't attempt to run against a stale cluster connection
Disconnect-NTNXCluster *
connectToCluster
listOrNot $vmname
###############################################################################
#
# Cleanup
#
###############################################################################
Disconnect-NTNXCluster *