VMware Cloud Community
farkasharry
Hot Shot
Hot Shot
Jump to solution

Need script to list VM Name, Guest OS, Datastore...

I need to move a large number of VM's with MS Server 2008 to another storage.
Could somebody provide me a small script to get a list about: all VM's with GuestOS Type (MS Server 2008 filtered), Datastore name and VM's overall size!

Thanks in advance

*** If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful! *** vExpert 2019, VCAP-DCA,VCP,MCSE,MCITS and some more...
Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, farkasharry-

How about something like:

Get-View -ViewType VirtualMachine -Property Name,Config.GuestFullName,Storage.PerDatastoreUsage,Summary.Storage -Filter @{"Config.GuestFullName" = "Windows.*2008.*"} | %{
   
## get the name(s) of the datastores on which this VM resides
    $strDatastoreNames = ($_.Storage.PerDatastoreUsage | %{Get-View -Id $_.Datastore -Property Name} | %{$_.Name}) -join ","
   
New-Object -TypeName PSObject -Property @{
        Name
= $_.Name
        GuestOS
= $_.Config.GuestFullName
       
"Datastore(s)" = $strDatastoreNames
       
## the amount of storage that the VM is using
        UsedGB = [Math]::Round($_.Summary.Storage.Committed / 1GB, 1)
       
## the total amount of storage that the VM _could_ use
        ProvisionedGB = [Math]::Round(($_.Summary.Storage.Committed + $_.Summary.Storage.Unommitted) / 1GB, 1)
    }
## end new-object
} | Select Name, GuestOS, "Datastore(s)", UsedGB, ProvisionedGB

This filters on VMs with an OS name ("GuestFullName") that matches "Windows.*2008.*", and returns their name, the GuestFullName, the datastore(s) on which they reside (possibly more than one), and the used- and provisioned disk space in GB.

The return items are objects, so you can format/select/export the info as you please, using, say, Format-Table, Select-Object, Export-Csv, etc.

And, since it uses the Get-View cmdlet, it should run pretty quickly.

How does that do for you?

View solution in original post

Reply
0 Kudos
3 Replies
jamesbowling
VMware Employee
VMware Employee
Jump to solution

Here is a script that can get you at least started.  It lists VMs with Name and Disk sizes.  You will need to modify it to gather the other information you need.

http://vsential.com/?attachment_id=273

James B. | Blog: http://www.vSential.com | Twitter: @vSential --- If you found this helpful then please awards helpful or correct points accordingly. Thanks!
Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, farkasharry-

How about something like:

Get-View -ViewType VirtualMachine -Property Name,Config.GuestFullName,Storage.PerDatastoreUsage,Summary.Storage -Filter @{"Config.GuestFullName" = "Windows.*2008.*"} | %{
   
## get the name(s) of the datastores on which this VM resides
    $strDatastoreNames = ($_.Storage.PerDatastoreUsage | %{Get-View -Id $_.Datastore -Property Name} | %{$_.Name}) -join ","
   
New-Object -TypeName PSObject -Property @{
        Name
= $_.Name
        GuestOS
= $_.Config.GuestFullName
       
"Datastore(s)" = $strDatastoreNames
       
## the amount of storage that the VM is using
        UsedGB = [Math]::Round($_.Summary.Storage.Committed / 1GB, 1)
       
## the total amount of storage that the VM _could_ use
        ProvisionedGB = [Math]::Round(($_.Summary.Storage.Committed + $_.Summary.Storage.Unommitted) / 1GB, 1)
    }
## end new-object
} | Select Name, GuestOS, "Datastore(s)", UsedGB, ProvisionedGB

This filters on VMs with an OS name ("GuestFullName") that matches "Windows.*2008.*", and returns their name, the GuestFullName, the datastore(s) on which they reside (possibly more than one), and the used- and provisioned disk space in GB.

The return items are objects, so you can format/select/export the info as you please, using, say, Format-Table, Select-Object, Export-Csv, etc.

And, since it uses the Get-View cmdlet, it should run pretty quickly.

How does that do for you?

Reply
0 Kudos
farkasharry
Hot Shot
Hot Shot
Jump to solution

Wow, thanks Matt!

It works like a charm! The only thing missing to my happiness is a line to count the machines, and a Filter for different datastores.

But the correct answer and the points have been definetly earned by you!

Thanks a lot

*** If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful! *** vExpert 2019, VCAP-DCA,VCP,MCSE,MCITS and some more...
Reply
0 Kudos