issues in script to bring OU information

Welcome Forums General PowerShell Q&A issues in script to bring OU information

Viewing 1 reply thread
  • Author
    Posts
    • #188509
      Participant
      Topics: 2
      Replies: 1
      Points: 25
      Rank: Member

      Hi everyone,

       

      Thanks a lot for your time in advance in reading this. (it is going to be a big question)

      I'm a new bee with lil bit knowledge on PS. I have a situating where I'm creating a installation wrapper script.

      This script will be executed on many user machines, and they may\may not have Active directory access.

      But the script should be in such a way that, if the Machine object is part of a specific OU in active directory then, choose the configuration file accordingly.

      Will the OU information store some where in the WMI!? What is ADSI searcher, will it need any special access or pre-requisites to work properly.!?

      I have googled and got the below script using ADSI and see to be working in my virtual machine, but not on few other virtual machines (reasons unknown to me)

      $ComputerName = $env:ComputerName
      
      $ADSISearcher = New-Object System.DirectoryServices.DirectorySearcher
      
      $ADSISearcher.Filter = '(&(name=' + $ComputerName + ')(objectClass=computer))'
      
      $ADSISearcher.SearchScope = 'Subtree'
      
      $Computer = $ADSISearcher.FindAll()
      
      $OU = $($Computer.Properties.Item('distinguishedName'))
      
      I tried running the script on various other user machines (mixed AD access levels), and "$OU = $($Computer.Properties.Item('distinguishedName'))" is giving error.
      
      `You cannot call a method on a null-valued expression.
      At line:1 char:42
      +         $OU = $($Computer.Properties.Item < <<< ('distinguishedName')) + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull` But "$computer" is not a null value. it has below result( I have modified the LDAP info..) ` Path                                                        Properties —-                                                        ———- LDAP://CN=,OU=< ........>,OU=V< ...>,OU=N... {primarygroupid, iscriticalsystemobject, msds-supportede...`
      

      I tried running the script on various other user machines (mixed AD access levels), and "$OU = $($Computer.Properties.Item('distinguishedName'))" is giving error.

      `You cannot call a method on a null-valued expression.
      At line:1 char:42
      +         $OU = $($Computer.Properties.Item < <<< ('distinguishedName'))
      + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull`
      
      But "$computer" is not a null value. it has below result( I have modified the LDAP info..)
      
      `
      Path                                                        Properties
      ----                                                        ----------
      LDAP://CN=,OU=< ........>,OU=V< ...>,OU=N... {primarygroupid, iscriticalsystemobject, msds-supportede...`
      

      Now the question is,  Am I in the right direction, will ADSI searcher query this information from locally cached information.

      If yes, can you help me fix this issue!? If I'm all wrong, happy to correct myself. I'm sitting around this for days now, and help would be much appricitated.

       

    • #188800
      Participant
      Topics: 8
      Replies: 1274
      Points: 1,033
      Helping Hand
      Rank: Community Hero

      ADSI is not using cached information, it is querying Active Directory with LDAP. The error is most likely due to the .FindAll() not returning a computer, so when you attempt access properties\methods of $Computer, it is NULL. Assuming you are using a domain account it should find the computer, but normally you'd wrap the OU code in an if to ensure the find code found a computer, like so:

      $Computer = $ADSISearcher.FindAll()
      
      if ($Computer) {
          $OU = $($Computer.Properties.Item('distinguishedName'))
      }
      else {
          'Could not find computer {0}' -f $ComputerName
      }
      

      Take a look at the link below that shows a WMI method and some example ADSI code (with some warnings on memory leaks using .FindAll()):

      https://stackoverflow.com/questions/27381454/how-do-i-use-wmi-to-get-the-current-ou-of-a-computer-and-list-all-other-computer

Viewing 1 reply thread
  • You must be logged in to reply to this topic.