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!
Try like this
$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
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
yes - that is the complete list of possible values, and those values are only used for that one column name.
Thanks again
Try like this
$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
ok thanks again - much appreciated!
ok thanks - to clarify - you used the variable $row -
it seems these are actually columns - would you agree?
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