Automation

 View Only
Expand all | Collapse all

Get-VMs with expanded info per Datastore

  • 1.  Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 08:29 PM

    I am sure this has been covered many times already, but I am having trouble finding an exact match in the discussion archives...

    Our production deployment has a single data center with 4 main data store "Folders" ( ie NetApp1, NetApp2, NAS-1, NAS-2), and each of these folders has many individual data stores (for example, NetApp1 has 32 different data stores). We need a script that will traverse all data stores and report back all VMs with their associated detailed information in addition to the data store folder name. The output should look like this:

    VM Name: abc-name

    Data Store: "Production A1e"

    Data Store folder: NetApp1

    VM Allocated space: xxxGB

    VM Used space: yyyGB

    Power State: Powered On

    Owner: bob

    Project: Test

    Notes: Notes associated with bob's VM

    Some pseudo code would look like this:




    foreach $ds (Get-Datastore) {


    foreach vm (Get-VM -datastore $ds) {


    print "VM Name:" $vm


    print "Data Store: $ds"


    print "Data Store Parent" $ds.Parent


    print "VM Allocated Storage" $vm.Allocated


    print "VM Used Storage" $vm.Used


    print "Power Status" $vm.PowerStatus


    print "VM Owner" $vm.Owner


    print "VM Project" $vm.Project


    print "VM Notes" $vm.Notes





    }


    }



    This would give us a good breakdown on all VMs and which data store they are associated with. If possible, a CSV file would be much more preferred as we can awk/sed the output for more flexibility.

    Does anyone have a script like this? Or, something they can point me to?

    Thanks,

    -Ron



  • 2.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 08:56 PM

    Hi,

    Maybe you'll find interesting information there: link .

    Hope it helps

    Franck



  • 3.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 09:19 PM

    Thanks Franck,

    The reports look fantastic. Unfortunately, I cannot find anywhere that links the VM to the data store. Did I miss it?

    -Ron



  • 4.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 09:23 PM

    Since I think you want to see the space allocated and committed per datastore for each guest and not the total over of a guest over all datastores, I created the following script.

    $report = @()
    
    foreach($ds in Get-Datastore){
    	foreach($vm in (Get-VM -Datastore $ds)){
    		$dsUsage = ($vm | Get-View).Storage.PerDatastoreUsage | where {$_.Datastore.ToString() -eq ($ds | Get-View).MoRef.ToString()}
    		$row = "" | Select "VM Name", "Data Store", "Data Store folder","VM Allocated space","VM Used space","Power State",Owner,Project,Notes
    		$row."VM Name" = $vm.Name
    		$row."Data Store" = $ds.Name
    		$row."Data Store folder" = (Get-View $ds.parentfolderid).Name
    		$row."VM Allocated space" = "{0:f0}" -f (($dsUsage.Committed + $dsUsage.Uncommitted)/1GB)
    		$row."VM Used space" = "{0:f0}" -f ($dsUsage.Committed/1GB)
    		$row."Power State" = $vm.PowerState
    		$row.Owner = $vm.CustomFields
    		$row.Project = $vm.CustomFields
    		$row.Notes = $vm.Notes
    		$report += $row
    	}
    }
    $report
    

    I attached the script since the forum SW doesn't like square brackets.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 5.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 10:23 PM

    Thank You - very much! I am almost there. The only issue is the Data Store parent folder gives me this error and returns with no data:

    -


    Get-Folder Folder with id 'Folder-group-s19798' not found, using the specified filter

    -


    I was hoping to get the parent "folder" name for this particular data store. In this case, the data store name is "Windows ThinProvision B1e" and logically lives under the "NetApp1" folder (in the Inventory-->Datastores view). I created a simple script that gets info on this particular data store (Get-Datastore -Name "Windows ThinProvision B1e"), and it returned a bunch of information - but nothing about the parent folder name. Here is some output:

    DatacenterId : Datacenter-datacenter-619

    ParentFolderId : Folder-group-s19798

    DatastoreBrowserPath : vmstores:\vc2@443\cats\Windows ThinProvision B1e

    FreeSpaceMB : 515472

    CapacityMB : 1048320

    Accessible : True

    Type : VMFS

    Id : Datastore-datastore-7512

    Name : Windows ThinProvision B1e

    It appears I may need to query the "Id" or the "ParentFolderId" values for the real parent folder name. But, I cannot find a corresponding "ParentFolderId" in the cmdlet guide. Any advice?

    Thanks for the excellent help so far!



  • 6.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 10:32 PM

    OOPS - looks like I was using the first example that had "Get-Folder" instead of "Get-View". Using the Get-View function worked very well!

    Again, many thanks for the very fast and helpful replies!



  • 7.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 13, 2010 05:32 PM

    Sorry to open this back up, but for some reason the space calculations (Allocated, Free, Used) are showing a value of "0" for many of our VMs. However, these VMs show 40G allocated in the VC. Here is the code I am using:

    Connect-VIServer <server>

    -User

    </server>

    Get-Datastore | ForEach-Object {

    $Datastore = $_

    $Datastore | Get-VM | ForEach-Object {

    $VM = $_

    $AllocatedSpace = 0

    $FreeSpace = 0

    $VM.Guest.Disks | ForEach-Object {

    $AllocatedSpace += $_.Capacity

    $FreeSpace += $_.FreeSpace

    }

    $Report = "" | Select-Object "VM Name","Data Store","Data Store Folder","VM Allocated Space - MB","VM Used Space - MB","VM Free Space - MB","Power State",Owner,Project,Notes

    $Report."VM name"= $VM.Name

    $Report."Data Store"= $Datastore.name

    $Report."Data Store Folder" = (Get-View -Id $Datastore.ParentFolderId).Name

    $Report."VM Allocated Space - MB" = "" -f ($AllocatedSpace/1MB)

    $Report."VM Used Space - MB" = "" -f (($AllocatedSpace - $FreeSpace)/1MB)

    $Report."VM Free Space - MB" = "" -f ($FreeSpace/1MB)

    $Report."Power State" = $VM.PowerState

    $Report.Owner = (Get-Annotation -Entity $VM -CustomAttribute "Owner (email)").Value

    $Report.Project = (Get-Annotation -Entity $VM -CustomAttribute "Project").Value

    $Report.Notes = $VM.Description

    $Report

    }

    } | Export-Csv -Path c:\temp\VMsWithDatastoresInfo-1021am.csv -NoTypeInformation

    Doing some debugging, if I initially set the AllocatedSpace and FreeSpace vars to "" (nothing), I get the error below on some VMs:

    -


    Method invocation failed because http://System.String doesn't contain a method named 'op_Subtraction'.

    At line:19 char:64

    + $Report."VM Used Space - MB" = "" -f (($AllocatedSpace - <<<< $FreeSpace)/1MB)

    + CategoryInfo : InvalidOperation: (op_Subtraction:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

    -


    This tells me I am querying the wrong value from the DB.

    Any ideas?

    BTW - how do I post "code" in the forum? I don't see any formatting styles in the message creation tool.



  • 8.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 13, 2010 06:33 PM

    Select the Plain Text tab at the top of the edit window.

    You will then get a "Plain Text Markup Help" button at the top-right.

    I usually use the markup for Java code.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 9.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 13, 2010 06:39 PM

    That script relies on the fact that the Guest.Disks objects contain the space usage and free values.

    But that is not the case when there are no VMware Tools installed on the guest.

    The other script doesn't rely on the VMware Tools being installed.

    When you populate the variable with "", it becomes a String.

    And there no operation know to substract the value from a blank string.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 10.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 13, 2010 06:44 PM

    LucD,

    Thanks for the info. I chose the first script because the one you posted seems to run forever and ever without any output. I just figured something was wrong.

    Is there a way to include some sort of "echo working" statement as each datastore and VM is being interrogated?

    -Ron



  • 11.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 13, 2010 07:01 PM

    The attached version will display every $row on screen instead of accumulating them in an array ($report).

    You should also be aware that the script only takes the space allocated and used on the specific datastore.

    If you have a guest that uses space on two or more datastores, that will be reported as two or more rows.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 12.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 13, 2010 07:03 PM

    And this version displays a line on screen showing the current datastore and guest being handled.

    The result is still accumulated in array $report, which you can export to a CSV for example.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 13.  RE: Get-VMs with expanded info per Datastore

    Posted Jul 12, 2010 09:18 PM

    Hi Ron,

    I made a script according to your pseudo code. I hope it does all that you want.

    Get-Datastore | ForEach-Object {
      $Datastore = $_
      $Datastore | Get-VM | ForEach-Object {
        $VM = $_
    	$AllocatedSpace = 0
    	$FreeSpace = 0
    	$VM.Guest.Disks | ForEach-Object {
    	  $AllocatedSpace += $_.Capacity
    	  $FreeSpace += $_.FreeSpace
    	}
    	$Report = "" | Select-Object "VM Name","Data Store","Data Store Folder","VM Allocated Space","VM Used Space","Power State",Owner,Project,Notes
    	$Report."VM name"= $VM.Name
    	$Report."Data Store"= $Datastore.name
    	$Report."Data Store Folder" = (Get-Folder -Id $Datastore.ParentFolderId).Name
    	$Report."VM Allocated Space" = $AllocatedSpace
    	$Report."VM Used Space" = $AllocatedSpace - $FreeSpace
    	$Report."Power State" = $VM.PowerState
    	$Report.Owner = (Get-Annotation -Entity $VM -CustomAttribute Owner).Value
    	$Report.Project = (Get-Annotation -Entity $VM -CustomAttribute Project).Value
    	$Report.Notes = $VM.Description
    	$Report
      }
    } | Export-Csv -Path VMsWithDatastoresInfo.csv -NoTypeInformation
    

    Regards, Robert