Help With Arrays and MatchInfo

Tagged: , ,

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Dan Davis Dan Davis 2 years ago.

  • Author
    Posts
  • #20639
    Profile photo of Dan Davis
    Dan Davis
    Participant

    Hi,
    I am having a problem with a script that I can't figure out with accessing values out of a MatchInfo Type. I will give a bit of background as i am new to PS so it may be that my workflow is not ideal.

    – Get a list of users from AD with specified values
    – Check a csv file to see if that user exists in the file
    – If the user is in the csv, get the values.
    – Compare the values between the AD object and the file

    Where I have a problem is getting the values from csv after the search

    This is what I am doing

    Get the csv

    $FromFile = Import-Csv $MyFile

    Check the file contains the user

    $SearchUser = $FromFile | Select-String -pattern $CN -casesensitive 

    I am then unable to retrieve any returned values, by doing something like

    $SearchUser.cn

    When I output the $SearchUser it looks like an array

    @{cn=test}

    But because the type has changed to MatchInfo I don't know how to retrieve the values.

    I have tried the below to try and make an array but to no avail.

    $var = @()
    $var += $FromFile | Select-String -pattern $CN -casesensitive
    $var.cn
    

    or

    $var =@($FromFile | Select-String -pattern $CN -casesensitive)
    $var.cn
    

    or

    [ARRAY]$var = $FromFile | Select-String -pattern $CN -casesensitive
    $var.cn
    

    Any help in returning the values or using an alternative to Select-String would be great.
    Regards, Dan.

  • #20646
    Profile photo of Craig Duff
    Craig Duff
    Participant

    When you use Import-CSV, you are creating an object with properties. Each line in the csv is an object, and each column is a property. Select-String searches a string for a pattern, but you aren't giving it strings.

    To get a list of users from AD use Get-ADUser
    To check if the user is in the csv file, assuming you are using the samaccountname, and samaccountname is the csv header for that column, you can do something like

    $samsInFile = $FromFile | Select-Object -ExpandProperty samaccountname
    $adUsers = Get-ADUser -Filter {someproperty -eq "somevalue"}
    ForEach($user in $adUsers){
        If($samsInFile -contains $user.samaccountname){ 
            #dosomething
        }
    }
    

    If the user is in the csv, you already have the values from the csv stored in $FromFile
    To compare the values between the ad user and the csv, you may need to add -Properites nameofproperty,anotherproperty to get-ADUser, then inside that If block do your comparisons

  • #20781
    Profile photo of Dan Davis
    Dan Davis
    Participant

    Hi Craig,
    Sorry its taken so long to get back to you, was one of those weeks last week.
    So I could find the user in the file but the issue was retrieving the associated values, cn, sn etc.
    Your point about the Select-String set me on the right path. Still need to get my head around the object thing.

    This works for me.

     $values = $FromFile | Select-Object -Property cn,GivenName, etc  | Where-Object  {$_.sAMAccountName -eq $ADsAM} 

    Thanks again, Dan.

You must be logged in to reply to this topic.