locked
indexing two arrays in Powershell RRS feed

  • Question

  • Hi All,

    I try to index two arrays by matching values;

    I have Inventory of computer names with AD last logon date, I want to update lastlogon date column with new arrays lastlogon date column

    I tried to use below script, I don't get any error but its not updating Inventory array.

    Any Idea...

    ForEach($newad in $newads)
    {
        ForEach($Inventory in $Inventorys)
          {           
            If($newad.name -eq $Inventory.All_Workstations)
              {
                $Inventory.AD_lastlogondate = $newad.lastlogondate
              }
          }
      }

    $Inventory
    All_Workstations AD_lastlogondate
    Computer1 05/06/2019
    Computer2 06/06/2019
    Computer3 07/06/2019
    Computer4 08/06/2019
    Computer5 09/06/2019
    Computer6 10/06/2019
    Computer7 11/06/2019


    $newad
    Name lastlogondate
    Computer1 09/07/2019
    Computer2 09/07/2019
    Computer3 09/07/2019
    Computer4 09/07/2019
    Computer5 09/07/2019


    Tuesday, July 9, 2019 12:03 PM

All replies

  • $inventory | 
        ForEach-Object{
            $name = $_.Name
            $_.laslogondate = $newad | Where-Object{$_.Name -eq $name}
        }
    
    

    Those are not arrays.  They are object collections.

    To assign based on a key value use the keys to do this.


    \_(ツ)_/

    Tuesday, July 9, 2019 12:36 PM
  • Perfect, Its working

    Much appreciated  Thank you very much.

    Tuesday, July 9, 2019 1:20 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, July 10, 2019 2:54 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, July 16, 2019 9:09 AM