Searching Arrays

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Stuart Stuart 3 days, 17 hours ago.

  • Author
    Posts
  • #65293
    Profile photo of Stuart
    Stuart
    Participant

    Hello,

    I am trying to search an array. Wondered if I could get some pointers please.

    This line populates the variable array as follows with a list of employeeIDs and their account names:

    $ADUsers = Get-ADUser -Filter * -Properties employeeid | Where-Object {($_.employeeid -notcontains $null)} | Select-Object employeeID, samaccountname

    I have a CSV which I am importing, which contains a list of employeeIDs and corresponding phone numbers. I import the CSV using the importCSV commendlet.

    I then want to search the $ADUsers array for matching EmployeeIDs from the imported CSV.

    I can see the -contains command lets me do this:

    $ADUsers.employeeid -contains "13583"

    It returns true, since 13583 is listed in the array. However what I need to do, is then pull out what the corresponding sAMAccountName is for that employeeID from the $ADusers array.

    Basically I am going to use the CSV (provided by a client) to set phone numbers on matching employeeIDs. Nice if they just provided an account name to begin with, but such is life.

    Not sure how to do that. Please could someone kindly advise.

    Many Thanks

  • #65296
    Profile photo of Dudebro
    Dudebro
    Participant

    Something like this might work.

    ForEach ( $Item in $CsvFile )
    {
        $ADUser = $ADUsers | Where-Object { $_.EmployeeId -EQ $Item.EmployeeId }
        If ( $ADUser ) { # Set phone number or whatever based on $ADUser.SAMAccountName }
    }
    
  • #65298
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    It might be easier to loop over the list provided by the client with the employee IDs and query the AD user objects individually instead of getting all AD user objects. Getting all objects puts more load on the domain controllers if you've thousands of users in your domain. The list to be updated is usually smaller than the number of users in a domain.

    Example (not tested):

    $phoneUpdateList = Import-Csv -Path phoneupdatelist.csv
    foreach ($phoneItem in $phoneUpdateList) {
      try {
        Get-ADUser -LdapFilter "(employeeID=$($phoneItem.EmployeeID))" -Properties employeeID, telephoneNumber, samAccountName -Error Action Stop|
          Set-ADUser -OfficePhone $phoneItem.Number
      } catch {
        Write-Error $_.Exception
      }
    }
    

    Please let me know thoughts. I hope above helps.

  • #65340
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    A couple of small updates to Daniel's code. When you search for a user with filter, it returns NULL and does not produce a terminating error. You need to check if the user search returned something and then attempt to set the attribute:

    $phoneUpdateList = Import-Csv -Path phoneupdatelist.csv
    foreach ($phoneItem in $phoneUpdateList) {
        $user = Get-ADUser -Filter {EmployeeID -eq $($phoneItem.EmployeeID)} -Properties employeeID, telephoneNumber, samAccountName
        if ($user) {
            try {
                Set-ADUser -Identity $user -OfficePhone $phoneItem.Number -ErrorAction Stop
                "Successfully set phone number {0} on employee {1}" -f $phoneItem.Number, $phoneItem.EmployeeID
            } catch {
                "Failed to set phone number {0} on {1}. {2}" -f $phoneItem.Number, $phoneItem.EmployeeID, $_.Exception
            }
        }
        else {
            "User with employee id {0} not found" -f $phoneItem.EmployeeID
        }
    }
    
  • #67182
    Profile photo of Stuart
    Stuart
    Participant

    Hello,

    This is just what I needed thanks.

    Seems to be a slight glitch which I cannot fix. The line:

    $user = Get-ADUser -Filter {EmployeeID -eq $($phoneItem.EmployeeID)} -Properties employeeID, telephoneNumber, samAccountName

    Doesnt want to play ball. It doesnt error, just doesnt populate the variable. I have checked manually that the command finds the account:

    $user = Get-ADUser -Filter {EmployeeID -eq "368"} -Properties employeeID, telephoneNumber, samAccountName

    And it does work. Ived tried various combinations:

    $user = Get-ADUser -Filter {EmployeeID -eq "$phoneItem.EmployeeID"} -Properties employeeID, telephoneNumber, samAccountName
    $user = Get-ADUser -Filter {EmployeeID -eq "$($phoneItem.EmployeeID)"} -Properties employeeID, telephoneNumber, samAccountName

    Even added another varaible before:

    $employeeid = $phoneItem.EmployeeID
        $user = Get-ADUser -Filter {EmployeeID -eq $($employeeid)} -Properties employeeID, mobile, samAccountName 

    But no cigar. Seems simple enough, but that commandlet just doesnt seem to accept variables. These variables are type system.object.

    I dont get it!

  • #67183
    Profile photo of Stuart
    Stuart
    Participant

    Ok this works:

        $employeeid = $phoneItem.EmployeeID
        $user = Get-ADUser -Filter {EmployeeID -eq $employeeid} -Properties employeeID, mobile, samAccountName 

    Interested to know if there woudl have been a better way though?

    Many THanks

You must be logged in to reply to this topic.