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!
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
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
Thanks for your script LucD!
Can we also write into that file from PowerCli?
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
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!
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