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
There are a couple of "issues".
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
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
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}}
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
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}}
There are a couple of "issues".
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
yes makes sense. thanks
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?
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