VMware Cloud Community
nigesh
Contributor
Contributor

how to get host IP address using get-vmhost command and use that to login viserver?

In below code, esx not connecting saying error protocol. I tried passing -protocol HTTPS but no use.

At same code if I give Connect-VIServer -Server 10.0.0.11 -User root -Password $CurrentPw | Out-Null #--> Esx connecting, and password changes as per code.

$CurrentPw = 'abcd@123'

# Get host inventory from vcenter

Connect-VIServer 10.0.0.10

$Host = @()

$Host = Get-VMHost


Disconnect-VIServer 10.0.0.10 -Confirm:$false


# Reset Password for root on each host in the cluster


ForEach ($VMHost in $Host)

{

Connect-VIServer -Server $VMHost -User root -Password $CurrentPw | Out-Null #--> Esx not connecting, error protocol

$NewPw = 'xyz@123'

Write-Output "Password on $VMHost changed to $NewPw"

Add-Content $Logfile ((get-date -Format "dd/MM/yy HH:mm")+","+$VMHost.Name+","+$NewPw+",Success")

Set-VMHostAccount -UserAccount root -password $NewPw

Disconnect-VIServer -Server $VMHost.Name -Confirm:$False

}

I assume some string parameter i'm missing $vmhost, thats only throwing error please correct me.

Also pls give code to get IP instead of host name in Get-VMHost to pass IP for all hosts connected.

Tags (3)
0 Kudos
7 Replies
LucD
Leadership
Leadership

WHat is Get-VMHost returning, a hostname or an IP ?

Is name resolution working ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
cloudteam
Contributor
Contributor

Hi lucd,

Its Get-VMhost is returning Host name only, but it doesn't resolve hostname when same is passed to Connect-VIserver -server $VMhost.

It says error protocol error, if I use a static IP instead of $VMhost its working.

0 Kudos
LucD
Leadership
Leadership

That seems to indicate that your name resolution setup is not working.

You should fix that first


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
cloudteam
Contributor
Contributor

Hi lucd,

Please let me know how to get IP address of all host from get-vmhost and store in a variable x.

Use variable x in for loop and then give iteration wise to connect-viserver as IP.

Please help me in above work around.

0 Kudos
LucD
Leadership
Leadership

Try like this

Get-VMHost |

Select Name,@{N="IP Address";E={($_.ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
nigesh
Contributor
Contributor

Get-VMHost |

Select Name,@{N="IP Address";E={($_.ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}}

The output of above code to get only ip didn't work out, it gave the below output

@{Name=esx02.localhost.com; IP Address=10.0.0.21}

I want to get only 10.0.0.21 as output

0 Kudos
LucD
Leadership
Leadership

Then you could do

$esx = Get-VMHost -Name MyEsx

($esx.ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference