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

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

  1. shaik afroze

    HI,

    We have scale setup, while we are calling few queries like get-ciuser , Get-OrgVdc getting timed out as we have some 50000 users , is there any way can we call page wise . Kindly help me in solution

  2. Scott

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

  3. Neal

    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

  4. Virtu-Al

    @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 !

  5. Simon Price

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

  6. Simon Price

    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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.