Author Posts

January 6, 2016 at 11:42 am

I have two csv files
1. contains a list of VM's I need to do some to.
2. Contains a list of all VM's in the company in a separate column is the vCenter Server it lives on.

I need to do a ForEach loop to take the server name from the first csv and search for it in the second csv file and return to me the column of the vCenter Server.

I have tried this by turning the second CSV file into a hash table, but when I seach it outputs the vCenter server of all servers, not the ones in my first CSV.

$VMs = Import-Csv "$env:USERPROFILE\Desktop\Find\VM's.csv"
$VMInfo = Import-Csv "$env:USERPROFILE\Desktop\Find\VmInfo.csv" -Encoding UTF8 
$Lookup = $VMInfo | Group-Object -AsHashTable -AsString -Property VmName

ForEach ($VM in $VMs){
$Lookup.$VM.VIServer
}

If I change the line

$Lookup.$VM.VIServer
to the specific Server Name It works
$Lookup.ServerNameVIServer

Thanks for the help in advance.

Bret

January 6, 2016 at 11:53 am

$VMs = Import-Csv "$env:USERPROFILE\Desktop\Find\VM's.csv"
$VMInfo = Import-Csv "$env:USERPROFILE\Desktop\Find\VmInfo.csv" -Encoding UTF8 

foreach ($vm in $vms) {
  $vminfo | where servername -eq $vm.servername
}

You mean something like that? I'm not certain where the hash table comes into play. This assumes each CSV has a "servername" column that contains what you want, but I'm not entirely certain from your post what the data actually is, so I imagine you'll have to adjust. I'm also at a loss as to what Group-Object was accomplishing for you, so it's entirely possible I'm missing your goal entirely.

January 6, 2016 at 12:00 pm

Thanks for the reply.
So my first csv has one column "ServerName"
The second csv has 3 columns: "VmName", "ViServer", and "FQDN".
I need to do a ForEach loop on the servers in the first csv, and search the second csv for the same server name, then return the vCenter server listed in the second column.

January 6, 2016 at 12:06 pm

$VMs = Import-Csv "$env:USERPROFILE\Desktop\Find\VM's.csv"
$VMInfo = Import-Csv "$env:USERPROFILE\Desktop\Find\VmInfo.csv" -Encoding UTF8 

foreach ($vm in $vms) {
  $vminfo | where vmserver -eq ($vm.servername) | select vmserver,viserver
}

Would that not do it?

January 6, 2016 at 12:18 pm

Perfect, thank you! I was obviously making this way more complicated than it needed to be.
I appreciate the help.