Hi,
I want to pullout Disk space used and free for VM form the list of specific VM like import from csv VM1,VM2,VM3 etc
Get-VM | ForEach-Object {
$VM = $_
$_.Guest.Disks | ForEach-Object {
$Report = "" | Select-Object -Property VM,Path,Capacity,FreeSpace
$Report.VM = $VM.Name
$Report.Path = $_.Path
$Report.Capacity = $_.Capacity
$Report.FreeSpace = $_.FreeSpace
$Report
}
} | export-csv c:\FreeSpace.csv -notypeinformation
So need to help in modification in the script.It should pull from the list
of specific VMs rather than from entire VC.
thanks
vmguy
Provided you input CSV looks like this
Name
VM1
VM2
VM3
you could do something like this
$report = @() Import-Csv "C:\VMnames.csv" -UseCulture | %{ $vm = Get-VM -Name $_.Name $vm.Guest.Disks | ForEach-Object { $row = "" | Select-Object -Property VM,Path,Capacity,FreeSpace $row.VM = $VM.Name $row.Path = $_.Path $row.Capacity = $_.Capacity $row.FreeSpace = $_.FreeSpace $Report += $row
} } $report | export-csv c:\FreeSpace.csv -notypeinformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello, vmhyperv-
A couple of ways that you could do this would be to either import the VMs' info from a CSV, or grab their names from a text file. Say that you have a CSV with the VM names in a columns "VMName", then you could just adjust your first line like:
Import-Csv c:\temp\myVMsInfo.csv | %{Get-VM -Name $_.VMName} | ForEach-Object {
...
Or, if you have a text file with one VM name per line, you could adjust the first line of your code like:
Get-VM -Name (Get-Content c:\temp\MyVMNames.txt) | ForEach-Object {
...
How do those do for you?
Provided you input CSV looks like this
Name
VM1
VM2
VM3
you could do something like this
$report = @() Import-Csv "C:\VMnames.csv" -UseCulture | %{ $vm = Get-VM -Name $_.Name $vm.Guest.Disks | ForEach-Object { $row = "" | Select-Object -Property VM,Path,Capacity,FreeSpace $row.VM = $VM.Name $row.Path = $_.Path $row.Capacity = $_.Capacity $row.FreeSpace = $_.FreeSpace $Report += $row
} } $report | export-csv c:\FreeSpace.csv -notypeinformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Right now i executing the script to pullout report for around 600 VMs but it taking time.Any thingbe done to execute the script fast ?
thanks
vmguy
This should be a bit faster since it uses only 1 call to the Get-VM cmdlet
$report = @() $vmNames = Import-Csv "C:\VMnames.csv" -UseCulture | %{$_.Name} foreach($vm in Get-VM -Name $vmNames){ $vm.Guest.Disks | ForEach-Object { $row = "" | Select-Object -Property VM,Path,Capacity,FreeSpace $row.VM = $VM.Name $row.Path = $_.Path $row.Capacity = $_.Capacity $row.FreeSpace = $_.FreeSpace $Report += $row
} } $report | export-csv c:\FreeSpace.csv -notypeinformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Will check and let you know.Earlier script still its running so its seems i need to stop and run this.
thanks
vmguy
LucD,
it shows error like
Get-VM : 11/10/2011 7:04:11 PM Get-VM VM with name 'ASTPA3KLRWH91' was not found, using the specified filter(s).
At E:\script\vm-freespace-list.ps1:9 char:22
+ foreach($vm in Get-VM <<<< -Name $vmNames){
+ CategoryInfo : ObjectNotFound: (:) [Get-VM], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
Get-VM : 11/10/2011 7:04:11 PM Get-VM VM with name 'ASTPA3KLRWH94' was not found, using the specified filter(s).
At E:\script\vm-freespace-list.ps1:9 char:22
+ foreach($vm in Get-VM <<<< -Name $vmNames){
+ CategoryInfo : ObjectNotFound: (:) [Get-VM], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
Please let help on this.
thanks
vmguy
That error is just saying it can't find a VM with that name. Are you connected to the correct vCenter server?
If you connect to your vCenter server and type "Get-VM -Name ASTPA3KLRWH94" do you get a response?
Make sure you don't have any typos.
Hi,
I am connected to correct vcenter server.
thanks
vmguy
Hi Matt,
can you merge your script (
Get-VM -Name (Get-Content c:\temp\MyVMNames.txt) | ForEach-Object {
with the below scipt
Get-VM | ForEach-Object {
$VM = $_
$_.Guest.Disks | ForEach-Object {
$Report = "" | Select-Object -Property VM,Path,Capacity,FreeSpace
$Report.VM = $VM.Name
$Report.Path = $_.Path
$Report.Capacity = $_.Capacity
$Report.FreeSpace = $_.FreeSpace
$Report
}
} | export-csv c:\FreeSpace.csv -notypeinformation
thanks
vmguy
Sure.
Get-VM -Name (Get-Content c:\temp\MyVMNames.txt) | ForEach-Object { $VM = $_ $_.Guest.Disks | ForEach-Object { $Report = "" | Select-Object -Property VM,Path,Capacity,FreeSpace $Report.VM = $VM.Name $Report.Path = $_.Path $Report.Capacity = $_.Capacity $Report.FreeSpace = $_.FreeSpace $Report} } | export-csv c:\FreeSpace.csv -notypeinformation
You need to provide the name of the VM, not the FQDN.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
I am trying through the Name of the VM as well FQDN both.Let me try this one as i have to submit today the report for around 600 vms with disk info.
Thanks
vmguy
LucD,
It worked but its generating the output in KB.can we pullout the output in GB .please let me know
$report = @() Import-Csv "C:\VMnames.csv" -UseCulture | %{ $vm = Get-VM -Name $_.Name $vm.Guest.Disks | ForEach-Object { $row = "" | Select-Object -Property VM,Path,Capacity,FreeSpace $row.VM = $VM.Name $row.Path = $_.Path $row.Capacity = $_.Capacity $row.FreeSpace = $_.FreeSpace $Report += $row
} } $report | export-csv c:\FreeSpace.csv -notypeinformation
thanks
vmguy
Having trouble getting VM disk space? This tool makes it easy..
Thanks parker
But i am looking through script as i have more than 5000 vms and i want to pull out for specific 600 vms.I want to genrate report from Power Cli.
thanks
vmguy
Sure, try this
$report = @() Import-Csv "C:\VMnames.csv" -UseCulture | %{ $vm = Get-VM -Name $_.Name $vm.Guest.Disks | ForEach-Object { $row = "" | Select-Object -Property VM,Path,Capacity,FreeSpace $row.VM = $VM.Name $row.Path = $_.Path $row.Capacity = [math]::Round($_.Capacity/1MB,2) $row.FreeSpace = [math]::Round($_.FreeSpace/1MB,2) $Report += $row
} } $report | export-csv c:\FreeSpace.csv -notypeinformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference