VMware Cloud Community
DanTourangeau
Contributor
Contributor
Jump to solution

Read/Modify the Host file of a vCenter with PowerCli

Good day,

I've been searching the web for a way to get the host file from a vCenter but I didn't find anything.
However I did find some scripts to get the Host File for a Host but nothing on vCenters.

 

Even trying to enable SSH with PowerCli I didn't find anything.
Any tips will be appreciated!

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your VCSA normally has the VMware Tools running, so you can use Invoke-VMScript.
The following uses a VICredentialStoreItem to fetch the root credentials for the VCSA, but you can even hardcode them into the script.

$vmName = 'MyVCSA'
$vm = Get-VM -Name $vmName

$viCred = Get-VICredentialStoreItem -Host $vm.Guest.HostName -User root
$cred = New-Object -TypeName PSCredential -ArgumentList $viCred.User,(ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force)

$code = @'
echo $($viCred.Password) | cat /etc/hosts
'@

$sInvoke = @{
  VM = $vm
  ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)
  GuestCredential = $cred
}
Invoke-VMScript @sInvoke


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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Your VCSA normally has the VMware Tools running, so you can use Invoke-VMScript.
The following uses a VICredentialStoreItem to fetch the root credentials for the VCSA, but you can even hardcode them into the script.

$vmName = 'MyVCSA'
$vm = Get-VM -Name $vmName

$viCred = Get-VICredentialStoreItem -Host $vm.Guest.HostName -User root
$cred = New-Object -TypeName PSCredential -ArgumentList $viCred.User,(ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force)

$code = @'
echo $($viCred.Password) | cat /etc/hosts
'@

$sInvoke = @{
  VM = $vm
  ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)
  GuestCredential = $cred
}
Invoke-VMScript @sInvoke


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

Reply
0 Kudos
DanTourangeau
Contributor
Contributor
Jump to solution

Thanks for your script LucD!

Can we also write into that file from PowerCli?

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, but you will need to use something like sed to do that inplace.

You can also transfer the file to your station, modify it and then write it back (Copy-VMGuestFile).


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

Reply
0 Kudos
DanTourangeau
Contributor
Contributor
Jump to solution

I never heard of sed.

I have found this command on one of your older post :

sed '$ a Your new line' /etc/hosts

Where should add this on your code?

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sed is a streaming editor, and a standard tool in Linux environments.

You can provide editor commands in a text file or on the stdin.


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

Tags (1)
Reply
0 Kudos