-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetESXiPassword.ps1
More file actions
38 lines (37 loc) · 1.52 KB
/
Copy pathSetESXiPassword.ps1
File metadata and controls
38 lines (37 loc) · 1.52 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
Function Set-ESXiPassword {
[CmdletBinding()]
param (
[String]$HostName,
[String]$UserName,
[String]$OldPassword,
[String]$NewPassword
)
try {
$Connection = Connect-VIServer $HostName -User $UserName -Password $OldPassword
}
catch {
switch -wildcard ($error[0].Exception.ToString().ToLower()) {
"*incorrect user*" { Write-Output "Incorrect username or password on host '$HostName'"; break }
"*" { write-output $error[0].Exception.ToString().ToLower(); break }
}
}
try {
$change = Set-VMHostAccount -UserAccount $UserName -Password $NewPassword | out-string
if ($change -like '*root*') {
Write-Output "Success"
}
else {
Write-Output "Failed"
}
Disconnect-Viserver * -confirm:$false
}
catch {
switch -wildcard ($error[0].Exception.ToString().ToLower()) {
"*not currently connected*" { Write-Output "It wasn't possible to connect to '$HostName'"; break }
"*weak password*" { Write-Output "Failed to execute script correctly against Host '$HostName' for the account '$UserName'. It appears the new password did not meet the password complexity requirements on the host."; break }
"*" { write-output $error[0].Exception.ToString().ToLower(); break }
default { Write-Output "Got here" }
}
}
}
Set-ESXiPassword -HostName '[HostName]' -UserName '[UserName]' -OldPassword '[OldPassword]' -NewPassword '[NewPassword]'