Hey Everyone,
I'm very new to powershell, so forgive me if this is easy....however....
I want to generate a .csv with the following information:
VMname
VMDK file size
server location and resource pool location making sure to report correctly nested Resource Pools
I have attempted this and have had limited success. I have never been able to combine all needs into one output or have been able to handle the nested Resource Pools......
Thanks in advance
As you can see (nearly) everything is possible with PowerShell
$report = @() $vms = Get-VM foreach ($vm in $vms){ foreach($hd in $vm | Get-HardDisk){ $row = "" | select VMname, VMdisksize, server, respool $row.VMname = $vm.Name $row.VMdisksize = $hd.CapacityKB $row.server = ($vm | Get-VMHost).Name $rpImpl = $vm | Get-ResourcePool $rp = Get-View -id ($vm | Get-ResourcePool).Id $nested = "" while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){ $nested = "/" + $rp.Name + "/" + $nested $rp = Get-View $rp.Parent } $row.respool = $nested.TrimEnd("/") $report += $row } } $report | Export-Csv "C:\test.csv" -noTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Anybody?
Something like this
$report = @() $vms = Get-VM foreach ($vm in $vms){ foreach($hd in $vm | Get-HardDisk){ $row = "" | select VMname, VMdisksize, server, respool $row.VMname = $vm.Name $row.VMdisksize = $hd.CapacityKB $row.server = ($vm | Get-VMHost).Name $row.respool = ($vm | Get-ResourcePool).Name $report += $row } } $report | Export-Csv "C:\test.csv" -noTypeInformation
Note that you will have a line per hard disk for guests that have more than 1 hard disk.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
copy and pasted your response into VI toolkit window and ran it.
I keep getting the following over and over again :
Get-ResourcePool : object reference not set to an instance of an object.
At line:7 char:43
+ $row.respool = ($vm | Get-ResourcePool) <<<< .Name
You did connect to the VC server with the Connect-VIServer cmdlet before ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes I was connected.
I disconnected, reconnected then I ran it again, and this time it did complete, however the nested resource pools aren't there, only the resource pool the VM is in. If that resource pool is under another, it's not being displayed.
Thanks
Oops, I must have missed that requirement.
This should give the complete resource pool path.
$report = @() $vms = Get-VM foreach ($vm in $vms){ foreach($hd in $vm | Get-HardDisk){ $row = "" | select VMname, VMdisksize, server, respool $row.VMname = $vm.Name $row.VMdisksize = $hd.CapacityKB $row.server = ($vm | Get-VMHost).Name $rpImpl = $vm | Get-ResourcePool $rp = Get-View -id ($vm | Get-ResourcePool).Id $nested = "" while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){ $nested += ("/" + $rp.Name) $rp = Get-View $rp.Parent } $row.respool = $nested $report += $row } } $report | Export-Csv "C:\test.csv" -noTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
extremely close, only problem is that the path for the resource pool is reveresed.
so for something with a resource pool path of top level1\top level2 it's displaying top level2\top level1.....
As you can see (nearly) everything is possible with PowerShell
$report = @() $vms = Get-VM foreach ($vm in $vms){ foreach($hd in $vm | Get-HardDisk){ $row = "" | select VMname, VMdisksize, server, respool $row.VMname = $vm.Name $row.VMdisksize = $hd.CapacityKB $row.server = ($vm | Get-VMHost).Name $rpImpl = $vm | Get-ResourcePool $rp = Get-View -id ($vm | Get-ResourcePool).Id $nested = "" while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){ $nested = "/" + $rp.Name + "/" + $nested $rp = Get-View $rp.Parent } $row.respool = $nested.TrimEnd("/") $report += $row } } $report | Export-Csv "C:\test.csv" -noTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
simply awesome, thanks so much, it's exactly what I wanted.
Thanks for this script. Works nice!
Hi Luc,
Thanks again for you contribution!
I do have one question however. With the provided script, if a VM has multiple disks, the information is written on two lines.
For example this output:
VM1,17826816,"[Datastore1] VM1/VM1.vmdk",esx01,/RP01
VM1,22020096,"[Datastore1] VM1/VM1_1.vmdk",esx01,/RP01
I tried to change some stuff, but with no luck.
Could you help me out on this one?
Regards,
What exactly did you try to change ?
To have each VM on 1 line, even when it has multiple disks ?
If yes, that could be done by placing the disks in an array but then the Export-CSV would have a problem.
That is why I decided to have multiple lines.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc,
That exactly what i'm trying to do; So i want each VM on one line, with all the disks.
I have this script:
-
Get-Cluster |
%{$cluster = $_; get-vm -Location $_ |
#%{$vm = $_; $dsDiskInfo = get-harddisk -vm $_ | % {$_.FileName, $_.CapacityKB};
%{$vm = $_; $dsDiskInfo = get-harddisk -vm $_ | % {$_.FileName, $_.CapacityKB};
write-host $cluster.name $vm.name $vm.NumCPU $vm.MemoryMB $dsDiskInfo
}
}
-
Which works pretty good, only i want to use export-csv instead of write-host. Searching for a solution i come across this post and your script.
Hello Luc, I am trying to run your code and being a complete noob in the powershell game I am getting the following error. Can you help.
Error is Unexpected token 'vms' in expression or statement
Unexpected token 'vms' in expression or statement.
At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\disksize.
ps1:1 char:19
+ $report = @() $vms <<<< = Get-VM foreach ($vm in $vms){ foreach($hd in $vm |
Get-HardDisk){ $row = "" | select VMname, VMdisksize, server, respool $row.VMn
ame = $vm.Name $row.VMdisksize = $hd.CapacityKB $row.server = ($vm | Get-VMHost
).Name $rpImpl = $vm | Get-ResourcePool $rp = Get-View -id ($vm | Get-ResourceP
ool).Id $nested = "" while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name
-ne "Resources"){ $nested = "/" + $rp.Name + "/" + $nested $rp = Get-View $rp.P
arent } $row.respool = $nested.TrimEnd("/") $report += $row } } $report | Expor
t-Csv "C:\test.csv" -noTypeInformation
I copy and pasted your code directly into powershell cmd window and also created a ps1 file. Same error on both. What am I doing wrong LOL.
I suspect you access the community with an IE browser ?
There are some known problems (should be fixed somewhere in November).
I attached the file so you have the correct script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Cheers mate, will be using firefox from now on. Appreciate the help.
I have been trying and stumbling to find a way to accurately format powershell code in forums regardless of the browser used, and have come up with an approach in this blog post.
Would be happy to receive improvements which I'll reflect.
I was also very glad to see LucD's comment that there are improvements coming real soon.
Thanks for the tip, I'll try your recommendations in my next "code" post.
The hint that improvements are coming is in
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello All,
I'm also very new to powershell and i look at this post and try to play whit your script and to adjust it to my needs but with no luck ;-( I need to generate a report that will include also the datastore information in this report
Sorry with the incontinence
Thanks in advance
Guy