I wanted to make a post about basic PowerCLI commands/scripts to manage vSphere 6.x. More commands will be added to this post as I go forward.
The Basics
Setting Execution Policy in PowerCLI
1 2 3 4 5 6 7 8 9 |
#Command Set-ExecutionPolicy RemoteSigned #Output Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y |
Connect to a vCenter Server
1 2 3 4 5 6 7 |
#Command Connect-VIServer vcsa-01.vcallaway.com -User administrator@vcallaway.net #Output Name Port User ---- ---- ---- vcsa-01.vcallaway.com 443 VCALLAWAY.NET\Administrator |
Get ESXi Hosts
1 2 3 4 5 6 7 8 9 10 11 |
#Command Get-VMHost #Output Example Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version ---- --------------- ---------- ------ ----------- ----------- ------------- ------------- ------- esxi6-nuc1.vcalla... Connected PoweredOn 4 1117 10364 27.739 31.892 6.0.0 esxi-03.vcallaway... Connected PoweredOn 4 3823 14396 3.393 8.000 6.0.0 esxi-01.vcallaway... Connected PoweredOn 4 200 14396 5.522 16.000 6.0.0 esxi-02.vcallaway... Connected PoweredOn 4 305 14396 5.121 12.000 6.0.0 |
Get Detailed Information on Hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#Command Get-VMHost | fl #Example Output State : Connected ConnectionState : Connected PowerState : PoweredOn VMSwapfileDatastoreId : VMSwapfilePolicy : WithVM ParentId : Folder-group-h4 IsStandalone : True Manufacturer : Model : NumCpu : 4 CpuTotalMhz : 10364 CpuUsageMhz : 1805 LicenseKey : xxxxxxxxxxxxxxxxxxxxxxxxx MemoryTotalMB : 32656.99609375 MemoryTotalGB : 31.891597747802734375 MemoryUsageMB : 28405 MemoryUsageGB : 27.7392578125 ProcessorType : Intel(R) Core(TM) i7-6770HQ CPU @ 2.60GHz HyperthreadingActive : True TimeZone : UTC Version : 6.0.0 Build : 3620759 Parent : host VMSwapfileDatastore : StorageInfo : HostStorageSystem-storageSystem-9 NetworkInfo : ESXi6-NUC1:vcallaway.com DiagnosticPartition : FirewallDefaultPolicy : VMHostFirewallDefaultPolicy:HostSystem-host-9 ApiVersion : 6.0 MaxEVCMode : intel-haswell Name : esxi6-nuc1.vcallaway.com CustomFields : {[AutoDeploy.MachineIdentity, ], [com.vmware.vsan.witnesshoststate, ]} ExtensionData : VMware.Vim.HostSystem Id : HostSystem-host-9 Uid : /VIServer=vcallaway.net\administrator@vcsa-01.vcallaway.com:443/VMHost=HostSystem-host-9/ Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient DatastoreIdList : {Datastore-datastore-10} |
Get VM’s in a vCenter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Command Get-VM #Output Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- vSphere Managemen... PoweredOn 1 0.586 vDP-01 PoweredOn 4 4.000 vRA-01 PoweredOn 4 4.000 PSC-01 PoweredOn 2 2.000 UMDS PoweredOn 2 2.000 VUM PoweredOn 2 2.000 H5 Web Client App... PoweredOn 2 4.000 vcsa-01 PoweredOn 2 8.000 Tiny Linux template PoweredOff 1 0.047 Linux-02 PoweredOff 1 0.500 Linux-01 PoweredOff 1 0.500 |
Get Detailed Information on VMs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Command Get-VM | fl #Example Output Name : vSphere Management Assistant (vMA-01) PowerState : PoweredOn Notes : The vSphere Management Assistant (vMA) allows administrators and developers to run scripts and agents to manage ESX/ESXi and vCenter Server systems. Guest : vSphere Management Assistant (vMA-01):SUSE Linux Enterprise 11 (64-bit) NumCpu : 1 MemoryMB : 600 MemoryGB : 0.5859375 VMHostId : HostSystem-host-9 VMHost : esxi6-nuc1.vcallaway.com . . . |
Power On a VM
1 2 3 4 5 6 7 |
#Command Start-VM "Tiny Linux template" #Output Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Tiny Linux template PoweredOn 1 0.047 |
Power Off a VM
1 2 3 4 5 6 7 8 9 10 11 12 |
#Command Stop-VM "Tiny Linux template" #Output Confirm Are you sure you want to perform this action? Performing the operation "Stop-VM" on target "VM 'Tiny Linux template'". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Tiny Linux template PoweredOff 1 0.047 |
Restart a VM
1 2 3 4 5 6 7 8 9 10 11 12 |
#Command Restart-VM "Tiny Linux template" #Output Confirm Are you sure you want to perform this action? Performing the operation "Restart-VM" on target "VM 'Tiny Linux template'". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Tiny Linux template PoweredOn 1 0.047 |
Generate Diagnostic Log Bundle on ESXi Host or vCenter
1 2 3 4 5 6 7 |
#Command Get-Log -Bundle -DestinationPath C:\vSphere_logs #Example Output Data ---- C:\vSphere_logs\vmsupport-xxxx-xxxx-xxxx-xxxxxxxxxx.tgz |