Wednesday, August 28, 2013

VMware PowerShell CLI One-Liners

PowerShell vSphere PowerCLI Information 
  • vspherecli and connect to vcenter by typing 'connect-viserver'  
  • connect-viserver -menu (will list recently connected to vCenter Servers) 

Disconnect from VI-Server 
  • Disconnect-VIServer -Confirm:$false 

Remove VM Host from vSphere 
  • remove-vmhost "servername 

Add VM Host to vSphere 
  • add-vmhost  

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  
Get Cluster Datastore Usage 
  • Get-Cluster "cluster name" |  get-vmhost | 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 

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-content  some-vm-list.txt | foreach-object {get-vm $_} | % { get-view $_.ID }| select name, @{ Name="ToolsStatus"; Expression={$_.guest.toolsstatus}}  
  • get-cluster "clusterName" | get-vmhost | get-vm | where { $_.toolsVersionStatus -notlike "guestToolsCurrent"} |ft -autosize name,version,toolsVersion,ToolsVersionSTatus  
  • get-cluster "MyCluster" |get-vm | % { get-view $_.ID } | select Name, @{ Name="hostName"; Expression={$_.guest.hostName}}, @{ Name="ToolsStatus"; Expression={$_.guest.toolsstatus}}, @{ Name="ToolsVersion"; Expression={$_.config.tools.toolsVersion}} | sort-object name   

Get vCenter Events  
  • get-vievent -username todd -type Error -maxSamples 20 |select FullFormattedMessage,CreatedTime 

How to Create VM from Template with PowerShell CLI 

  • new-vm -name MyNewVM -Template myVMtemplate1 -Location "Afolder" -datastore san001 -vmhost "esx001.mydomain.com"  Name                 PowerState Num CPUs MemoryGB      ----                 ---------- -------- --------      myvm1             PoweredOff 1        4.000          And since I want to reduce the memory to 2gb for this one, I just run this and also add note.  
  • set-vm MyNewVM -memoryGB 2 -notes "Test App Server: Java " -confirm:$false  Name                 PowerState Num CPUs MemoryGB      ----                 ---------- -------- --------      MyNewVM             PoweredOff 1        2.000     Then to verify my information 
    •   get-vm MyNewVM |fl name, host, numcpu, memoryGB, notes   Name     : MyNewVM Host     : esx001mydomain.com NumCpu   : 1 MemoryGB : 2 Notes    : Test App Server: Java 


Find Deleted VM's 
  • $start = (Get-Date).AddDays(-14)  $events = Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) |  where {"VMCreatedEvent","VMRemovedEvent" -contains $_.GetType().Name} |  Select CreatedTime,@{N="VM";E={$_.VM.Name}},UserName 


Get ScsiControllers 
  • get-VM | Select Name,@{N="Controller Type";E={Get-ScsiController -VM $_ | Select -ExpandProperty Type}} 
  • get-vm  

How to view ESX Hosts and CPU/Memory 
  • To see list of ESX Hosts and CPU/Memory run get-vmhost 
    [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> get-vmhost 
     

How to view DataStore Size and Usuage 

  • To see all datastores and show FreeSpace and total capacity run get-datastore 
    [vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> get-datastore 

List or Move VM's on Specific Host 

  • To list all VM's on specific host run   get-vmhost srv0esx003.mydomiain.com | get-vm | select name 

  • To move single VM from one host to another: i.e. from esx003 to esx002  
    • get-vmhost srv0esx003.mydom.com | get-vm -name vmname | move-vm -destination srv0esx001.mydom.com 
    • To move all, just use 'get-vm' without specifiying vm  

  • To move specific VM use:  
    • [vSphere PowerCLI] > get-vmhost srv0esx001.mydom.com | get-vm -name srv0smx00,srv0as44,vmname1,vmname2,vmname3| move-vm -destination srv0esx002.mydom.com 


Get All PropertyTypes of VM 
  • get-vm | get-member -MemberType property 

Reference 

No comments:

Post a Comment