VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

datatore VM report

how can I get a report on what vms are on what datastores ?

I have this working but the format is not what I am looking for

Get-Content .\luns.txt | % { Get-vm -Datastore $_ | select name, @{N="Datastore";E={[string]::Join(',',(get-datastore -id $_.datastoreIdlist | select -ExpandProperty Name))}}} | Export-Csv vms_in_datastore_report.csv




I want to have the output like

datastorename1  VM1

                            VM2

datastorename2  VM3

                              VM4

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There are a couple of "issues".

  • there is a parenthesis missing at the start of the Expression code block
  • The DatastoreUrl property points to a VirtualMachineConfigInfoDatastoreUrlPair object, which has multiple properties. If you don't select the property, the Select-Object don't know how to display the values
  • A VM can have multiple datastores. To cope with that (especially when you to do an Export-Csv or similar), join the values together into one string

Try like this

Get-Datastore | Get-VM |

select Name, @{N="datastore"; e={($_ | get-view).config.datastoreurl.Name -join ' | '}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$dsNames = Get-Content .\luns.txt

Get-Datastore -Name $dsNames | %{

    $obj = [ordered]@{

        Datastore = $_.Name

        VM = ''

    }

    $i = 1

    (Get-View -Id $_.ExtensionData.VM -property Name).Name | %{

        If($i -gt 1){

            $obj.Datastore = ''

        }

        $obj.VM = $_

        $i++

        New-Object PSObject -Property $obj

    }

} | Export-Csv D:\Temp\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

tdubb123
Expert
Expert
Jump to solution

Thanks luc

is this another way to get my VM to datastore info?

foreach ($ds in (get-content luns.txt){

get-vm -datastore $ds | select Name, @{N="datastore"; e=$ds}}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That will work as well (there is always more than one way to achieve something in PowerShell).

I used the Get-View construct, since that tends to be a bit faster in larger environments.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

hi Luc

how come I dont get the results I want with this ?

get-datastore | get-vm | select Name, @{N="datastore"; e={$_ | get-view).config.datastoreurl}}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are a couple of "issues".

  • there is a parenthesis missing at the start of the Expression code block
  • The DatastoreUrl property points to a VirtualMachineConfigInfoDatastoreUrlPair object, which has multiple properties. If you don't select the property, the Select-Object don't know how to display the values
  • A VM can have multiple datastores. To cope with that (especially when you to do an Export-Csv or similar), join the values together into one string

Try like this

Get-Datastore | Get-VM |

select Name, @{N="datastore"; e={($_ | get-view).config.datastoreurl.Name -join ' | '}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

yes makes sense. thanks

0 Kudos
tdubb123
Expert
Expert
Jump to solution

hi lucd

playing around with this varation

$result = @()

$luns = get-content luns.txt

foreach ($lun in $luns) {

$vms = get-vm -datastore $lun

foreach ($vm in $vms) {

$datastore = get-vm $vm | Get-Datastore

$obj = New-Object psobject

$obj | add-member -MemberType NoteProperty -Name VM_NAme -Value $vm.Name

$obj | Add-Member -MemberType NoteProperty -Name Datastore_NAme -Value $datastore.Name

$result += $obj

}

}

$result | ft -AutoSize



Can i use a single foreach loop to get this to work?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is another way of doing the same.

Get-Datastore -Name (Get-Content luns.txt) -PipelineVariable ds |

    Get-View -Id $ds.ExtensionData.VM -Property Name |

    Select @{N='Datastore';E={$ds.Name}},

        @{N='VM';E={$_.Name}}

} | ft -AutoSize


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos