Script Extracts and Examples (PowerCLI): Difference between revisions

From vwiki
Jump to navigation Jump to search
(→‎Useful CmdLets etc: Added "ESXi Logs")
m (Typo fix)
 
(60 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Getting Started ==
#REDIRECT [[:Category:PowerCLI]]
Before being able to use any the following script you need to install...
* [http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Windows PowerShell V1]
* [http://support.microsoft.com/kb/968929 Windows Powershell V2]
* [http://www.vmware.com/go/powershell VI Toolkit]
 
On the first run you need to allow the Toolkit to run properly by running (you need to run this command as an administrator, so if you're using Windows 7, for example, you'll need to start the Powershell console as an administrator, regardless of whether you're currently logged in as an admin)...
<source lang="powershell"> Set-ExecutionPolicy RemoteSigned </source>
 
Connect to the Virtual Centre (or ESX) server using the following command using your normal username and password (same as you'd use to log into the VI Client).  You will need access to the servers on TCP 443.
<source lang="powershell"> Connect-VIServer -Server <server> -User <user> -Password <pass> </source>
 
Once connected you can do real work with the PowerCLI, to see what's available, login to https://server/mob.  This is effectively where you end up once you've completed the <code> Connect-VIServer </code> command.
 
Be aware that PowerShell commands generally return objects, rather than text, and that the textual representation of the return object is often abbreviated for simplicity.  To see the entire return for a command, pipe the result into <code>Format-List</code>.  To complicate matters further, some return objects contain further objects, see examples below
 
<source lang="powershell">
[vSphere PowerCLI] E:\> get-vm -name "vserver1"
 
Name                PowerState Num CPUs Memory (MB)
----                ---------- -------- -----------
vserver1              PoweredOn  1        756
 
 
[vSphere PowerCLI] E:\> get-vm -name "vserver1" | Format-List *
 
PowerState          : PoweredOn
Description        :
Guest              : VMware.VimAutomation.Client20.VMGuestImpl
NumCpu              : 1
MemoryMB            : 756
CDDrives            : {CD/DVD Drive 1}
FloppyDrives        : {Floppy Drive 1}
HardDisks          : {Hard Disk 1}
NetworkAdapters    : {Network Adapter 1}
Host                : esx1.domain.com
HostId              : HostSystem-host-301
HARestartPriority  : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel  : AsSpecifiedByCluster
CustomFields        : {}
Id                  : VirtualMachine-vm-25136
Name                : vserver1
 
 
[vSphere PowerCLI] E:\> get-vm -name "vserver2" | Format-List *
 
PowerState          : PoweredOff
Description        :
Guest              : VMware.VimAutomation.Client20.VMGuestImpl
NumCpu              : 2
MemoryMB            : 2048
CDDrives            : {CD/DVD Drive 1}
FloppyDrives        : {Floppy Drive 1}
HardDisks          : {Hard Disk 1}
NetworkAdapters    : {Network Adapter 1, Network Adapter 2, Network Adapter 3}
Host                : vserver2.domain.com
HostId              : HostSystem-host-662
HARestartPriority  : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel  : AsSpecifiedByCluster
CustomFields        : {}
Id                  : VirtualMachine-vm-697
Name                : vserver2
 
 
[vSphere PowerCLI] E:\> get-vm -name "vserver2" | ForEach-Object {$_.NetworkAdapters}
 
MacAddress      : 00:50:AB:CD:40:59
WakeOnLanEnabled : True
NetworkName      : Some_Network
Type            : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id              : VirtualMachine-vm-697/4000
Name            : Network Adapter 1
 
MacAddress      : 00:50:AB:CD:55:40
WakeOnLanEnabled : True
NetworkName      : Another_Network
Type            : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id              : VirtualMachine-vm-697/4001
Name            : Network Adapter 2
 
MacAddress      : 00:50:AB:CD:56:da
WakeOnLanEnabled : True
NetworkName      : Yet_Another_Network
Type            : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id              : VirtualMachine-vm-697/4002
Name            : Network Adapter 3
</source>
 
== Useful CmdLets etc ==
=== Virtual Machine ===
 
{|cellpadding="2" cellspacing="0" border="1"
! Command    !! Description
|-
| <code> get-vm </code>
| Get list of VM's
|-
| <code> get-vm <nowiki>|</nowiki> sort -property Name </code>
| Get list of VM's, sorted by name
|-
| <code> get-vm -name "MyVM" -location (Get-Folder -Name "MyFolder") </code>
| Get VM within a specific folder (for when you've VM's with identical names if different folders)
|-
| <code> get-vmguest -VM (get-vm -name "vserver1") </code>
| Get VM guest info (IP address, OS)
|-
| <code> Get-VM "vserver1" <nowiki>|</nowiki> %{(Get-View $_.Id).config.uuid} </code>
| Get VM's UUID
|-
| <code> Get-VM "MyVM" <nowiki>|</nowiki> Move-VM -datastore (Get-datastore "DestinationDatastore") </code>
| Storage VMotion
|-
| <code> Shutdown-VMGuest -VM (get-vm -name "vserver1") </code>
| Sends OS shutdown command via VM Tools
|-
| <code> get-VM "vserver1" <nowiki>|</nowiki> Get-VIEvent <nowiki>|</nowiki> Where {$_.fullFormattedMessage -like "*Power*"} </code>
| Get Power On/Off events for VM
|}
 
=== ESX Host ===
{|cellpadding="2" cellspacing="0" border="1"
! Command    !! Description
|-
| <code> get-vmhost -name esxt1* <nowiki>|</nowiki> %{(Get-View $_.ID).Config.Product} </code>
| Get ESX software version (inc build no)
|-
| <code> Get-ScsiLun -VMHost esxt1* -LunType disk </code>
| Get ESX disk VML ID's (SAN ID are a part of the VML - useful for identifying LUN's with SAN team). See [[ESX#SAN_LUN_ID|ESX SAN LUN ID]]
|-
| <code> Get-ScsiLun -VMHost esxt1* -LunType disk <nowiki>|</nowiki> Get-ScsiLunPath </code>
| Get ESX SCSI paths info
|}
 
=== ESXi Logs ===
{|cellpadding="2" cellspacing="0" border="1"
! Command    !! Description
|-
| <code> Get-Log -VMHost (Get-VMHost "MyESX*") -Bundle -DestinationPath C:\Users\name </code>
| Generate diagnostic bundle
|-
| <code> Get-Log vpxa -VMHost (Get-VMHost "MyESX*") <nowiki>|</nowiki> Select -ExpandProperty Entries </code>
| Get vpxa log
|-
| <code> Get-Log messages -VMHost (Get-VMHost "MyESX*") <nowiki>|</nowiki> Select -ExpandProperty Entries </code>
| Get messages log
|-
| <code> Get-Log hostd -VMHost (Get-VMHost "MyESX*") -StartLineNum 1000 -NumLines 100 <nowiki>|</nowiki> Select -ExpandProperty Entries </code>
| Get hostd log from line 1000
|}
 
=== Storage ===
{|cellpadding="2" cellspacing="0" border="1"
! Command    !! Description
|-
| <code> get-vm -name "MyVM" <nowiki>|</nowiki> get-harddisk <nowiki>|</nowiki> Format-List </code>
| Get the vHD details for a particular VM
|-
| <code> (Get-VM -name "MyVM" <nowiki>|</nowiki> Get-VMGuest).Disks </code>
| Get the HD details for a particular VM (as seen from within guest OS)
|-
| <code> get-vm -Location (Get-Folder -Name "MyFolder") <nowiki>|</nowiki> get-harddisk <nowiki>|</nowiki> sort-object -Property Filename </code>
| Get the vHD details for all VM’s in a folder (sorted by VMDK path)
|}
 
== Performance Statistics ==
The full wealth of performance metrics that are collated by virtual centre are available via the PowerCLI.  This can be very useful for creating custom reports, feeding data into other tools etc.
 
It's important to understand how ESX's generate and manage performance data before going any further...
# An ESX collects performance metrics every 20 sec (know as the Realtime interval), this is retained on the server for 1 hour.
# The ESX rolls the Realtime data up into 5 min interval data
#* [Stand-alone ESX] This 5 min data is retained for 1 day
#* [VC managed ESX] The data is uploaded to the VC database
 
Once in the Virtual Centre database, the data is then rolled up into Historical Interval tables via SQL jobs...
* 5 min interval, 1 day retention
* 30 min interval, 1 week retention
* 2 hr interval, 1 month retention
* 1 day interval, 1 year retention
 
Note that the following are configurable (to a varying degree) via the VI Client.
* The VC rollup intervals and retention periods
* The statistics depth (typically certain metrics are discarded as the retention period grows)
 
To see the available intervals for which metrics are available, use the following command (the results will vary depending on whether you're connected to an ESX or a Virtual Centre).
<source lang="powershell">[vSphere PowerCLI] E:\> Get-StatInterval
 
Name                          SamplingPeriodSecs          StorageTimeSecs Client
----                          ------------------          --------------- ------
Past Day                                      300                    86400 VMware.VimAutomation....
Past Week                                    1800                  604800 VMware.VimAutomation....
Past Month                                  7200                  2592000 VMware.VimAutomation....
Past Year                                  86400                31536000 VMware.VimAutomation....</source>
 
=== Available Metrics ===
Metrics are available for all objects you'd expect (and probably more), not limited to the following...
* Virtual Machines
* Virtual machines Hosts (ESX's)
* Resource Pools
* Datastores
 
For a full list of what objects have what performance counters, and what those counters mean see http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.PerformanceManager.html. 
 
The easiest way to ascertain what metric is available for what object is to use Get-StatType...
<source lang="powershell">[vSphere PowerCLI] E:\> Get-VM "vm-server" | Get-StatType
cpu.usage.average
cpu.usagemhz.average
mem.usage.average
mem.granted.average
mem.active.average
...
 
[vSphere PowerCLI] E:\> Get-VMHost "esx-server*" | Get-StatType
cpu.usage.average
cpu.usagemhz.average
mem.usage.average
mem.granted.average
mem.active.average
mem.shared.average
mem.zero.average
mem.unreserved.average
...</source>
 
'''In order to determine which sampling periods are available''', you need to use the '''<code> Get-StatInterval </code>''' command.  Remember that there is also the '''Realtime''' interval, at which all metrics are available.  The results depend on whether you're connected to a Virtual Centre (and how its configured) or an ESX (and whether that's being managed by a VC)...
<source lang="powershell">
[vSphere PowerCLI] E:\> Get-StatInterval
 
Name                          SamplingPeriodSecs          StorageTimeSecs Client
----                          ------------------          --------------- ------
Past Day                                      300                    86400 VMware.VimAutomation....
Past Week                                    1800                  604800 VMware.VimAutomation....
Past Month                                  7200                  2592000 VMware.VimAutomation....
Past Year                                  86400                31536000 VMware.VimAutomation....
</source>
 
However, just because a metric is available, doesn't make it available in the interval you'd like.  '''In order to find out whether a statistic is available in the interval you'd like''', you need to use the '''<code> Get-StatType -Interval <secs/name> </code>'''...
 
<source lang="powershell">
[vSphere PowerCLI] E:\> Get-VMHost "esx-server*" | Get-Stat -MaxSamples 5 -IntervalSecs 300
 
 
MetricId                  Timestamp                                Value Unit
---------                ----------                              ----- ----
cpu.usage.average        19/01/2010 09:15:00                      7.64 %
cpu.usage.average        19/01/2010 09:10:00                        6.5 %
cpu.usage.average        19/01/2010 09:05:00                      6.37 %
cpu.usage.average        19/01/2010 09:00:00                      6.39 %
cpu.usage.average        19/01/2010 08:55:00                      6.35 %
cpu.usagemhz.average      19/01/2010 09:15:00                        813 MHz
cpu.usagemhz.average      19/01/2010 09:10:00                        692 MHz
cpu.usagemhz.average      19/01/2010 09:05:00                        678 MHz
cpu.usagemhz.average      19/01/2010 09:00:00                        680 MHz
cpu.usagemhz.average      19/01/2010 08:55:00                        676 MHz
mem.usage.average        19/01/2010 09:15:00                      24.89 %
mem.usage.average        19/01/2010 09:10:00                      24.88 %
mem.usage.average        19/01/2010 09:05:00                      24.89 %
mem.usage.average        19/01/2010 09:00:00                      24.9 %
mem.usage.average        19/01/2010 08:55:00                      24.91 %
disk.usage.average        19/01/2010 09:15:00                        156 KBps
disk.usage.average        19/01/2010 09:10:00                        124 KBps
disk.usage.average        19/01/2010 09:05:00                        131 KBps
disk.usage.average        19/01/2010 09:00:00                        126 KBps
disk.usage.average        19/01/2010 08:55:00                        136 KBps
net.usage.average        19/01/2010 09:15:00                          7 KBps
net.usage.average        19/01/2010 09:10:00                          8 KBps
net.usage.average        19/01/2010 09:05:00                          7 KBps
net.usage.average        19/01/2010 09:00:00                          7 KBps
net.usage.average        19/01/2010 08:55:00                          7 KBps
sys.uptime.latest        19/01/2010 09:15:00                    5173577 second
sys.uptime.latest        19/01/2010 09:10:00                    5173277 second
sys.uptime.latest        19/01/2010 09:05:00                    5172977 second
sys.uptime.latest        19/01/2010 09:00:00                    5172677 second
sys.uptime.latest        19/01/2010 08:55:00                    5172376 second
</source>
 
A couple of final points to note
* To retrieve data for the Realtime interval, use the <code> -Realtime </code> option, not <code> -IntervalSecs 20 </code>
** All Realtime metrics are averages (as they're an average over the sampling period)
* Virtual Centre generally drops metrics during roll-ups etc (subject to configuration), therefore for the fullest range of metrics, use the Realtime interval.
** I'd suggest always trying to go via the Virtual Centre, but the data you're after isn't available, check with the ESX direct.
* Be careful when retrieving metrics that might have multiple instances (eg CPU), this is relatively obvious in vSphere as it'll show an instance column, not so in VI3.  You can filter so as to see, for example, only the aggregate instance
** <source lang="powershell">get-vmhost "esx-server*" | get-stat -Stat cpu.usage.average -MaxSamples 1 -Realtime | Where{$_.Instance -eq ""}</source>
 
== Script Extracts ==
=== VM's with Datastores List ===
List of Virtual Machines, and their datastores (with usage)
<source lang="powershell">
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
</source>
 
=== VM's with Host and Cluster List ===
<source lang="powershell">
$vms = Get-VM | sort -property Name
foreach ($vm in $vms)
{
    $vm | Get-Cluster | Select-Object @{Name="VM"; Expression={$vm.name}},@{Name="Current Host"; Expression={$vm.host}},Name
}
</source>
 
=== VM's Inventory CSV ===
 
<source lang="powershell">
$start = Get-Date
 
# Create table for output
# Name DC OS UUID IP Cluster ESX's
 
$table = New-Object system.Data.DataTable "Results"
 
$col1 = New-Object system.Data.DataColumn Name,([string])
$col2 = New-Object system.Data.DataColumn DC,([string])
$col3 = New-Object system.Data.DataColumn OS,([string])
$col4 = New-Object system.Data.DataColumn UUID,([string])
$col5 = New-Object system.Data.DataColumn MgmtIP,([string])
$col6 = New-Object system.Data.DataColumn Cluster,([string])
#$col7 = New-Object system.Data.DataColumn ESXs,([string])
 
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
#$table.columns.add($col7)
 
$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
"Created table after $duration secs"
 
# Get VMs object
$vms = Get-VM | Sort -property Name
 
$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
"Got object list of VM's after $duration secs"
 
foreach ($vm in $vms)
{
$row = $table.NewRow()
$row.Name = (Get-VM -Name $vm).Name
$row.DC = (Get-Datacenter -VM $vm).Name
$row.OS = (Get-VMGuest -VM $vm).OSFullName
$row.UUID = %{(Get-View $vm.Id).config.uuid}
$row.MgmtIP =  [string]::join(" ", ((Get-VMGuest -VM $vm).IPAddress)) # Need to join potential list of IP's
$row.Cluster = (Get-Cluster -VM $vm).Name
$table.Rows.Add($row)
"Added row for $vm"
}
 
$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
"Populated table after $duration secs"
 
$table | Format-Table
$table | Export-Csv -path result.csv
</source>
 
 
[[Category:VMware]]
[[Category:PowerShell]]

Latest revision as of 15:36, 4 October 2016

Redirect to: