Difference between revisions of "Script Extracts and Examples (PowerCLI)"

From vwiki
Jump to navigation Jump to search
m (→‎Script Extracts: Added Synrtax Highlighting)
Line 272: Line 272:
=== VM's with Datastores List ===
=== VM's with Datastores List ===
List of Virtual Machines, and their datastores (with usage)
List of Virtual Machines, and their datastores (with usage)
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
<source lang="powershell">
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
$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 ===
=== VM's with Host and Cluster List ===
$vms = Get-VM | sort -property Name
<source lang="powershell">
foreach ($vm in $vms)
$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
{
}
    $vm | Get-Cluster | Select-Object @{Name="VM"; Expression={$vm.name}},@{Name="Current Host"; Expression={$vm.host}},Name
}
</source>


=== VM's Inventory CSV ===
=== VM's Inventory CSV ===


<pre>
<source lang="powershell">
$start = Get-Date
$start = Get-Date


Line 338: Line 342:
$table | Format-Table
$table | Format-Table
$table | Export-Csv -path result.csv
$table | Export-Csv -path result.csv
</pre>
</source>




[[Category:VMware]]
[[Category:VMware]]
[[Category:PowerShell]]
[[Category:PowerShell]]

Revision as of 09:41, 19 January 2010

Getting Started

Before being able to use any the following script you need to install...

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)...

 Set-ExecutionPolicy RemoteSigned

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.

 Connect-VIServer -Server <server> -User <user> -Password <pass>

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 Connect-VIServer 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 Format-List. To complicate matters further, some return objects contain further objects, see examples below

[vSphere PowerCLI] E:\> get-vm -name "winstg"

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
winstg               PoweredOn  1        756


[vSphere PowerCLI] E:\> get-vm -name "winstg" | 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                : uklonesxt1.datastream.com
HostId              : HostSystem-host-301
HARestartPriority   : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel  : AsSpecifiedByCluster
CustomFields        : {}
Id                  : VirtualMachine-vm-25136
Name                : winstg


[vSphere PowerCLI] E:\> get-vm -name "ukt1ewapilp1" | 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                : uklonesxtml1.datastream.com
HostId              : HostSystem-host-662
HARestartPriority   : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel  : AsSpecifiedByCluster
CustomFields        : {}
Id                  : VirtualMachine-vm-697
Name                : ukt1ewapilp1


[vSphere PowerCLI] E:\> get-vm -name "ukt1ewapilp1" | ForEach-Object {$_.NetworkAdapters}

MacAddress       : 00:50:56:89:40:59
WakeOnLanEnabled : True
NetworkName      : PubLBFE2_52.128
Type             : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id               : VirtualMachine-vm-697/4000
Name             : Network Adapter 1

MacAddress       : 00:50:56:89:55:40
WakeOnLanEnabled : True
NetworkName      : PubLBBE1_52.192
Type             : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id               : VirtualMachine-vm-697/4001
Name             : Network Adapter 2

MacAddress       : 00:50:56:89:56:da
WakeOnLanEnabled : True
NetworkName      : PubHostBE2_49.192
Type             : Flexible
ConnectionState  : VMware.VimAutomation.Client20.ConnectInfoImpl
Id               : VirtualMachine-vm-697/4002
Name             : Network Adapter 3

Useful CmdLets etc

Virtual Machine

Command Description
get-vm Get list of VM's
get-vm | sort -property Name Get list of VM's, sorted by name
get-vmguest -VM (get-vm -name "winstg") Get VM guest info (IP address, OS)
Get-VM "winstg" | %{(Get-View $_.Id).config.uuid} Get VM's UUID
Shutdown-VMGuest -VM (get-vm -name "winstg") Sends OS shutdown command via VM Tools

ESX Host

Command Description
get-vmhost -name uklonesxt1* | %{(Get-View $_.ID).Config.Product} Get ESX software version (inc build no)
Get-ScsiLun -VMHost uklonesxt1* -LunType disk Get ESX disk VML ID's (SAN ID are a part of the VML - useful for identifying LUN's with SAN team)
Get-ScsiLun -VMHost uklonesxt1* -LunType disk | Get-ScsiLunPath Get ESX SCSI paths info

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...

  1. An ESX collects performance metrics every 20 sec (know as the Realtime interval), this is retained on the server for 1 hour.
  2. 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).

[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....

Available Metrics

Metrics are available for each of the following types of object

  • Virtual Machines
  • Virtual machines Hosts (ESX's)
  • Resource Pools
  • Datastores

The easiest way to ascertain what metric is available for what object is to use Get-StatType...

[vSphere PowerCLI] E:\> Get-VM "vm-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.swapped.average
mem.swaptarget.average
mem.swapin.average
mem.swapout.average
mem.vmmemctl.average
mem.vmmemctltarget.average
mem.overhead.average
disk.usage.average
net.usage.average
sys.uptime.latest
sys.heartbeat.summation
mem.consumed.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
mem.swapused.average
mem.sharedcommon.average
mem.heap.average
mem.heapfree.average
mem.state.latest
mem.swapin.average
mem.swapout.average
mem.vmmemctl.average
mem.overhead.average
disk.usage.average
net.usage.average
sys.uptime.latest
clusterServices.cpufairness.latest
clusterServices.memfairness.latest
cpu.reservedCapacity.average
mem.reservedCapacity.average
mem.consumed.average
mem.sysUsage.average

However, just because a metric is available, doesn't make it available in the interval you'd like. I find the most informative method is to see what sampling intervals are available by using Get-StatInterval, and then query for 5 samples of each metric...

[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....

[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

A couple of final points to note

  • To retrieve data for the Realtime interval, use the -Realtime option, not -IntervalSecs 20
    • All metrics at this interval are averages
  • Virtual Centre generally drops metrics during roll-ups etc (subject to configuration), therefore for the fullest range of metrics, check with the ESX direct (though only 20sec and 300sec intervals are available).
    • 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.

Script Extracts

VM's with Datastores List

List of Virtual Machines, and their datastores (with usage)

$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"

VM's with Host and Cluster List

$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
}

VM's Inventory CSV

$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