Friday, August 16, 2013

VMware PowerShell One-Liners

Some basic command for PowerShell and Vsphere

  • vspherecli and connect to vcenter by typing 'connect-viserver'  


Get list of VM's  by Name & OS 
  • get-vm | foreach-object { get-vmguest $_ | select VMname, OSFullname} 
  • get-vm | foreach-object { get-vmguest $_ | where {$_.OSFullName -notlike "*Windows*"}} | select VMName, OSFullname 

Search for VMhost with Parent Name 
  • S H:\> get-vmhost | where { $_.parent -match "RedHat" } | select Name, Parent  
Show Datastore for single Host 
  • get-vmhost vmhost1.mydom.com | get-datastore 

View Stats 
  • Get-StatType -Entity (Get-VM $vmguest/host) 

Unmount CD or ISO 
  • Get-VM <filters> | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$false 
  • get-cluster "clusterName"|get-vmhost | get-vm | get-cddrive |set-cdDrive -NoMedia -confirm:$false 


Get Number of Virtual CPU on all VMS

  • get-vmhost | where { $_.parent -match "Cluster-Name" } | get-vm |select-object -property name,numcpu 


Check VMware Tools  
  • Add viProperty 
    • New-VIProperty -Name ToolsVersionStatus -ObjectType VirtualMachine `     -ValueFromExtensionProperty 'Guest.ToolsVersionStatus' `     -Force 
    • New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine `     -ValueFromExtensionProperty 'Config.tools.ToolsVersion' `     -Force        
  • get-cluster "clusterName" | get-vmhost | get-vm | where { $_.toolsVersionStatus -notlike "guestToolsCurrent"} |ft -autosize name,version,toolsVersion,ToolsVersionSTatus