Powershell API for VCenter
VMware has an interface for VCenter using Powershell that is named PowerCLI.
Network Access:
The Powershell cmdlets for VMware are talking to the web services API running on the VCenter. So if you have network access from the computer to run the VMware vShpere Client, then you should be able to also use the Powershell cmdlets.
Once you have downloaded and installed PowerCLI, to initiate the environment, run the following commands with an account that has access to VCenter (Connect-Server does allow specifying the username/password for a service account)):
[powershell]
cd “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI”
Add-PSSnapin -Name VMware.VimAutomation.Core
.\Scripts\Initialize-PowerCLIEnvironment.ps1
Connect-VIServer -Server <servername>
[/powershell]
Once the environment is setup, all of the cmdlets are loaded and help is available. To view a list of all of the cmdlets, use the following command:
[powershell]
Get-VICommand
[/powershell]
There are additional help docs available at (and online from the link at the top):
C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\VICore Documentation\
To get an array of all VM’s (that the account running the command has permissions to):
[powershell]
$VMs = Get-VM
[/powershell]
Once you have that you do things like retrieve the last performance statistic average for each VM and send it to Out-Gridview (which allows you to sort and filter). This allows you to answer questions like “Which VM’s are using the most CPU?” or “Which VM’s have the longest uptime (VM, not OS)?” or “Which VM’s have the highest Disk I/O?”:
[powershell]
$VMs | get-stat -MaxSamples 1 | Select-Object * | Out-GridView
[/powershell]
Or if you just want a report of all of the VM’s:
[powershell]
$VMs | Select-Object Name, PowerState, NumCpu, MemoryMB, ProvisionedSpaceGB, Folder, Notes, Guest | Out-GridView
[/powershell]
If you want to upgrade the VMware Tools on a batch of machines (in this example, all Engineering License servers named engr-lic-%) without rebooting them, then entire script would be:
[powershell]
cd “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI”
Add-PSSnapin -Name VMware.VimAutomation.Core
.\Scripts\Initialize-PowerCLIEnvironment.ps1
Connect-VIServer -Server <servername>
$VMs = Get-VM
ForEach($VM in $VMs){
if($VM.Name -like “engr-lic-%”){
$VM | Update-Tools -NoReboot
}
}
[/powershell]