Category: Powercli

Powercli: Host configuration Backup

Powercli: Host configuration Backup

Run the following command when connected to you vcenter via power cli to perform a backup of all your host’s configuration

get-vmhost | get-vmhostfirmware -BackupConfiguration -DestinationPath “C:\Download”


PowerCLI: Set VM Tools Time Sync

PowerCLI: Set VM Tools Time Sync

Please see below for the snippet to enable VM time sync with host time via VMware Tools using PowerCLI

$vm = Get-VM -Name "TEST-VM-01"
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.changeVersion = $vm.ExtensionData.Config.ChangeVersion
$ = New-Object VMware.Vim.ToolsConfigInfo
$ = $true
$_this = Get-View -Id $vm.Id
PowerCLI: Rename VM Inventory Name

PowerCLI: Rename VM Inventory Name

If you have seen my older post about changing the Windows hostname I have also had the challenge of changing the VM names within the inventory to match. Here is the rather simple solution to do this

set-vm Old-VM-NAME=01 -name 'New-VM-Name=01' -confirm:$false
set-vm Old-VM-NAME=02 -name 'New-VM-Name=02' -confirm:$false
set-vm Old-VM-NAME=03 -name 'New-VM-Name=03' -confirm:$false


PowerCLI: Rename Windows Guest OS Hostname

PowerCLI: Rename Windows Guest OS Hostname

Okay, today I had run a script but I had not taken into account the character limit of the Windows hostname. The Result was I had 14 VM’s that are all running with the same hostname and I was not prepared to go into every single VM and change the hostname manually.

So the solution, I had been fishing around the internet for a quick solution to this problem and the solution was to use PowerCli with the Invoke-VMscript command which turns out to be pretty powerful so below is an example of using Invoke-VMscript to change the Windows Hostname

Invoke-VMScript -VM VM-Windows-01 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-01 -Restart' -GuestUser Administrator -GuestPassword ChangeMe
Invoke-VMScript -VM VM-Windows-02 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-02 -Restart' -GuestUser Administrator -GuestPassword ChangeMe
Invoke-VMScript -VM VM-Windows-03 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-03 -Restart' -GuestUser Administrator -GuestPassword ChangeMe


PowerCLI: Refresh Host Networks

PowerCLI: Refresh Host Networks

I have been making automation scripts now for around 3 months and when provisioning VM’s I have found unless you refresh the host’s networking it will not appear. The following snippet resolves this issue for you but be aware that this can considerably slow down your script end to end execution time

Get-View -ViewType HostSystem -Property Name,ConfigManager | %{
(Get-View $_.ConfigManager.NetworkSystem).RefreshNetworkSystem()
Start-Sleep -Seconds 120