PowerCLI script examples

Here are some of the examples of PowerCLI scripts that are used to perform some tasks in the vSphere environment, as follows:

  • To move VMs to another host, use the following script:
get-vmhost -name esxi-prod-3.learnvmware.local | get-vm | Move-VM -Destination (Get-VMHost-name esxi-prod-1.learnvmware.local) 
  • To move a single VM to a different host, use the following script:
Move-VM -VM VM_name -Destination esxi-prod-1.learnvmware.local
  • To get information about the VMs, previously, we used the Get-VM command to retrieve a list of running VMs in the vCenter Server instance. You also have the option of exporting the list of VMs in a .csv file, including some properties you want to specify:
Get-VM | Select-Object Name,NumCPU,MemoryMB,PowerState,Host | Export-CSV VMinfo.csv -NoTypeInformation
  • To find out on which host a specific VM runs, use the following script:
Get-VMHost -VM (Get-VM -Name VMname)

  • Configuring NTP: We have already discussed the importance of having the hosts time-synced to avoid authentication issues. The following cmdlet configures the NTP server for the specific host, as well as setting the service to automatically start with the host:
Add-VmHostNtpServer -VMHost $vmhost -NtpServer 172.16.1.1,172.16.1.2
Get-VmHostService -VMHost $vmhost | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Get-VmHostService -VMHost $vmhost | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on"

As you can see, with PowerCLI you can automate everything, and I would strongly suggest getting used to PowerCLI, because it is a powerful tool that can save you a lot of time, especially with repetitive tasks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.221.113