# /======================================================================= # /= # /= Change-Root-Pwd.ps1 # /= # /= AUTHOR: Jake Bentz # /= DATE: 01/25/2013 # /= # /= REQUIREMENTS: N/A # /= # /= DESCRIPTION: This script changes the root password on all ESX hosts in the esxservers.txt textfile # /= # /= REVISION HISTORY # /= VER DATE AUTHOR/EDITOR COMMENT # /= 1.0 01/25/2013 Jake Bentz Created script # /= # /=======================================================================# # # Create log folder if it doesn't exist $Logpath = "e:\logs\ESX" If ((Test-Path -Path $Logpath) -ne $true) { New-Item -ItemType directory -Path $Logpath} # # Create log file $Logfile = "e:\logs\ESX\ESX_Root"+ (get-date -format '_ddMMMyyyy_hhmm') +".txt" Write "================================================================" > $Logfile Write "Root Password Update Output File" >> $Logfile Write "================================================================" >> $Logfile # # Add VI-toolkit # Add-PSsnapin VMware.VimAutomation.Core Initialize-VIToolkitEnvironment.ps1# Get old root credential $oldrootPassword = Read-Host ìEnter old root passwordî -AsSecureString $oldrootCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist ìrootî,$oldrootPassword # Get new root credential $newrootPassword = Read-Host ìEnter new root passwordî -AsSecureString $newrootCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist ìrootî,$newrootPassword $newrootPassword2 = Read-Host ìRetype new root passwordî -AsSecureString $newrootCredential2 = new-object -typename System.Management.Automation.PSCredential -argumentlist ìrootî,$newrootPassword2 # Compare passwords If ($newrootCredential.GetNetworkCredential().Password -ceq $newrootCredential2.GetNetworkCredential().Password) { # Create new root account object $rootaccount = New-Object VMware.Vim.HostPosixAccountSpec $rootaccount.id = ìrootî $rootaccount.password = $newrootCredential.GetNetworkCredential().Password $rootaccount.shellAccess = ì/bin/bashî # Get list of Host servers from textfile to change root password on Get-Content esxservers.txt | %{ Connect-VIServer $_ -User root -Password $oldrootCredential.GetNetworkCredential().Password -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null If ($ConnectError -ne $Null) { Write-Host ìERROR: Failed to connect to ESX server:î $_ $Output = "ERROR: Failed to connect to ESX server: " $Output = $Output += $_ Write $Output >> $Logfile } Else { $si = Get-View ServiceInstance $acctMgr = Get-View -Id $si.content.accountManager $acctMgr.UpdateUser($rootaccount) Write-Host ìRoot password successfully changed onî $_ $Output = ìRoot password successfully changed on: î $Output = $Output += $_ Write $Output >> $Logfile Disconnect-VIServer * -Confirm:$False | Out-Null } } } Else { Write-Host ìERROR: New root passwords do not match. Exiting...î $Output = ìERROR: New root passwords do not match. Exiting...î Write $Output >> $Logfile }