Powercli
Powercli
Useful Commands
Create credential store
New-VICredentialStoreItem -Host <vcenter-hostname_or_vcenter-ip> -User "<username>" -Password "<password>"
Update vmTools with no reboot
Get-VM "<vm_name>" | Update-Tools -NoReboot
Create snapshot
new-snapshot -vm <vm_name> -name snapshot_test
Remove snapshot
get-snapshot -vm <vm_name> | remove-snapshot
vMotion
get-vm -Name <vm_name> | move-vm -Destination <hostname_or_host_ip>
Storage vMotion
Get-VM "<vm_name>" | Move-VM -Datastore <datastore_name>
List all virtual machines with snapshots
get-vm | get-snapshot | format-list vm,name
Example output:
VM : webserver01 Name : pre Windows updates VM : ad01 Name : pre disk expansion VM : exchange01 Name : pre database move
List vmx file locations for vm's
Get-View -ViewType VirtualMachine | % { $_.Config.Files.VmPathName }
Example output:
[iscsi-datastore-01] DATABASE/DATABASE.vmx [iscsi-datastore-02] webserver/webserver.vmx [fc-datastore-01] ad01/ad01.vmx [fc-datastore-02] exchange01/exchange01.vmx
Get host boot up time/date
$esxis = Get-VMHost Get-ViEvent -entity $esxis -Start (Get-Date "10:00 10/09/2016") -Finish (Get-Date "15:00 10/09/2016") -MaxSamples ([int]::MaxValue) | Where {$_.FullFormattedMessage -eq "Host has booted."}
Example output:
EventTypeId : esx.audit.host.boot Severity : Message : Arguments : ObjectId : host-155553 ObjectType : HostSystem ObjectName : vsphere-host-01 Fault : Key : 117978286 ChainId : 117978286 CreatedTime : 10/09/2016 13:11:21 UserName : Datacenter : VMware.Vim.DatacenterEventArgument ComputeResource : VMware.Vim.ComputeResourceEventArgument Host : VMware.Vim.HostEventArgument Vm : Ds : Net : Dvs : FullFormattedMessage : Host has booted. ChangeTag : DynamicType : DynamicProperty :
List virtual machines with RDM disks
Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName | fl
Example output:
Parent : webserver01 Name : Hard disk 3 DiskType : RawPhysical ScsiCanonicalName : naa.6019cb61f5397ffc45a0959620009933 DeviceName : vml.02000000006019cb61f5397ffc45a0959620009933313030452d30 Parent : exchange01 Name : Hard disk 2 DiskType : RawPhysical ScsiCanonicalName : naa.68b7b2cc76b0e016966ec56d600540b1 DeviceName : vml.020000000068b7b2cc76b0e016966ec56d600540b1313030452d30
Start virtual machine
start-vm <virtual machine name>
Shutdown virtual machine
shutdown-vmguest <virtual machine name> -confirm:$false
Restart virtual machine
restart-vm <virtual machine name>
Get VM and ESX Events Between Hours
Get just host entries:
$esxs = Get-VMHost $start = Get-Date "12:00 28/09/2015" $finish = Get-Date "18:00 29/09/2015" $events = Get-ViEvent -entity $esxs -Start $start -Finish $finish -MaxSamples ([int]::MaxValue) $events | Select CreatedTime, UserName, ObjectName, EventTypeId, FullFormattedMessage | out-gridview
Get both host and vm entries:
$objs = Get-VMHost $objs += Get-VM $start = Get-Date "12:00" $finish = Get-Date "18:00" $events = Get-ViEvent -entity $esxs -Start $start -Finish $finish -MaxSamples ([int]::MaxValue) $events | Select CreatedTime, ObjectName, EventTypeId, FullFormattedMessage | out-gridview
Rescan HBA/VMFS on all hosts in cluster
Get-Cluster -Name "CLUSTER01" | Get-VMHost | sort-object name | Get-VMHostStorage -RescanAllHba -RescanVmfs | out-null