What is PowerCLI :
PowerCLI is a Windows PowerShell interface for managingVMware vSphere. VMware describes PowerCLI as “a powerful command-line tool that lets you automate all aspects of vSphere management, including network, storage, VM, guest OS and more.
1) Get-VM
Get-VM It will produce the inventory of all VMs which are currently found within vCenter . Using the Get-VM cmdlet, and the standard PowerShell Select-Object cmdlet, It will retrieve properties and use Export-CSV to export that data into CSV file .
Get-VM | Select-Object Name,NumCPU,MemoryMB,PowerState,Host | Export-CSV VMs.csv -NoTypeInformation
Get-VM to find snapshots you might have forgotten about in your environment.
Get-VM | Get-Snapshot | format-list | out-file c:\snapshots.txt
2) Move-VM
It will move all the VMs from one host to another for possible maintenance, you can use the Move-VM cmdlet. See the below command for Eg.. it will taking VMs on esxiA and moving them to esxiB.
Get-VMHost esxiA | Get-VM | Move-VM -Destination (Get-VMHost esxiB)
IT will take a VM called MyVM and move it to the esxiA host.
Move-VM -VM 'ProdVM' -Destination 'esxiA'
Use for storage vMotion also . MyVM is the VM name and MyDatastore is your destination datastore. Eg..
Move-VM -VM 'ProdVM' -Datastore 'ProdDatastore'
3) New-VM
It will use New-VM using your information to create a single VM. It will create a blank VM . You can see below for other options to deploy multiple VMs and use templates and customization specifications instead.
New-VM –Name “ProdVM” –VMHost esxiA –ResourcePool Production –DiskGB 30 –DiskStorageFormat Thin –DataStore ProdDatastore –MemoryGB 4 –NetworkName “Production”
It will deploy your VM to the esxiA host, although the ResourcePool parameter accepts ResourcePool, Cluster, vApp, and standalone VMHost objects so you can use any of those.
$myResourcePool = Get-ResourcePool -Name esxiA $mySpecification = Get-OSCustomizationSpec -Name WinUATSpec New-VM -VM OriginalVM -Name NewVM8 -OSCustomizationSpec $mySpecification -ResourcePool $myResourcePool
You cancreate CSV for multiple VMs, like this.
Then after you have the CSV, you can use the New-VM cmdlet below and deploy multiple VMs. For this I typically don’t go crazy as it taxes vCenter if you have a ton of VMs. If you think this is neat, check out the post Andy wrote here. It’s a seriously awesome script and will take it even further.
$vms = Import-CSV C:\Scripts\NewVMs.csv foreach ($vm in $vms){ $Template = Get-Template $vm.template $VMHost = Get-VMHost $vm.host $Datastore = Get-Datastore $vm.datastore $OSCustomization = Get-OSCustomizationSpec $vm.customization New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization ` -Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync }
4) Invoke-VMScript
It will Invoke-VMScript cmdlet will save you the hassle of doing that.
The script below will run a BAT script. In BAT scripts, to access environment variables, you must use the following syntax: %<environment variable>% (for example, %programfiles%).
The outer quotes ($script = ‘…’) are required because this is how you define a string variable in PowerShell. The inner double quotes are required because there are spaces in the path.
$script = '"%programfiles%\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "%tmp%\inforeport"' Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential -ScriptType Bat
5) Get-VMHost
A common issue in virtual environments is NTP. Hosts are out of sync, then the login issue creep up. Performance monitoring quits working, etc. It’s overlooked a lot. You can use Host Profiles if you’ve got Enterprise Plus licensing, but you can also use the command below to modify and change the NTP settings on a host.
Get-VMHost esxA | Add-VMHostNtpServer -NtpServer ntpservername
Another good reason to use Get-VMHost would be to easily find what host a VM is currently running on.
$MyVM = Get-VM -Name ProdVM Get-VMHost -VM $ProdVM
Thanks hope you like it.
Rajiv Pandey.