Difference between revisions of "Script Extracts and Examples (PowerCLI)"
Jump to navigation
Jump to search
m (moved VI Toolkit (PowerShell) to Script Extracts and Examples (PowerCLI): Move of content from original PowerCLI/VIToolkit page) |
(Move of content from original PowerCLI/VIToolkit page (to help eliviate site perf probs)) |
||
Line 1: | Line 1: | ||
== Virtual Machine == | |||
=== VM's with Host and Cluster List === | |||
<source lang="powershell"> | <source lang="powershell"> | ||
$vms = Get-VM | sort -property Name | $vms = Get-VM | sort -property Name | ||
Line 365: | Line 9: | ||
</source> | </source> | ||
=== VM's Inventory CSV === | |||
<source lang="powershell"> | <source lang="powershell"> | ||
Line 422: | Line 66: | ||
</source> | </source> | ||
=== VM's with Snapshots Running === | |||
Be aware that there's a bug in the way <code>Get-Snapshot</code> handles VM's in VI4, in that for the snapshot size it tends to report the maximum size a snapshot file could become, not the actual size that it is. As a result there is a workaround to found here - http://blogs.vmware.com/vipowershell/2010/09/snapshot-size.html. I've found that this reports smaller sizes for VI3 snapshots as well (which match the actual disk usage in the few occasions I've bothered to check). | Be aware that there's a bug in the way <code>Get-Snapshot</code> handles VM's in VI4, in that for the snapshot size it tends to report the maximum size a snapshot file could become, not the actual size that it is. As a result there is a workaround to found here - http://blogs.vmware.com/vipowershell/2010/09/snapshot-size.html. I've found that this reports smaller sizes for VI3 snapshots as well (which match the actual disk usage in the few occasions I've bothered to check). | ||
Line 505: | Line 149: | ||
...then I've just replaced <code>Get-Snapshot</code> with <code>CalculateVMSnapshotsSizeMB</code> | ...then I've just replaced <code>Get-Snapshot</code> with <code>CalculateVMSnapshotsSizeMB</code> | ||
=== VM's Effective CPU Shares === | |||
Calculates the effective relative CPU shares of VM's contained within resource pools. Can only handle resource pool depth of 1. | Calculates the effective relative CPU shares of VM's contained within resource pools. Can only handle resource pool depth of 1. | ||
Line 584: | Line 228: | ||
</source> | </source> | ||
=== Ping All VM's On ESX === | |||
Useful sanity check prior to and after network level changes | Useful sanity check prior to and after network level changes | ||
<source lang="Powershell"> | <source lang="Powershell"> | ||
Line 623: | Line 267: | ||
</source> | </source> | ||
=== Enable Change Block Tracking === | |||
This script enables CBT for all VM's managed by the vCentre its run on. It runs through all VM’s managed by the VC and determines which can be changed, then run through and changes them. | This script enables CBT for all VM's managed by the vCentre its run on. It runs through all VM’s managed by the VC and determines which can be changed, then run through and changes them. | ||
Line 694: | Line 338: | ||
</source> | </source> | ||
=== VM's Recently Created === | |||
I run the following script twice a week (via a scheduled task), to generate a report of VM's recently created, as a catch-all reminder to check VM backup's, monitoring, etc etc have been set-up as required. The number of days history it searches through can either be altered in the script (the <code>$days</code> parameter), or passed as a command line parameter when run as a scheduled task. | I run the following script twice a week (via a scheduled task), to generate a report of VM's recently created, as a catch-all reminder to check VM backup's, monitoring, etc etc have been set-up as required. The number of days history it searches through can either be altered in the script (the <code>$days</code> parameter), or passed as a command line parameter when run as a scheduled task. | ||
Line 701: | Line 345: | ||
I use a username and password file for credentials, see [[#Store_Password_Securely|Store Password Securely]] for more info, plus a CSV with a list of vCentres in (one column with a heading of "vc"). | I use a username and password file for credentials, see [[#Store_Password_Securely|Store Password Securely]] for more info, plus a CSV with a list of vCentres in (one column with a heading of "vc"). | ||
<source lang="Powershell"> | <source lang="Powershell" enclose="div"> | ||
$UserFile = "User.fil" | $UserFile = "User.fil" | ||
$PassFile = "Pass.fil" # Encrypted file to store password in | $PassFile = "Pass.fil" # Encrypted file to store password in | ||
Line 891: | Line 535: | ||
</source> | </source> | ||
== ESX == | |||
=== ESX NIC Info === | |||
Provides a list of all vmnic speeds for ESX's connected to vCentre | Provides a list of all vmnic speeds for ESX's connected to vCentre | ||
<source lang="powershell"> | <source lang="powershell"> | ||
Line 911: | Line 555: | ||
</source> | </source> | ||
=== ESX Log Tail === | |||
'''''Note that the below only seems to work for ESX3.5, I gave up trying to get it work for both v3 and v4, its a lot easier to just enable [[ESX#ESXi_Tech_Support_Mode|ESXi Tech Support Mode]] and tail the log''''' | '''''Note that the below only seems to work for ESX3.5, I gave up trying to get it work for both v3 and v4, its a lot easier to just enable [[ESX#ESXi_Tech_Support_Mode|ESXi Tech Support Mode]] and tail the log''''' | ||
Line 977: | Line 621: | ||
</source> | </source> | ||
=== ESX Discovered Networks Hint === | |||
This function provides the discovered network hints for the network interface its passed. Bear in mind that its just a hint, for an ESX to be aware of a particular vLAN it needs to see traffic. If there's no traffic it will show nothing. | This function provides the discovered network hints for the network interface its passed. Bear in mind that its just a hint, for an ESX to be aware of a particular vLAN it needs to see traffic. If there's no traffic it will show nothing. | ||
Line 1,016: | Line 660: | ||
</source> | </source> | ||
=== ESX CDP Info === | |||
Adapted from posted by LucD on VMware forum http://communities.vmware.com/message/977487 | Adapted from posted by LucD on VMware forum http://communities.vmware.com/message/977487 | ||
<source lang="powershell"> | <source lang="powershell"> | ||
Line 1,029: | Line 673: | ||
</source> | </source> | ||
=== ESX BIOS, NIC and HBA Driver Versions === | |||
Getting BIOS, NIC and HBA driver versions is dependant on what's reported to the ESX by the hardware vendor's CIM provider. This script was written using various HP servers and may well only work for them. | Getting BIOS, NIC and HBA driver versions is dependant on what's reported to the ESX by the hardware vendor's CIM provider. This script was written using various HP servers and may well only work for them. | ||
<source lang="Powershell"> | <source lang="Powershell" enclose="div"> | ||
# =============================================================== | # =============================================================== | ||
# ESX Inventory Getter | # ESX Inventory Getter | ||
Line 1,179: | Line 823: | ||
</source> | </source> | ||
=== Daily ESX Up/Down Check === | |||
Simple script that runs every morning just before I start work to provide a basic sanity check email to glance through over the first tea of the day. | Simple script that runs every morning just before I start work to provide a basic sanity check email to glance through over the first tea of the day. | ||
<source lang="Powershell"> | <source lang="Powershell" enclose="div"> | ||
############################################################################################# | ############################################################################################# | ||
# | # | ||
Line 1,312: | Line 956: | ||
</source> | </source> | ||
== Storage == | |||
=== VM's with Datastores List === | |||
List of Virtual Machines, and their datastores (with usage) | List of Virtual Machines, and their datastores (with usage) | ||
<source lang="powershell"> | <source lang="powershell"> | ||
Line 1,321: | Line 965: | ||
</source> | </source> | ||
=== VM Storage Usage === | |||
'''Total''' storage usage (including any snapshots, logs, etc, etc), not just the VMDK files. | '''Total''' storage usage (including any snapshots, logs, etc, etc), not just the VMDK files. | ||
Adapted from post by Arnim van Lieshout http://www.van-lieshout.com/2009/07/how-big-is-my-vm/ | Adapted from post by Arnim van Lieshout http://www.van-lieshout.com/2009/07/how-big-is-my-vm/ | ||
Line 1,372: | Line 1,016: | ||
</source> | </source> | ||
=== ESX Storage Events === | |||
Creates a CSV of storage events (eg path up/downs). Useful in order to be analyse storage events, for example if you need to tally up to SAN logs etc. Note the <code> get-VIEvent </code> is limited to 1000 results, which will typically get 1 - 2 days worth depending on your infrastructure. To get a longer history would require multiple calls to <code> get-VIEvent </code> using the <code>-Start</code> and <code>-Finish</code> parameters | Creates a CSV of storage events (eg path up/downs). Useful in order to be analyse storage events, for example if you need to tally up to SAN logs etc. Note the <code> get-VIEvent </code> is limited to 1000 results, which will typically get 1 - 2 days worth depending on your infrastructure. To get a longer history would require multiple calls to <code> get-VIEvent </code> using the <code>-Start</code> and <code>-Finish</code> parameters | ||
<source lang="Powershell"> | <source lang="Powershell"> | ||
Line 1,401: | Line 1,045: | ||
</source> | </source> | ||
=== ESX Datastore to LUN Mapping (ESX3) === | |||
Its bizarrely difficult to be able to map VMware presented datastore names to the underlying LUN's, despite the fact that its readily available via the VI Client. The following was adapted from the work of ''Catman'' found in this forum thread - http://communities.vmware.com/thread/240466#240466. As I expect to maybe want some of the other fields available during the working my notes from working through this are to be found [[Datastore to LUN Mapping (PowerCLI)|here]] | Its bizarrely difficult to be able to map VMware presented datastore names to the underlying LUN's, despite the fact that its readily available via the VI Client. The following was adapted from the work of ''Catman'' found in this forum thread - http://communities.vmware.com/thread/240466#240466. As I expect to maybe want some of the other fields available during the working my notes from working through this are to be found [[Datastore to LUN Mapping (PowerCLI)|here]] | ||
<source lang="powershell"> | <source lang="powershell" enclose="div"> | ||
# Thieved and adapted from the good work by catman... | # Thieved and adapted from the good work by catman... | ||
# http://communities.vmware.com/thread/240466#240466 | # http://communities.vmware.com/thread/240466#240466 | ||
Line 1,477: | Line 1,121: | ||
=== ESX Datastore to LUN Mapping (ESX4) === | |||
Produces a similar output to the above, but works for ESX4 servers. | Produces a similar output to the above, but works for ESX4 servers. | ||
<source lang="powershell"> | <source lang="powershell" enclose="div"> | ||
function Get-DS-LUNs-v4 ($objESXs) { | function Get-DS-LUNs-v4 ($objESXs) { | ||
foreach ($cluster in (Get-View -ViewType "ClusterComputeResource")) { | foreach ($cluster in (Get-View -ViewType "ClusterComputeResource")) { | ||
Line 1,506: | Line 1,150: | ||
} | } | ||
</source> | </source> | ||
[[Category: | |||
[[Category: | [[Category:PowerCLI]] | ||
[[Category:Virtual Machine]] | |||
[[Category:ESX]] | |||
[[Category:Storage]] |