#Code to generate Report...
##-------------------------
$vm = gc "vm.txt"
&{foreach($vm in Get-VM -Location $DC){
$parent = Get-View $vm.ExtensionData.Parent
$path = $vm.Name
while($parent.Parent){
if($parent.Name -ne "vm"){
$path = $parent.Name + "\" + $path
}
$parent = Get-View $parent.parent
}
#<#
Get-Datastore -VM $vm |
Select @{N="Cluster";E={Get-Cluster -VM $vm | Select -ExpandProperty Name}},
@{N="DataStore";E={$_.Name}},
@{N="VM Name";E={$vm.Name}},
@{N="VM Path";E={$path}},
@{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($vm.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},
@{N="UsedStorage";E={Get-FriendlyUnit -value ($vm.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}}
}} $report | Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end
You could just pipe this to the Export-Xlsx function
Get-VM -Name $vm |
Select @{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
@{N="DataStore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}},
@{N="VM Name";E={$_.Name}},
@{N="VM Path";E={$path}},
@{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($_.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},
@{N="UsedStorage";E={Get-FriendlyUnit -value ($_.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}} |
Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
How are the VM names stored in that file vm.txt ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
vm1
vm2
vm3
vm4
So where do you have a problem ?
When I do
$vm = Get-Content vm.txt
Get-Datastore -VM $vm
it works without an issue.
Or do you get another error ?
Perhaps include the error message.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
there is no error. The results are all of the vm's in the datacenter instead of just the ones listed in the vm.txt file.
But you are asking for datastores if I'm correct ?
That's what the 'Get-Datastore -VM $vm' does.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
DOH!. How could we change the script to get the same info for just the vms in the vm.txt file?
Did you already try something like this
Get-VM -Name $vm |
Select @{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
@{N="DataStore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}},
@{N="VM Name";E={$_.Name}},
@{N="VM Path";E={$path}},
@{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($_.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},
@{N="UsedStorage";E={Get-FriendlyUnit -value ($_.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
actually that works but how do I export to excel file?
$report | Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end
You could just pipe this to the Export-Xlsx function
Get-VM -Name $vm |
Select @{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
@{N="DataStore";E={Get-Datastore -VM $_ | Select -ExpandProperty Name}},
@{N="VM Name";E={$_.Name}},
@{N="VM Path";E={$path}},
@{N="ProvisionedStorage";E={Get-FriendlyUnit -value ($_.ProvisionedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}},
@{N="UsedStorage";E={Get-FriendlyUnit -value ($_.UsedSpaceGB * 1GB) | %{"{0,7:f2} {1,2}" -f $_.Value,$_.Unit}}} |
Export-Xlsx -Path $ExcelFile -WorksheetName $DC -AppendWorksheet -SheetPosition end
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yeah I removed $Report...Thanks