Purpose:
- Produce a PSCustomObject which exposes properties (and perhaps later, methods) that represent every virtual machine which runs on one or more given VMWare ESX host servers.
Requirements:
- The SSH automation tool plink.exe from the PuTTY project must be in your $env:path.
Input and Parameters:
- [pipeline]: String or strings containing IP addresses and/or hostnames of ESX servers to query. [required]
- Credential: A PsCredential object which can be produced using the Get-Credential cmdlet. This holds your username and password for the servers in question. [required]
- Verbose: If this switch is present, script will output timestamped messages suitable for logging.
- Snapshot: If this switch is present, script will query each VM to see if it has any snapshots. This can take several seconds per VM and can add up quickly on large farms so use with care.
- Help: Shows a brief help message.
Output:
- A PsCustomObject with these properties:
- VmHost: ESX server
- VmName: Not actually the VM name at present, but rather the filename portion of the VMX configuration file. Improvement needed here.
- VMXpath: This is the key that vmware-cmd uses when working with the virtual machines. It is the full VMFS path to the virtual machine configuration file.
- HasSnapshot: If the snapshot switch was provided as input, the script will execute “vmware-cmd <vmx path> hassnapshot” and return true or false accordingly for this property.
- RemoveSnapshots: Doesn’t do anything yet unfortunately, but this is coming.
This script, Get-hVm.ps1 (H for Hal, doncha know) uses the “vmware-cmd” Perl utility included with ESX to work with the service. I’ve been asked why I did it this way, as there are a plethora of APIs (SOAP, VmCOM, VmPerl, VIX) that are undoubtedly more full-featured and even more appropriate than the vmware-cmd utility. Here are my reasons:
- I didn’t have to learn the APIs, most of which are made for programmers, not system administrators.
- As previously mentioned on this blog, VMware is soon to have their own PS cmdlets. Doesn’t make sense to expend a crazy amount of time on this task.
- The SOAP API requires valid, trusted SSL certificates to be installed on each host. In my non-prod environment this is a pain.
- The COM API is not installed by default on ESX (it is on the free VMware Server) as it is being obsoleted. Plus, I looked into it and everything is asynchronous, so I’d have to do a lot of loops to wait for stuff to come back. It would complicate things a lot for me.
Attached at the bottom of this post is my rough draft version. Functionally it’s only about 50% complete to my liking, but it may be useful to some as it is. Its results are very predictable as long as you know about the…
Known Bugs:
- If your VMX path contains certain characters such as parentheses or unenclosed spaces, the code does not parse it that well. This only affects the HasSnapshot function right now. In fact, it only affects those VMs with the funky names. Others should show the correct snapshot status.
I’m open to input as to what methods you might find useful, or any other feedback you have. I’ll probably work on power status changes once I’ve got snapshot removal working. I could also change this from executing remotely using plink.exe to executing locally using the VmPerl toolkit (and thus, perl.exe vmware-cmd). The change should be trivial, but I’ll have to think on any benefits/drawbacks.
Example:
1# $esxhosts = "server1","server2","server3" 2# $cred = Get-Credential 3# $vms = $esxhosts | Get-hVm.ps1 -cred $cred -snap 4# $vms | Format-List VmHost,VmName | Select-Object -First 6 VmHost : eazye VmName : patch2k3x64sp2 VmHost : eazye VmName : Domino654 VmHost : eazye VmName : virtusa2 VmHost : eazye VmName : patchvistaent64 5# $vms | gm TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method System.Boolean Equals(Object obj) GetHashCode Method System.Int32 GetHashCode() GetType Method System.Type GetType() ToString Method System.String ToString() HasSnapshot NoteProperty System.Boolean HasSnapshot=False VmHost NoteProperty System.Management.Automation.PSObject VmHost=eazye VmName NoteProperty System.String VmName=patch2k3x64sp2 VMXpath NoteProperty System.Management.Automation.PSObject VMXpath=/vmfs/volumes/4642d... RemoveSnapshots ScriptMethod System.Object RemoveSnapshots();
File Attachment: Get-hVm.ps1

[...] PowerShell Script to Query VmWare ESX Servers [...]
[...] Get-hVm script from Hal [...]
Hal,
Before you put a lot of time in this. Vmware ESX will change dramatically. The new version will not have a service console and you can’t ssh to the server. You will have to get your data from one of the interface to the VCenter management console.
regards
Michiel
I’m well aware there will be changes. In fact, I’m on the VI-TK beta so my script has been obsoleted already. But no SSH? That seems extreme especially considering that ESX is both a singular product and part of VI. Those without VI/VC would have no other way to administer the product, right? Do you have any links to back that up?
the no SSH is only 3i… ESX will still have SSH in the future.
[...] from TechProsaic wrote a blog post here http://halr9000.com/article/445 about Using Plink to get Information about ESX and creating a custom object for the VM’s that the [...]
[...] PowerShell Script to Query VmWare ESX Servers [...]