PowerCLI: Local stored VM’s One-Liner

So, you have installed ESX on your nice pair of RAID 1 72GB or 146GB disks and have a whole bunch of disk space left over, what do you do with it, well if your anything like me you will format it as a VMFS partition and use it for storing Templates or temporary VMs that you dont care if they die.

But what if your not like me, what if your one of those admins who doesnt really know where they are storing their VM’s, they dont even care, they just click to order the storage by free space and put it on the one that has the most free space – even if it is local storage (come on, we all know them).

The following one-liner will list all the VMDK files for VMs stored on your local storage.

Please note:  The where clause assumes your local storage has the word local in it, this can obviously be changed or you can use a –nomatch to exclude your san storage if needs be.

Get-Datastore |where {$_.Name -match local} |Get-VM |Get-HardDisk |select Filename |Export-Csv c:\LocalVMs.csv

You can also add multiple names for your local storage by added them with a | between them in the match string (Thanks LucD) like the following:

Get-Datastore |where {$_.Name -match store|local|storage} |Get-VM |Get-HardDisk |select Filename |Export-Csv c:\LocalVMs.csv

5 thoughts on “PowerCLI: Local stored VM’s One-Liner”

  1. Alan,
    In your one-liner that will list all the VMDK files for VMs stored on your local storage, I changed ‘local’ to ‘sc local’. The script now pulls in other datastores with ‘sc’ in the name. How can I exclude other matching characters and key off of SC-LOCAL only? {$_.name -match “SC-LOCAL“}

  2. What would the syntax look like to return results that did not contain ‘local’ in the name? Just the opposite of what you did above?

    Thanks

    Neal Edwards

  3. @Simon Price
    Thanks Simon, looks like there is a slight issue with this one as the object returned after the get-view does not contain the correct name, I have however modified it to create an easier funtion than the one I was using. Good job !

  4. Sorry .. the full solution:
    get-datastore|get-view | ? {$_.summary.multiplehostaccess -eq $false} | select @{n=”DS: Name”;e={$_.name}},@{n=”DS: Shared”;e={$_.summary.multiplehostaccess}} | % {get-vm -datastore $_.”ds: name”|get-harddisk|select @{n=”Datastore”;e={$_.filename.split(“[]”)[1]}},@{n=”Filename”;e={$_.filename.split(“[]”)[2]}},@{n=”CapacityGB”;e={“{0:n2}” -f ($_.capacitykb/1mb)}}}

  5. A way of searching for shared datstores is as follows:
    get-datastore|get-view | ? {$_.summary.multiplehostaccess -eq $false} | select @{n=”DS: Name”;e={$_.name}},@{n=”DS: Shared”;e={$_.summary.multiplehostaccess}}
    This will display any datastore that is only accessible by a single host, regardless of the friendly name.
    Cheers,
    Simon P

Leave a Reply