VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Import Lists of VMs with Different Column Titles

I need to run scripts against multiple csv input files and get lists of VMs.  However I'm running an issue.  My script is like this:

$path = "c:\inputfile.csv"

Import-Csv -Path $path -UseCulture | foreach{

$vmname = $_.vmname

get-vm $vmname}

However the problem is that the input files have multiple different column names for VMname. Some input files have VMname, some have Systemname, hostname, servername, server or VM as the column title for the VM name.

How can I modify my script so that I assign the $vmname to whichever of these column titles is used?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$path = "c:\inputfile.csv"

$propName = 'VMname','Systemname','hostname','servername','server','VM'


foreach($row in (Import-Csv -Path $path -UseCulture)){

   foreach($name in $propName){

     if($row | Get-Member -Name $name){

       break

     }

   }

   $vmname = $row."$name"

   get-vm $vmname

}


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Is that the discrete list of possibilities?

I mean 'VMname','Systemname', 'hostname', 'servername', 'server', 'VM'

And could in any of the files 2 of those names appear?


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

TheVMinator
Expert
Expert
Jump to solution

yes - that is the complete list of possible values, and those values are only used for that one column name.

Thanks again

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$path = "c:\inputfile.csv"

$propName = 'VMname','Systemname','hostname','servername','server','VM'


foreach($row in (Import-Csv -Path $path -UseCulture)){

   foreach($name in $propName){

     if($row | Get-Member -Name $name){

       break

     }

   }

   $vmname = $row."$name"

   get-vm $vmname

}


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

ok thanks again - much appreciated!

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

ok thanks - to clarify - you used the variable $row -

it seems these are actually columns - would you agree?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

A CSV is an array of rows, where each row has one or more columns.

But it's just a name, you can call it whatever you want :smileygrin:


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

0 Kudos