This Blog explains how can we start working with power cli commands to reduce our efforts on configuration and management of VMware infrastructure .
PowerCLI is the best tool for automating management and configuration of VMware vSphere .
Start with understanding the commands of vSphere PowerCLI
1. Install vSphere Power cli on your machine .
2. Run the vSphere PowerCLI
3. Set the execution policy for smooth run .
Set-ExecutionPolicy [-executionPolicy] Policy
{ Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined}
[[-Scope] ExecutionPolicyScope ] [-Force]
[-whatIf] [-confirm]
4. Use get-command to list down the cmdlet and details
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> get-command
cmdlet and its full details won't be visible on the screen , so we can export that to a csv file and check .
Export-Csv command will help to exporting to csv .
> get-command | Export-Csv c:\commands.csv
5. Connect to vCenter for collecting and understand information about VMware infra from CLI .
>get-vm | Where-Object {$_.PowerState -eq "PoweredOff"} ->List Powered off VMs
>get-vm | Where-Object {$_.PowerState -eq "PoweredOn"} ->List Powered on VMs
>Add-VMHost "ESXHOST" -Location Datacenter -User root -Password password -RunAsync -force:$true -> Adding new esxi host to vCenter Server
> Get-VMHost | Get-ScsiLun -LunType disk -> Get connected disk details including name , Multipathing , LunType for esxi host
CanonicalN ConsoleDeviceName LunType CapacityGB MultipathPolicy
ame
---------- ----------------- ------- ---------- ---------------
naa.600... /vmfs/devices/disks/naa.600... disk 1,024.000 RoundRobin
naa.600... /vmfs/devices/disks/naa.600... disk 4,096.000 RoundRobin
Note:- Export to csv for complete information .
> Get-VMHost "ESXIHOST" | Get-ScsiLun -LunType disk -> Get connected disk details including name , Multipathing , LunType for specific host
>Get-VMHost "ESXIHOST" | Get-ScsiLun -CanonicalName "naa.60002ac0000000000000000900006e54" | Set-ScsiLun -MultipathPolicy "roundrobin"
-> You can set the LUN to required multipathing policy
> Get-VM BACKUPTEST | Get-AdvancedSetting -> List advanced configuration of a VM
Name Value Type Description
---- ----- ---- -----------
svga.present true VM
sched.cpu.latency... normal VM
sched.scsi0:0.thr... off VM
tools.guest.deskt... false VM
pciBridge0.present true VM
pciBridge4.present true VM
List virtual machine where CBT enabled
>Get-VM| Where-Object{$_.ExtensionData.Config.ChangeTrackingEnabled -eq $true}
List virtual machine where CBT disabled
>Get-VM| Where-Object{$_.ExtensionData.Config.ChangeTrackingEnabled -eq $false}
Check and Configure NTP on all ESXI hosts
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"}
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False
ntpd NTP Daemon on True False
ntpd NTP Daemon off False False
ntpd NTP Daemon off False False
> Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"}
Name Enabled OutgoingPorts Protocols ServiceRunning
---- ------- ------------- --------- --------------
NTP Client True 123 UDP True
NTP Client True 123 UDP True
NTP Client False 123 UDP False
NTP Client False 123 UDP False
NTP Client False 123 UDP False
>Get-VMHost | Add-VMHostNtpServer 192.168.1.1,192.168.1.2
> get-vmhost esx1.test.com |Get-VMHostNtpServer
192.168.1.1
192.168.1.2
> get-vmhost esx1.test.com |Remove-VMHostNtpServer 92.168.1.2 -confirm:$false
> Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} |Set-VMHostFirewallException -Enabled:$true
Name Enabled OutgoingPorts Protocols ServiceRunning
---- ------- ------------- --------- --------------
NTP Client True 123 UDP True
NTP Client True 123 UDP True
NTP Client True 123 UDP False
NTP Client True 123 UDP False
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False
ntpd NTP Daemon on True False
ntpd NTP Daemon off True False
ntpd NTP Daemon off True False
> Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
Note:- Get-VMhost "ESXIHOSTNAME" , if you use this you can apply these setting only to that HOST .
Update VMware Tools without reboot
> get-vm VMNAME | Update-Tools -NoReboot
List the VMs connected CD drive and find the ISO path
>Get-VM | FT Name, @{Label="ISO file"; Expression = { ($_ | Get-CDDrive).ISOPath }}
?
>Get-VM | where {($_ | Get-CDDrive).ISOPath -ne $null}
>Get-vm | where { $_ | Get-CDDrive | where { $_.ConnectionState.Connected}}
Disconnect the CD from All VMS where cd-rom connected
>Get-VM | Get-CDDrive | Where {$_.ConnectionState.Connected} | Set-CDDrive -Connected $false -Confirm:$false
list the VMs where Floppy Drive Connected
>Get-vm | where { $_ | Get-FloppyDrive | where { $_.ConnectionState.Connected -eq "true"}}
List the FC adapter details
> Get-VMhost | Get-VMHostHBA -Type FibreChannel
Device Type Model Status
------ ---- ----- ------
vmhba0 FibreChannel Emulex OneConnect OCe11100 ... online
vmhba1 FibreChannel Emulex OneConnect OCe11100 ... online
vmhba0 FibreChannel Emulex OneConnect OCe11100 ... online
List the WWN of Host
> Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN"
;E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device
VMHost Device WWN
------ ------ -------
esxi-01 vmhba1 50060B0000C26600
esxi-01 vmhba2 50060B0000C26602
esxi-02 vmhba1 50060B0000C26604
esxi-02 vmhba2 50060B0000C26606
> Get-VMhost esxi-01 | Get-VMHostHBA
Device Type Model Status
------ ---- ----- ------
vmhba0 FibreChannel Emulex OneConnect OCe14000,... online
vmhba1 FibreChannel Emulex OneConnect OCe14000,... online
vmhba2 Block Smart Array P244br unknown
List the iSCSI HBA details
> Get-VMhost esxi-01 | Get-VMHostHBA -Type iSCSI
Device Type Model Status
------ ---- ----- ------
vmhba32 IScsi iSCSI Software Adapter online
vmhba32 IScsi QLogic 5709 1 Gigabit Ether... unbound
vmhba33 IScsi QLogic 5709 1 Gigabit Ether... unbound
vmhba32 IScsi QLogic 5709 1 Gigabit Ether... unbound
vmhba33 IScsi QLogic 5709 1 Gigabit Ether... unbound
Find what version of the tools where installed
List Virtual machine Hardware information
>Get-VM | ForEach-Object {Get-View $_.ID} | ForEach-Object {Write-Host $_.Name $_.Config.Version}
List Complete Network Adapter Information
> get-vm |Get-NetworkAdapter
Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 VM Network 00:12:45:55:16:12 True
Network adapter 2 Vmxnet3 VM Network 00:12:45:55:ca:23 True
Network adapter 1 Vmxnet3 SAPLA_VLA... 00:12:45:55:ed:45 True
Network adapter 1 Vmxnet3 SAPLA_VLA... 00:12:45:55:fa:56 True
Network adapter 1 e1000 MobileAPP... 00:12:45:55:g9:34 True
List only Network Adapter Information
> Get-VM | Get-NetworkAdapter | Select @{N="VM";E={$_.Parent.Name}},Name,Type,WakeOnLan | ft -Autosize
List network adapters which are not VMXNet3
>Get-VM | Get-NetworkAdapter |
Where-object {$_.Type -ne "Vmxnet3"} |
Select @{N="VM";E={$_.Parent.Name}},Name,Type |
export-Csv c:\Network.csv -NoTypeInformation
List Virtual Machine Inside a Datastore
> Get-Datastore VM-DATASTORE-01 |get-vm
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
SCCM-DC-VM-01 PoweredOn 4 4.000
SCCM-DC-VM-02 PoweredOn 4 4.000
ADFS-DC-VM-01 PoweredOn 2 8.000
List only VM Name residing in a specific Datastore
List VM with Data store Information
> get-datastore|%{$ds=$_; $ds.Extensiondata.Vm|%{$_|select @{n='vm name';e={(Get-View -property name -Id $_.toS
tring()).name}},@{n='ds name';e={$ds.name}} }}
vm name ds name
------- ---------
SCCM-DC-VM-01 VM-DATASTORE-01
SCCM-DC-VM-02 VM-DATASTORE-01
ADFS-DC-VM-03 VM-DATASTORE-03
ADPROD-DC-VM-01 VM-DATASTORE-05
List Get-VMHostService name and Status
> Get-VMHost esxi-01 | Get-VMHostService
Key Label Policy Running Required
--- ----- ------ ------- --------
DCUI Direct Console UI on True False
TSM ESXi Shell off False False
TSM-SSH SSH off False False
Get Status of Specific services of all ESXi Host
> Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
TSM-SSH SSH off False False
> Get-VMHost | Get-VMHostService | Where { $_.Key -eq "ntpd"}
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
> Get-VMHost | Get-VMHostService | Where { $_.Label -eq "SSH"}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
TSM-SSH SSH off False False
TSM-SSH SSH on True False
TSM-SSH SSH off False False
Get Status of Specific services of all ESXi Host inclsing Host Name
> Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} | Select Key,Label,Policy,Running,VMHost,Required | ft -Autosize
Key Label Policy Running VMHost Required
--- ----- ------ ------ ------------ --------
TSM-SSH SSH off False esxi-09.test.com False
TSM-SSH SSH off False esxi-10.test.com False
TSM-SSH SSH on True esxi-01.test.com False
TSM-SSH SSH off False esxi-02.test.com False
Start a Partucular ESXi host Service
> Get-VMHost esxi-01.test.com | Foreach { Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) -confirm:$false}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off True False
Start a Partucular host Service all ESXi host
> Get-VMHost | Foreach { Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) -confirm:$false}
Stop a Partucular ESXi host Service
> Get-VMHost esxi-01.test.com | Foreach { Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) -Confirm:$false}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH on False False
Suppress the waring of the SHH of esxi host when is it is running
>Get-VMHost esxi-01.test.com | Get-VmHostAdvancedConfiguration -Name UserVars.SuppressShellWarning
Name Value
---- -----
UserVars.SuppressShellWarning 0
>Get-VMHost esxi-01.test.com | Set-VmHostAdvancedConfiguration -Name UserVars.SuppressShellWarning -Value 1
Name Value
---- -----
UserVars.SuppressShellWarning 1
Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -match "TSM-SSH|NTP"} | Select-Object @{N="Service";E={$_.Label}}, @{N="Policy";E={ switch ($_.Policy) { "off" { "Start and stop manually"} "on" { "Start and stop with host" } "automatic" { "Start automatically if any ports are open, and stop when all ports are closed" }}}}
Note:- you may add different host services to get details
Set the ESXi host service poly on or off
>Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"} | Set-VMHostService -policy "off" -Confirm:$false
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
>Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"} | Set-VMHostService -policy "on" -Confirm:$false
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH on False False
Check the esxi hostservice Policy status
> Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
Restart the vmhost service
>Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"} | Restart-VMHostService -Confirm:$false
List VM - Guest Operating system Hard Drive Information
>ForEach ($VM in (Get-VM |Get-View)){($VM.Guest.Disk |Select @{N=“Name“;E={$VM.Name}},DiskPath, @{N=“Capacity(MB)“;E={[math]::Round($_.Capacity/ 1MB)}}, @{N=“Free Space(MB)“;E={[math]::Round($_.FreeSpace / 1MB)}}, @{N=“Free Space %“;E={[math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)),0)}}) | Format-Table}
Name DiskPath Capacity(MB) Free Space(MB) Free Space %
---- -------- ------------ -------------- ------------
VM1 C:\ 102048 80117 79
Name DiskPath Capacity(MB) Free Space(MB) Free Space %
---- -------- ------------ -------------- ------------
VM2 C:\ 102397 42434 41
VM2 D:\ 102298 71183 70
Get the IP address of a VM
>Get-VM | Select Name, @{N="IP Address";E={@($_.guest.IPAddress[0])}}
Name IP Address
---- ----------
VM1 192.168.3.45
VM2 192.168.3.46
Get IP address of a VM , if there multiple IP configured (Remove [0])
>Get-VM | Select Name, Host, @{N="IP Address";E={@($_.guest.IPAddress)}}
Name IP Address
---- ----------
VM3 192.168.3.48,192.168.3.67,fe90::210:46ef:fe24:
VM4 192.168.3.49,192.168.3.68
For Specific Cluster
>Get-Cluster "Cluster" | Get-VM | Select Name, Host, @{N="IP Address";E={@($_.guest.IPAddress[0])}}
Join an esxi host to domain
Get-VMHostAuthentication -VMHost "hostname" | Set-VMHostAuthentication -Domain domain.com -User "uaename@domain.com" -Password XXXXXX -JoinDomain -Confirm:$false
Remove a host from Domain
Get-VMHostAuthentication -VMHost "hostname" | Set-VMHostAuthentication
-Leavedomain
List of MAC addresses for all VMs on vcenter and export to CSV
Get-Cluster “clustername” | Get-VMHost | Get-VirtualSwitch -Name “vSwitch0” | New-VirtualPortGroup -Name “Portgrounp Name” -VLanId "XX"
Example
Get-Cluster VMLAB | Get-VMHost | Get-VirtualSwitch -Name “vSwitch0” | New-VirtualPortGroup -Name “LAB_PROD” -VLanId "10"
PowerCLI is the best tool for automating management and configuration of VMware vSphere .
1. Install vSphere Power cli on your machine .
2. Run the vSphere PowerCLI
3. Set the execution policy for smooth run .
Set-ExecutionPolicy [-executionPolicy] Policy
{ Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined}
[[-Scope] ExecutionPolicyScope ] [-Force]
[-whatIf] [-confirm]
4. Use get-command to list down the cmdlet and details
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> get-command
cmdlet and its full details won't be visible on the screen , so we can export that to a csv file and check .
Export-Csv command will help to exporting to csv .
> get-command | Export-Csv c:\commands.csv
5. Connect to vCenter for collecting and understand information about VMware infra from CLI .
> Connect-VIServer "vCenter Name / IP "
It will ask for user name and password to access vCenter , or alternate we can add username and password to same line .
> Connect-VIServer "vCenter Name / IP " -user "username" -password "password "
6. Use the below basic commands
> Get-Esx "ESx hostname/ IP " -user "username" -password "password "
Name Port User
---- ---- ----
esxihost 443 root
> Get-Datacenter ->List Datacenter Inventory
Name
----
DATACENTER NAME
> Get-Cluster ->List Cluster Inventory
Name HAEnabled HAFailover DrsEnabled DrsAutomationLevel
Level
---- --------- ---------- ---------- ------------------
Cluster Name True 0 False FullyAutomated
> Get-VMHost ->List Complete HOST Inventory
Name Connection PowerState NumCpu CpuUsage CpuTotal Memory Memory Version
State Mhz Mhz UsageGB TotalGB
---- ----------- ------- ----- ------- -------- -------- -------- ------
esxi-01 Connected PoweredOn 8 387 19200 24.840 159.990 6.0.0
esxi-02 Connected PoweredOn 8 2730 19200 48.579 159.990 6.0.0
> Get-VM -> List Complete VM Inventory
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
TESTVM PoweredOn 4 4.000
>Get-Datastore -> List Datastore Inventory
Name FreeSpaceGB CapacityGB
---- ----------- ----------
esxi-01(Local) 130.802 131.750
esxi-02(Local) 130.802 131.750
> Get-VirtualSwitch -> List vswitch Inventory
Name NumPorts Mtu Notes
---- -------- --- -----
vSwitch0 3072 1500
vSwitch1 3072 1500
> Get-VirtualPortGroup -> List Portgroup Inventory
Name Key VLanId
----------- -------- --------
VmNeetwork1 Key-vim.host.PortGroup-VmNeetwork1 122
VmNeetwork2 key-vim.host.PortGroup-VmNeetwork2 123
get-vm | get-snapshot | format-list , The command above will give you the following properties:
Description
Created
Quiesced
PowerState
VM
VMId
Parent
ParentSnapshotId
ParentSnapshot
Children
SizeMB
IsCurrent
IsReplaySupported
ExtensionData
Id
Name
Uid
> get-vm | Get-Snapshot -> Generate compplete snapshot inventory with few details
Name Description PowerState
---- ----------- ----------
VM Tools PoweredOn
VM Tools PoweredOn
Restore Point Avamar Snapshot generated for back PoweredOff
>get-vm | get-snapshot | format-list | Export-Csv c:\snaps.csv -> Export compplete snapshot inventory to csv file
>get-vm -location “My lab” | get-snapshot -> Generate snapshot information inside folder
> get-vm | get-snapshot | where {$_.Description -match "Avamar Snapshot generated for back" } -> Generate snapshot information with specific Description
Name Description PowerState
---- ----------- ----------
Avamar_a3de7b18-641a-446a-987f-80e1 Avamar Snapshot generated for back PoweredOn
Avamar_a3de7b18-641a-446a-987f-80e1 Avamar Snapshot generated for back PoweredOn
>get-vm | get-snapshot | where {$_.Description -match "Avamar Snapshot generated for back"}| Format-Table -Property VM,Name,Created,Description, SizeMB
-> Generate snapshot information with specific Description and formateed mode
VM Name Created Description SizeMB
-- ---- ------- ----------- ------
VM1 Avamar_a3de7b18 16.05.2015 12:56:25 Avamar Snapshot generated for back 6768,13
VM2 Avamar_a3de7b18 16.05.2015 13:01:25 Avamar Snapshot generated for back 892,63
> get-vm backuptest |New-Snapshot -name test -Description test -> Create new snapshot with descrition and name
Name Description PowerState
---- ----------- ----------
test test PoweredOff
> get-vm backuptest |Get-Snapshot -> Get snapshot information about specific VM
Name Description PowerState
---- ----------- ----------
TEST test PoweredOff
> get-vm backuptest |Get-Snapshot |Remove-Snapshot -Confirm:$false -> Remove snapshot of a specific VM
Remove Snapshot By Description
>get-vm | get-snapshot | where {$_.Description -match "Avamar Snapshot generated for back"}| Remove-Snapshot -Confirm:$false -RunAsync
> Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-130)} -> List snapshot created prior to "X" days
Name Description PowerState
---- ----------- ----------
06 Jan 1.30PM PoweredOn
06012016 5.1.3 PoweredOn
> Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-130)} | Select-Object VM, Name, Created, SizeMB
VM Name Created SizeMB
-- ---- ------- ------
VM1 06 Jan 1.30PM 1/6/2016 1:39:37 PM 16396.18355846405029296875
VM2 06012016 5.1.3 1/6/2016 2:31:26 PM 41040.17965984344482421875
Description
Created
Quiesced
PowerState
VM
VMId
Parent
ParentSnapshotId
ParentSnapshot
Children
SizeMB
IsCurrent
IsReplaySupported
ExtensionData
Id
Name
Uid
> get-vm | Get-Snapshot -> Generate compplete snapshot inventory with few details
Name Description PowerState
---- ----------- ----------
VM Tools PoweredOn
VM Tools PoweredOn
Restore Point Avamar Snapshot generated for back PoweredOff
>get-vm | get-snapshot | format-list | Export-Csv c:\snaps.csv -> Export compplete snapshot inventory to csv file
>get-vm -location “My lab” | get-snapshot -> Generate snapshot information inside folder
> get-vm | get-snapshot | where {$_.Description -match "Avamar Snapshot generated for back" } -> Generate snapshot information with specific Description
Name Description PowerState
---- ----------- ----------
Avamar_a3de7b18-641a-446a-987f-80e1 Avamar Snapshot generated for back PoweredOn
Avamar_a3de7b18-641a-446a-987f-80e1 Avamar Snapshot generated for back PoweredOn
>get-vm | get-snapshot | where {$_.Description -match "Avamar Snapshot generated for back"}| Format-Table -Property VM,Name,Created,Description, SizeMB
-> Generate snapshot information with specific Description and formateed mode
VM Name Created Description SizeMB
-- ---- ------- ----------- ------
VM1 Avamar_a3de7b18 16.05.2015 12:56:25 Avamar Snapshot generated for back 6768,13
VM2 Avamar_a3de7b18 16.05.2015 13:01:25 Avamar Snapshot generated for back 892,63
> get-vm backuptest |New-Snapshot -name test -Description test -> Create new snapshot with descrition and name
Name Description PowerState
---- ----------- ----------
test test PoweredOff
> get-vm backuptest |Get-Snapshot -> Get snapshot information about specific VM
Name Description PowerState
---- ----------- ----------
TEST test PoweredOff
> get-vm backuptest |Get-Snapshot |Remove-Snapshot -Confirm:$false -> Remove snapshot of a specific VM
Remove Snapshot By Description
>get-vm | get-snapshot | where {$_.Description -match "Avamar Snapshot generated for back"}| Remove-Snapshot -Confirm:$false -RunAsync
> Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-130)} -> List snapshot created prior to "X" days
Name Description PowerState
---- ----------- ----------
06 Jan 1.30PM PoweredOn
06012016 5.1.3 PoweredOn
> Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-130)} | Select-Object VM, Name, Created, SizeMB
VM Name Created SizeMB
-- ---- ------- ------
VM1 06 Jan 1.30PM 1/6/2016 1:39:37 PM 16396.18355846405029296875
VM2 06012016 5.1.3 1/6/2016 2:31:26 PM 41040.17965984344482421875
>get-vm | Where-Object {$_.PowerState -eq "PoweredOff"} ->List Powered off VMs
>get-vm | Where-Object {$_.PowerState -eq "PoweredOn"} ->List Powered on VMs
>Add-VMHost "ESXHOST" -Location Datacenter -User root -Password password -RunAsync -force:$true -> Adding new esxi host to vCenter Server
> Get-VMHost | Get-ScsiLun -LunType disk -> Get connected disk details including name , Multipathing , LunType for esxi host
CanonicalN ConsoleDeviceName LunType CapacityGB MultipathPolicy
ame
---------- ----------------- ------- ---------- ---------------
naa.600... /vmfs/devices/disks/naa.600... disk 1,024.000 RoundRobin
naa.600... /vmfs/devices/disks/naa.600... disk 4,096.000 RoundRobin
Note:- Export to csv for complete information .
> Get-VMHost "ESXIHOST" | Get-ScsiLun -LunType disk -> Get connected disk details including name , Multipathing , LunType for specific host
>Get-VMHost "ESXIHOST" | Get-ScsiLun -CanonicalName "naa.60002ac0000000000000000900006e54" | Set-ScsiLun -MultipathPolicy "roundrobin"
-> You can set the LUN to required multipathing policy
> Get-VM BACKUPTEST | Get-AdvancedSetting -> List advanced configuration of a VM
Name Value Type Description
---- ----- ---- -----------
svga.present true VM
sched.cpu.latency... normal VM
sched.scsi0:0.thr... off VM
tools.guest.deskt... false VM
pciBridge0.present true VM
pciBridge4.present true VM
List virtual machine where CBT enabled
>Get-VM| Where-Object{$_.ExtensionData.Config.ChangeTrackingEnabled -eq $true}
List virtual machine where CBT disabled
>Get-VM| Where-Object{$_.ExtensionData.Config.ChangeTrackingEnabled -eq $false}
Check and Configure NTP on all ESXI hosts
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"}
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False
ntpd NTP Daemon on True False
ntpd NTP Daemon off False False
ntpd NTP Daemon off False False
> Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"}
Name Enabled OutgoingPorts Protocols ServiceRunning
---- ------- ------------- --------- --------------
NTP Client True 123 UDP True
NTP Client True 123 UDP True
NTP Client False 123 UDP False
NTP Client False 123 UDP False
NTP Client False 123 UDP False
>Get-VMHost | Add-VMHostNtpServer 192.168.1.1,192.168.1.2
> get-vmhost esx1.test.com |Get-VMHostNtpServer
192.168.1.1
192.168.1.2
> get-vmhost esx1.test.com |Remove-VMHostNtpServer 92.168.1.2 -confirm:$false
> Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} |Set-VMHostFirewallException -Enabled:$true
Name Enabled OutgoingPorts Protocols ServiceRunning
---- ------- ------------- --------- --------------
NTP Client True 123 UDP True
NTP Client True 123 UDP True
NTP Client True 123 UDP False
NTP Client True 123 UDP False
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False
ntpd NTP Daemon on True False
ntpd NTP Daemon off True False
ntpd NTP Daemon off True False
> Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
Update VMware Tools without reboot
> get-vm VMNAME | Update-Tools -NoReboot
List the VMs connected CD drive and find the ISO path
>Get-VM | FT Name, @{Label="ISO file"; Expression = { ($_ | Get-CDDrive).ISOPath }}
?
>Get-VM | where {($_ | Get-CDDrive).ISOPath -ne $null}
>Get-vm | where { $_ | Get-CDDrive | where { $_.ConnectionState.Connected}}
Disconnect the CD from All VMS where cd-rom connected
>Get-VM | Get-CDDrive | Where {$_.ConnectionState.Connected} | Set-CDDrive -Connected $false -Confirm:$false
list the VMs where Floppy Drive Connected
>Get-vm | where { $_ | Get-FloppyDrive | where { $_.ConnectionState.Connected -eq "true"}}
List the FC adapter details
> Get-VMhost | Get-VMHostHBA -Type FibreChannel
Device Type Model Status
------ ---- ----- ------
vmhba0 FibreChannel Emulex OneConnect OCe11100 ... online
vmhba1 FibreChannel Emulex OneConnect OCe11100 ... online
vmhba0 FibreChannel Emulex OneConnect OCe11100 ... online
List the WWN of Host
> Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN"
;E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device
VMHost Device WWN
------ ------ -------
esxi-01 vmhba1 50060B0000C26600
esxi-01 vmhba2 50060B0000C26602
esxi-02 vmhba1 50060B0000C26604
esxi-02 vmhba2 50060B0000C26606
List the HBA adapter details of esxi host
Device Type Model Status
------ ---- ----- ------
vmhba0 FibreChannel Emulex OneConnect OCe14000,... online
vmhba1 FibreChannel Emulex OneConnect OCe14000,... online
vmhba2 Block Smart Array P244br unknown
List the iSCSI HBA details
> Get-VMhost esxi-01 | Get-VMHostHBA -Type iSCSI
Device Type Model Status
------ ---- ----- ------
vmhba32 IScsi iSCSI Software Adapter online
vmhba32 IScsi QLogic 5709 1 Gigabit Ether... unbound
vmhba33 IScsi QLogic 5709 1 Gigabit Ether... unbound
vmhba32 IScsi QLogic 5709 1 Gigabit Ether... unbound
vmhba33 IScsi QLogic 5709 1 Gigabit Ether... unbound
Find what version of the tools where installed
>Get-View -ViewType VirtualMachine | select Name, @{ Name=”ToolsVersion”; Expression={$_.config.tools.toolsVersion}}
Find toolversion and toolversionstatus
>Get-View -ViewType VirtualMachine | select Name, @{ Name=”ToolsVersion”; Expression={$_.config.tools.toolsVersion}},@{ Name=”ToolStatus”; Expression={$_.Guest.ToolsVersionStatus}}
>Get-VM | ForEach-Object {Get-View $_.ID} | ForEach-Object {Write-Host $_.Name $_.Config.Version}
List Complete Network Adapter Information
> get-vm |Get-NetworkAdapter
Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 VM Network 00:12:45:55:16:12 True
Network adapter 2 Vmxnet3 VM Network 00:12:45:55:ca:23 True
Network adapter 1 Vmxnet3 SAPLA_VLA... 00:12:45:55:ed:45 True
Network adapter 1 Vmxnet3 SAPLA_VLA... 00:12:45:55:fa:56 True
Network adapter 1 e1000 MobileAPP... 00:12:45:55:g9:34 True
List only Network Adapter Information
List network adapters which are not VMXNet3
>Get-VM | Get-NetworkAdapter |
Where-object {$_.Type -ne "Vmxnet3"} |
Select @{N="VM";E={$_.Parent.Name}},Name,Type |
export-Csv c:\Network.csv -NoTypeInformation
List Virtual Machine Inside a Datastore
> Get-Datastore VM-DATASTORE-01 |get-vm
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
SCCM-DC-VM-01 PoweredOn 4 4.000
SCCM-DC-VM-02 PoweredOn 4 4.000
ADFS-DC-VM-01 PoweredOn 2 8.000
List only VM Name residing in a specific Datastore
Get-Datastore
-Name
'datastore_name'
).Extensiondata.Vm|%{(
Get-View
-Id
$_
.toString()).name}
List VM with Data store Information
> get-datastore|%{$ds=$_; $ds.Extensiondata.Vm|%{$_|select @{n='vm name';e={(Get-View -property name -Id $_.toS
tring()).name}},@{n='ds name';e={$ds.name}} }}
vm name ds name
------- ---------
SCCM-DC-VM-01 VM-DATASTORE-01
SCCM-DC-VM-02 VM-DATASTORE-01
ADFS-DC-VM-03 VM-DATASTORE-03
ADPROD-DC-VM-01 VM-DATASTORE-05
List Get-VMHostService name and Status
> Get-VMHost esxi-01 | Get-VMHostService
Key Label Policy Running Required
--- ----- ------ ------- --------
DCUI Direct Console UI on True False
TSM ESXi Shell off False False
TSM-SSH SSH off False False
Get Status of Specific services of all ESXi Host
> Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
TSM-SSH SSH off False False
> Get-VMHost | Get-VMHostService | Where { $_.Key -eq "ntpd"}
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
> Get-VMHost | Get-VMHostService | Where { $_.Label -eq "SSH"}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
TSM-SSH SSH off False False
TSM-SSH SSH on True False
TSM-SSH SSH off False False
Get Status of Specific services of all ESXi Host inclsing Host Name
> Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} | Select Key,Label,Policy,Running,VMHost,Required | ft -Autosize
Key Label Policy Running VMHost Required
--- ----- ------ ------ ------------ --------
TSM-SSH SSH off False esxi-09.test.com False
TSM-SSH SSH off False esxi-10.test.com False
TSM-SSH SSH on True esxi-01.test.com False
TSM-SSH SSH off False esxi-02.test.com False
Start a Partucular ESXi host Service
> Get-VMHost esxi-01.test.com | Foreach { Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) -confirm:$false}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off True False
Start a Partucular host Service all ESXi host
> Get-VMHost | Foreach { Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) -confirm:$false}
Stop a Partucular ESXi host Service
> Get-VMHost esxi-01.test.com | Foreach { Stop-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) -Confirm:$false}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH on False False
Suppress the waring of the SHH of esxi host when is it is running
>Get-VMHost esxi-01.test.com | Get-VmHostAdvancedConfiguration -Name UserVars.SuppressShellWarning
Name Value
---- -----
UserVars.SuppressShellWarning 0
>Get-VMHost esxi-01.test.com | Set-VmHostAdvancedConfiguration -Name UserVars.SuppressShellWarning -Value 1
Name Value
---- -----
UserVars.SuppressShellWarning 1
Check the services Policy information with what ON/OFF indicates
Note:- you may add different host services to get details
Set the ESXi host service poly on or off
>Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"} | Set-VMHostService -policy "off" -Confirm:$false
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
>Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"} | Set-VMHostService -policy "on" -Confirm:$false
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH on False False
Check the esxi hostservice Policy status
> Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"}
Key Label Policy Running Required
--- ----- ------ ------- --------
TSM-SSH SSH off False False
Restart the vmhost service
>Get-VMHostService -VMHost esxi-01.test.com | Where-Object {$_.Key -eq "TSM-SSH"} | Restart-VMHostService -Confirm:$false
List VM - Guest Operating system Hard Drive Information
>ForEach ($VM in (Get-VM |Get-View)){($VM.Guest.Disk |Select @{N=“Name“;E={$VM.Name}},DiskPath, @{N=“Capacity(MB)“;E={[math]::Round($_.Capacity/ 1MB)}}, @{N=“Free Space(MB)“;E={[math]::Round($_.FreeSpace / 1MB)}}, @{N=“Free Space %“;E={[math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)),0)}}) | Format-Table}
Name DiskPath Capacity(MB) Free Space(MB) Free Space %
---- -------- ------------ -------------- ------------
VM1 C:\ 102048 80117 79
Name DiskPath Capacity(MB) Free Space(MB) Free Space %
---- -------- ------------ -------------- ------------
VM2 C:\ 102397 42434 41
VM2 D:\ 102298 71183 70
Get the IP address of a VM
>Get-VM | Select Name, @{N="IP Address";E={@($_.guest.IPAddress[0])}}
Name IP Address
---- ----------
VM1 192.168.3.45
VM2 192.168.3.46
Get IP address of a VM , if there multiple IP configured (Remove [0])
>Get-VM | Select Name, Host, @{N="IP Address";E={@($_.guest.IPAddress)}}
Name IP Address
---- ----------
VM3 192.168.3.48,192.168.3.67,fe90::210:46ef:fe24:
VM4 192.168.3.49,192.168.3.68
For Specific Cluster
>Get-Cluster "Cluster" | Get-VM | Select Name, Host, @{N="IP Address";E={@($_.guest.IPAddress[0])}}
Join an esxi host to domain
Get-VMHostAuthentication -VMHost "hostname" | Set-VMHostAuthentication -Domain domain.com -User "uaename@domain.com" -Password XXXXXX -JoinDomain -Confirm:$false
Remove a host from Domain
Get-VMHostAuthentication -VMHost "hostname" | Set-VMHostAuthentication
-Leavedomain
Get VM host network details and output to the PowerCLI console:
Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, Mac, PortGroupName, vMotionEnabled, mtu, FullDuplex, BitRatePerSec
Get VM host network details to CSV
Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, Mac, PortGroupName, vMotionEnabled, mtu, FullDuplex, BitRatePerSec | Export-Csv C:\VMHostNetworkDetails.csv
List of MAC addresses for all VMs on vcenter and export to CSV
Get-View -Viewtype VirtualMachine -Property Name, Config.Hardware.Device | `
Select name,
@{n="MAC(s)"; e={($_.Config.Hardware.Device | ?{($_ -is [VMware.Vim.VirtualEthernetCard])} | %{$_.MacAddress}) -join ","}} | `
Export-Csv c:\temp\VMMACsInfo.csv -UseCulture -NoTypeInformation
Create and add PortGroup to all ESXi hosts in cluster
Get-Cluster “clustername” | Get-VMHost | Get-VirtualSwitch -Name “vSwitch0” | New-VirtualPortGroup -Name “Portgrounp Name” -VLanId "XX"
Example
Get-Cluster VMLAB | Get-VMHost | Get-VirtualSwitch -Name “vSwitch0” | New-VirtualPortGroup -Name “LAB_PROD” -VLanId "10"
Configure ESXi shell Time out
For Single Host
Get-VMHost esxi | Get-AdvancedSetting -Name 'UserVars.ESXiShellInteractiveTimeOut' | Set-AdvancedSetting -Value "300" -Confirm:$false
For all the ESXi host
Get-VMHost | Get-AdvancedSetting -Name 'UserVars.ESXiShellInteractiveTimeOut' | Set-AdvancedSetting -Value "300" -Confirm:$false
Get-VMHost esxi | Get-AdvancedSetting -Name 'UserVars.ESXiShellInteractiveTimeOut' | Set-AdvancedSetting -Value "300" -Confirm:$false
For all the ESXi host
Get-VMHost | Get-AdvancedSetting -Name 'UserVars.ESXiShellInteractiveTimeOut' | Set-AdvancedSetting -Value "300" -Confirm:$false
Same Page will be Updated with more commands and details asap.
Great effort
ReplyDeleteGood sharing...
ReplyDeleteVery Good Brother ! Keep it up !
ReplyDelete