Script Extracts and Examples (PowerCLI): Difference between revisions

From vwiki
Jump to navigation Jump to search
m (→‎VM Datastores: Renamed to "Script Extracts" and added "List VM's with Host and Cluster")
m (Typo fix)
 
(88 intermediate revisions by the same user not shown)
Line 1: Line 1:
Before being able to use any the following script you need to install...
#REDIRECT [[:Category:PowerCLI]]
* [http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Windows PowerShell]
* [http://www.vmware.com/go/powershell VI Toolkit]
 
The following website is a good resource for general PowerShell help
* http://powershell.com/cs/
 
== Getting Started ==
 
On the first run you need to allow the Toolkit to run properly by running...
Set-ExecutionPolicy RemoteSigned
 
Connect to the Virtual Centre 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 uklonvcp1 -User <user> -Password <pass>
 
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
 
<pre>
[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
</pre>
 
== 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-vmguest -VM (get-vm -name "winstg") </code>
| Get VM guest info (IP address, OS)
 
|-
| <code> Get-VM "winstg" <nowiki>|</nowiki> %{(Get-View $_.Id).config.uuid} </code>
| Get VM's UUID
 
 
 
|}
 
=== ESX Host ===
 
{|cellpadding="2" cellspacing="0" border="1"
! Command    !! Description
|-
| <code> get-vmhost -name uklonesxt1* <nowiki>|</nowiki> %{(Get-View $_.ID).Config.Product} </code>
| Get ESX software version (inc build no)
|-
| <code> Get-ScsiLun -VMHost uklonesxt1* -LunType disk <nowiki>|</nowiki> Get-ScsiLunPath </code>
| Get ESX SCSI paths info
|}
 
== Script Extracts ==
=== List VM's with Datastores ===
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"
 
 
=== List VM's with Host and Cluster ===
$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
}
 
 
 
 
[[Category:VMware]]

Latest revision as of 15:36, 4 October 2016

Redirect to: