VMware Cloud Community
Pete_Howarth
Enthusiast
Enthusiast

Simple GetVM Properties Script from Imported CSV Not working

Hi -  I need some help with the script below.  It produces the output I want, but it's not limiting it to only the VM's in the imported csv variable.  I end up getting all the VM's in the vCenter.

Thanks,

 

$vms = Import-Csv c:\Temp\javavm.csv

$vms | foreach {
Get-VM | Select Name,@{N="Cluster";E={Get-Cluster -VM $_}}, NumCpu,MemoryGB | Export-Csv c:\temp\javavmreport.csv -NoTypeInformation -UseCulture
}

 

0 Kudos
3 Replies
LucD
Leadership
Leadership

You are getting all VMs for each iteration.
You could use the column name on the Get-VM.
The following assumes that column is name "Name"

Import-Csv c:\Temp\javavm.csv -PipelineVariable row |
ForEach-Object -Process {
    Get-VM -Name $row.Name | 
    Select Name,@{N="Cluster";E={Get-Cluster -VM $_}}, NumCpu,MemoryGB
} | Export-Csv -Path c:\temp\javavmreport.csv -NoTypeInformation -UseCulture

You could also do this with just 1 call to Get-VM

Get-VM -Name (Import-Csv c:\Temp\javavm.csv).Name | 
Select Name,@{N="Cluster";E={Get-Cluster -VM $_}}, NumCpu,MemoryGB |
Export-Csv -Path c:\temp\javavmreport.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Pete_Howarth
Enthusiast
Enthusiast

Hey Luc,

 

On the first script I'm getting this error.

 

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then
try the command again.
At line:3 char:18
+ Get-VM -Name $row.Name |
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

 

On the second script I'm getting this error


PS F:\> Get-VM -Name (Import-Csv c:\Temp\javavm.csv).Name |
Select Name,@{N="Cluster";E={Get-Cluster -VM $_}}, NumCpu,MemoryGB
Get-VM : Cannot validate argument on parameter 'Name'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the
command again.
At line:1 char:14
+ Get-VM -Name (Import-Csv c:\Temp\javavm.csv).Name |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

0 Kudos
Pete_Howarth
Enthusiast
Enthusiast

My fault. The second one works fine when I put a heading row 'Name' in the csv.

 

Thanks so much!

0 Kudos