none
Help with text comparison from a couple sources RRS feed

  • Question

  • Hi all, 

    So I have been working on this for some time and just can't seem to get my head around the right solution.

    Essentially what I trying to do is automate the updating of a list of users for a migration effort. 

    The data I have is as follows:

    One list comes in a spread sheet with names in the form Last, First 

    Another list is a spreadsheet that contains all staff but is in a form of UNCPath, URL

    Basically my end goal here is to take the first list and remove the people it contains from the second. 

    I found the UNCPath is in the form \\servername\folder1\folder2\folder3 where folder3 turns out to be the samaccount name.

    I was thinking of the following logic:

    1. Import csv files
    2. get samaccount name from either Name or combo of Givenname and Surname attributes
    3. get samaccount name from UNC path
    4. Compare names
    5. If a name matches, remove that entry from second csv file

    I have the following scripted but keep running into issues with compare

    $dirs = import-csv srclist.csv | select -ExpandProperty DIRECTORY
    $names = import-csv names2.csv -Header last,first
    
    foreach ($dir in $dirs)
    {
        foreach( $name in $names)
        {
    
            $sam1 = $dir | Split-Path -Leaf
            $firstfilter = $user.first
            $secondfilter = $user.last
            $sam2 = Get-ADUser -Filter {GivenName -like $firstfilter -and Surname -like $secondfilter} | select-object samaccountname
    
            if ($sam1 -eq $sam2)
            {
                $sam1
                $sam2
            }
        }
    }
    
    

    I am stuck here...the if statement was just me testing to see if I could actually get antything relevant with nested foreachs like that.

    Any help would be greatly appreciated!

    Tuesday, August 13, 2019 4:20 PM

All replies

  • Getting all AD users
    $list = @array()
    $names = import-csv names2.csv -Header last,first
    foreach ( $n in $names ) 
            {
              $user =  Get-ADUser -Filter {GivenName -eq '$n.first' -and Surname -eq '$n.last' }
    $list += $user
            }
    
    
    
    
    
    

    $dirs = import-csv srclist.csv | select -ExpandProperty DIRECTORY
    
    
    #compare both list
    
    (Compare-Object $($list.samaccountname) $dirs).inputobject
    

    • Proposed as answer by asharma5 Wednesday, August 14, 2019 10:08 AM
    Tuesday, August 13, 2019 4:41 PM
  • Thank you asharma5

    I think I see your logic but I get an error on the compare-object line.

    What I was trying to do was to grab the samaccountname from the folder name in the unc path

    \\servername\foldername\subfolder\samaccountname

    since the last folder in the path is the samaccount name.

    Would I need to loop through the $dirs and pull that off? 

    After failing miserably with regex to do so I landed on this which works nicely:

    foreach( $dir in $dirs)
        {
    
            $sam1 = $dir | Split-Path -Leaf
         }
    
    
    
    

    Would I need to have another array to store the names in? Or something similar to:

    $list = @()
    $names = import-csv names2.csv -Header last,first
    $dirs = import-csv srclist.csv | select -ExpandProperty DIRECTORY
    
    foreach ($name in $names)
    {
        $user =  Get-ADUser -Filter {GivenName -like '$name.first' -and Surname -like '$name.last'} | select-object samaccountname
        $list += $user
        
    }
    foreach($dir in $dirs)
    {
    
        (Compare-Object$($list.samaccountname) $dir).inputobject
    }

    Compare-Object$($list.samaccountname) : The term 'Compare-Object$($list.samaccountname)' is not recognized as the name of a cmdlet, function, script file, or operable 
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    Tuesday, August 13, 2019 5:56 PM
  • Get username from share path:

    ('\\servername\foldername\subfolder\samaccountname' -split '\\')[2]


    \_(ツ)_/

    Tuesday, August 13, 2019 6:04 PM