Some Basic #PowerCLI using foreach to modify #NTP and do #ESXi host backups #vExpert


You will have seen my earlier post about learning PowerShell, and now I am using what I think is one of the best things about PowerShell……The foreach loop.

I have seen it used many times, and never fully understood it, I kind of got what it did, but now I am getting it into my head and I have been using it to try and various things.

So I have been doing more bits in PowerCLI to help improve my scripting skills and just to see if I can make my life easier.

Modifying NTP settings on multiple ESXi hosts

#Connects to your vCenter
Connect-VIServer -Server <yourvCenter>

#This variable pulls every host out of the cluster
$VMhosts = Get-Cluster "Aberford Test Cluster" | Get-VMHost

foreach($VMhost in $VMhosts) {

#For every host listed in the cluster, this will remove the current NTP sources that are configured
Remove-VMHostNtpServer -VMHost $VMhost -NtpServer ntp1.old.com, ntp2.old.com, ntp3.old.com, ntp4.old.com -Confirm: $false

#For every host listed in the cluster, this will add the following NTP sources
Add-VMHostNtpServer -VMHost $VMhost -NtpServer ntp1.prod.com, ntp2.prod.com, ntp3.prod.com, ntp4.prod.com 
   
#For every host listed in the cluster, this will set the NTP service to automatic (stop and start with port usage) and it won't ask for confirmation about making the change
Get-VMHostService -VMHost $VMhost | Where-Object -Property Key -EQ ntpd | Set-VMHostService -Policy Automatic -Confirm: $false

#For every host listed in the cluster, this will restart the NTP service (if it is not started it will start it) and it won't ask for confirmation about making the change
Get-VMHostService -VMHost $VMhost | Where-Object -Property Key -EQ ntpd | Restart-VMHostService -Confirm: $false
   
}

#Disconnects from your vCenter
Disconnect-VIServer -Server <yourvCenter>

There is a known bug in PowerCLI 10 where Remove-VMHostNtpServer looks like it has worked, but does not actually do anything. See the below link:

https://communities.vmware.com/thread/590014

Now time for another script

Backing up ESXi Host Configurations

Now both these scripts end up with the same result, take your pick!

#Connects to your vCenter
Connect-VIServer -Server <yourvCenter>

#This works and is very simple, it gets every host in vCenter and exports the configs for each host to a .tgz file
Get-VMHostFirmware -VMHost (get-vmhost) -BackupConfiguration -DestinationPath c:\temp\

#Disconnects from your vCenter
Disconnect-VIServer -Server <yourvCenter>

#Connects to your vCenter
Connect-VIServer -Server <yourvCenter>

#This works too. This cycles through each host and produces a .tgz for each host with its backup config in c:\temp\foreach
$VMhosts = Get-VMHost
foreach ($VMhost in $VMhosts) {
Get-VMHostFirmware -VMHost $VMHost -BackupConfiguration -DestinationPath C:\temp\foreach
}

#Disconnects from your vCenter
Disconnect-VIServer -Server <yourvCenter>

Also please check out the new PowerCLI Cookbook for VSAN here:

https://storagehub.vmware.com/section-assets/powercli-cookbook-for-vsan

It covers things outside of vSAN too and the scripts @jasemccartyhas put together, are commented with info making them easier to follow!


Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.