Can you help me out? I have a list of VM names I want to get details on from the vSphere PowerCLI. When I use get-content to read the flat-file list of VM names I am specifically interested in, how do I transform the name of the VM into a VM object so I can look up the details I want for each VM in my user-specified input list? This code does not quite work. Be gentle, I am beginner at this. Any help on this is greatly appreciated.
$MyList = Get-Content C:\Specific_List_of_VM_Names.txt
MyList | Get-VMGuest | Select VmName -ExpandProperty Disks |
Select VmName, Path, $hdCapacity, $hdSpaceUsed, $hdFreeSpace, $hdPercFree |
Export-Csv $filelocation -NoTypeInformation -Force # Export output to csv
The Get-VMGuest cmdlet requires the -VM parameter (which you provide through the pipe).
$MyList = Get-Content C:\Specific_List_of_VM_Names.txt $MyList | %{Get-VM $_ | Get-VMGuest | Select VmName -ExpandProperty Disks} | Export-Csv $filelocation -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I believe LucD has already provided a good answer in a recent post http://communities.vmware.com/thread/225599
You'll still need to import as LucD just said
$MyList = Get-Content C:\Specific_List_of_VM_Names.txt
$report = @()
$MyList | %{
$vm = Get-VM $_
$vm | Get-HardDisk | %{
I'm relatively new as well and not sure how to format the code in the post as LucD does, but if you start out as above and then follow how he structured in the link provided you should be able to gather your information into the collection to export to CSV. I've used this in the past to good success. Welcome to the fun world of PowerCLI.
Josh Atwell
http://day2dayadmin.blogspot.com
Thank you, this works. However I have two follow-up questions: If several server names are passed as input but do not exist, I get
Get-VM: 10/12/2009 4:52:25 PM Get-VM VM with name 'my name;' not found, using the specified filter(s).
However, this takes a long time to execute. How can I speed this up (i.e, quickly bypass server names that don't exist
so the script runs more quickly? Secondly, how can I suppress this specific message? I'm not sure what the
trap condition should be for this in the trap statement. The problem is the input file will contain many server names,
some of which are VM's and some which aren't. So speed is now becoming a serious problem. Any help on this is greatly appreciated.
An If statement should take care of that, or if you're interested in finding information on both physical and virtual you could use PowerShell by pulling from WMI.
$MyList = Get-Content C:\Specific_List_of_VM_Names.txt
$MyList | %{
If (Get-VM $_ -ne $Null){
Get-VM $_ | Get-VMGuest | Select VmName -ExpandProperty Disks} |
Export-Csv $filelocation -NoTypeInformation
}
}
Josh Atwell
http://day2dayadmin.blogspot.com
The test with the Get-VM cmdlet is sufficient.
You can add the -erroraction parameter to suppress the messages that will show up on the screen.
.... if((Get-VM $vmName -ea SilentlyContinue) -ne $null){ .... # VM exists .... }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks. However, when I try your code I get:
An empty pipe element is not permitted.
+ Get-VM $_ | Get-VMGuest | Select VMName -ExpandProperty Disks} | <<<<
Any suggestions?
Here is my original code for reference which works, albeit very slowly:
MyList = Get-Content C:\Excel\Costing.txt
$filelocation = 'D:\MyFile.csv'
$hdCapacity = @{ N = "Capacity (GB)"; E = { % { $_.Capacity / 1000000000 } } }
$hdFreeSpace = @{ N = "FreeSpace (GB)"; E = { % { $_.FreeSpace / 1000000000 } } }
$hdSpaceUsed = @{ N="Used (GB)"; E={ ( $_.Capacity - $_.FreeSpace ) / 1000000000 } }
$hdPercFree = @{ N="PercFree"; E={ ::Round( ( 100 * ( $_.FreeSpace / $_.Capacity ) ),0 ) } }
$MyList | %{Get-VM $_ | Get-VMGuest | Select VmName -ExpandProperty Disks} |
Select VmName, Path, $hdCapacity, $hdSpaceUsed, $hdFreeSpace, $hdPercFree |
Export-Csv $filelocation -NoTypeInformation -Force # Export output to csv
I looked at it a little more and I must be missing where to determine freespace on the VM with PowerCLI. The other info can be determine through mathematics as you've already posted. I did get a running script that worked pretty quickly in my environment and properly skipped servers that did not exist.
$filelocation = "C:\Report.csv"
$MyList = Get-Content "C:\Servers.txt"
$myCol = @()
$MyList | %{
If ((Get-VM $_ -ea "SilentContinue") -ne $null) {
$myObj = "" |
Select Name,Disk,HDCapacity
$vm = Get-VM $_
$disks = $vm | Get-HardDisk
foreach ($disk in $disks){
$myObj.Name = $vm.Name
$myObj.Disk = $disk.Name
$myObj.HDCapacity = $disk.CapacityKB
$myCol += $myObj
}
}
}
$myCol | Export-Csv $filelocation -NoTypeInformation
I also attached a script that uses WMI to determine disk information from a list of servers. I don't recall who wrote this script and what contribution I made to it, but I've attached it nonetheless.This will work for all of your Windows VMs and Physical servers alike.
Hope this helps.
Josh Atwell
I must have the free space. Can you show me how to get the free space using your script?