Import CSV and search

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Bret Robinson Bret Robinson 8 months, 4 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33559
    Profile photo of Bret Robinson
    Bret Robinson
    Participant

    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

    #33560
    Profile photo of Don Jones
    Don Jones
    Keymaster
    $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.

    #33563
    Profile photo of Bret Robinson
    Bret Robinson
    Participant

    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.

    #33566
    Profile photo of Don Jones
    Don Jones
    Keymaster
    $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?

    #33567
    Profile photo of Bret Robinson
    Bret Robinson
    Participant

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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.