Changing the ESXI host forgotten root password using PowerCli
In my recent experience forgot a few esxi hosts root password. There is multiple methods to reset password. But which is easiest method to reset password without impact.
Please follow the below steps
> Connect to powercli
> Run the command to connect vCenter
Connect-VIServer
> Enter vCenter name or IP details
> Provide the credentials to login.
> Copy the code from below and paste it into Notepad and save it as .PS1 format
> Open the PowerCli and to go the directory where it is saved using cd command
> Run the command . .\Filename.ps1 (Note :There is a space between 2 dots.)
> Function is loaded in local session of PowerCli.
> Now run this command to change the host password
Get-VMHost EsxiName|Set-VMHostPassword -UserName root -Password Vmware123!
--------------------------------------------------------------------------------
function Set-VMHostPassword
{
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[VMware.VimAutomation.ViCore.Impl.V1.VIObjectImpl]$VMHost,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]]$UserName,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]$Password
)
process {
try
{
$validation1=Get-VMHost $VMHost -ErrorAction Stop
}
catch
{
Write-Error -Message "Please check the host is part of connected vCenter or not and try again" -ErrorAction Stop
}
If(($validation1.ConnectionState -eq "Connected") -or ( $validation1.ConnectionState -eq "Maintenance"))
{
$esxcli=Get-EsxCli -VMHost $VMHost -V2
$IDList=$esxcli.system.account.list.invoke().UserID
If(($IDList -contains $UserName) -ne $true){Write-Error -Message "Entered Username does not exist in esxi userid list" -ErrorAction stop}
}
else
{
Write-Error -Message "ESXI is not connected or maintenance mode to perform the action" -ErrorAction Stop
}
$argu=$esxcli.system.account.set.CreateArgs()
$argu.id=$UserName
$argu.password=$Password
$argu.passwordconfirmation=$Password
$output=$esxcli.system.account.set.invoke($argu)
}
end{
If($output -eq $true)
{
Get-VIEvent -Entity (Get-VMHost $VMHost) -MaxSamples 1|?{$_.fullformattedmessage -match "Password"}|select UserLogin,Createdtime,Username,Fullformattedmessage|ft -AutoSize
$hostd=Get-Log -Key hostd -VMHost (Get-VMHost $VMHost)
$hostd.Entries|Select-String "Password was changed for account" |select -Last 1
}
}
}
--------------------------------------------------------------------------------
No comments: