Excluding or removing items from a multi column array

Welcome Forums General PowerShell Q&A Excluding or removing items from a multi column array

Viewing 2 reply threads
  • Author
    Posts
    • #219735
      Participant
      Topics: 1
      Replies: 0
      Points: 32
      Rank: Member

      I want the ability to pull from AD a list of computers as an array and then import a csv report which also contains computers as an array and compare them.  I think my problem is that both arrays don’t have identical columns in them so I’m stripping each temporarily down to just host name to host name to do the comparison.

      I’m looking for a new array that contains the computers that are in AD but missing off the list.  I want to keep all the attributes that I’ve already pulled (or pull them again I don’t really care) to be able to do… stuff… the action doesn’t really matter for this question against this modified array.

      $ADComputers = Get-ADComputer -Filter * -Properties * -SearchBase "OU=path,OU=path,OU=path,OU=path,DC=path,DC=path,DC=edu" | select-object Name, Description, Location, roomNumber, employeeID, serialNumber, extensionAttribute1, Company | Sort-Object Name
      $CurrentAD = $ADCopmuters.name
      $ReportComputers = Import-Csv "\\filepath\_Inventory.csv" | Select-Object Name
      $ReportComputers = $ReportComputers.Name
      $MissingComputers = $CurrentAD | where {$ReportComputers -notcontains $_}

      This will get me the host names of what computers I’m missing.  How do I get all the other attributes back (Description, location, roomNumber…. ect)?  If there is a better way to do this completely I’m also open to suggestions.

    • #219747
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Hi Joshua,

      Welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code or error messages or sample data or console output format it as code using the code tags “PRE“, please. Thanks in advance.

      Give this a try

      $ADComputers = Get-ADComputer -Filter * -Properties * -SearchBase “OU=path,OU=path,OU=path,OU=path,DC=path,DC=path,DC=edu” |
          Select-Object *, Description, Location, roomNumber, employeeID, serialNumber, extensionAttribute1, Company
      $ReportComputers = Import-Csv “\\filepath\_Inventory.csv”
      $MissingComputers = $ADComputers | where {$ReportComputers.name -notcontains $_.name}
      
      

      Instead of saving the property you need to compare in the variable, just reference it where needed. As far as the properties being populated, when I ran the first line as you had it, it would strip out DNSHostname, Distinguishedname, etc. If you want to use select, try as I have it listed with a select *, and then individual properties that are not already present in the propertyset. If you include a property that already exists, you’ll get an error like this

      
      select : The property cannot be processed because the property "name" already exists.
      
      

      I hope this helps.

      • This reply was modified 1 month, 1 week ago by Doug Maurer.
      • This reply was modified 1 month, 1 week ago by Doug Maurer.
    • #219759
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Just as an aside note: To reduce the stress you put on your DC and even to speed up your script you should avoid using the asterisk (*) for the parameter -Properties of the cmdlet Get-ADComputer oder Get-ADUser. Espexcially when you’re already using it for the -Filter parameter.
      A good way to make the code a little easier to read would be like this:

      $Properties = 'Name', 'Description', 'Location', 'roomNumber', 'employeeID', 'serialNumber', 'extensionAttribute1', 'Company'
      $SearchBase = 'OU=path,OU=path,OU=path,OU=path,DC=path,DC=path,DC=edu'
      $ADComputers = Get-ADComputer -Filter * -Properties $Properties -SearchBase $SearchBase | Sort-Object -Property Name
Viewing 2 reply threads
  • You must be logged in to reply to this topic.