Determine the OU which doesn’t exist from input file

Welcome Forums General PowerShell Q&A Determine the OU which doesn’t exist from input file

Viewing 5 reply threads
  • Author
    Posts
    • #211254
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      I have created a script, which pulls out the counts of computer, users and group from a specific ou’s. However, the ou name is the input file, and I also want to get an output that if OU name doesn’t exist after parsing through each domain, it should give me that output. but somehow I am not able to get that. Can someone guide me.

      The script as below, i get the desired output, but not he ou which doesnt exist.

       

      [email protected]()
      [email protected]()
      [email protected]()
      [email protected]()
      $domains = (Get-ADForest).Domains
      $OUs= Get-Content C:\temp\ouname.txt
      ForEach($Ou in $Ous){
      Foreach ($domain in $Domains)
      {

      $OUPaths= Get-ADOrganizationalUnit -filter ‘Name -like $OU’ -server $domain | select -expandproperty distinguishedName
      IF($OUPaths) {
      $usersinou=Get-ADuser -Filter * -SearchBase $OUPaths -server $domain
      $computersinou=Get-ADComputer -Filter * -SearchBase $OUPaths -server $domain
      $groupsinou=Get-ADgroup -Filter * -SearchBase $OUPaths -server $domain

      $data += [PSCustomObject]@{
      Site = $ou
      Domain=$domain
      Usercount = $usersinou.count
      ComputerCount=$computersinou.count
      GroupCount=$groupsinou.count
      }
      }
      }
      }
      $data |export-csv C:\temp\oumembersCount_empytSites.csv -NoTypeInformation

    • #211296
      Participant
      Topics: 13
      Replies: 1758
      Points: 3,153
      Helping Hand
      Rank: Community Hero

      Please, when you post code format it as code using the code tags “PRE“. Thanks

      You are using an IF statement, so you just need to add an ELSE. Also cleaned the code up a bit:

    • #211929
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      Please, when you post code format it as code using the code tags “PRE“. Thanks

      You are using an IF statement, so you just need to add an ELSE. Also cleaned the code up a bit:

      PowerShell
      43 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 7px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      $domains = (Get-ADForest).Domains
      $OUs= Get-Content C:\temp\ouname.txt
      $results = foreach ($Ou in $Ous){
      foreach ($domain in $Domains){
      $OUPath= Get-ADOrganizationalUnit FilterName -eq $OUServer $domain |
      Select expandproperty distinguishedName
      if ($OUPath) {
      $params = @{
      Filter = ‘*’
      SearchBase = $OUPath
      Server = $domain
      }
      $usersinou = Get-ADuser @params
      $computersinou = Get-ADComputer @params
      $groupsinou = Get-ADGroup @params
      [PSCustomObject]@{
      Site = $ou
      Domain=$domain
      Usercount = $usersinou.count
      ComputerCount=$computersinou.count
      GroupCount=$groupsinou.count
      Status = ‘Success’
      }
      }
      else {
      [PSCustomObject]@{
      Site = $ou
      Domain=$domain
      Usercount = 0
      ComputerCount=0
      GroupCount=0
      Status = ‘Failed’
      }
      }
      }
      }
      $results | Export-Csv C:\temp\oumembersCount_empytSites.csv NoTypeInformation
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Thanks Rob.

    • #218148
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      Not sure If i can revisit this question again, as what I want is the OU which doesnt exist “After parsing through each domain”.In this case, it shows for every domain.

    • #218160
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      I had used the variable input as $exist=false, and then change this to true whenever its exist. is this right approach or any better way.

       

    • #218166
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      … better would be to use

      … and check like this

Viewing 5 reply threads
  • The topic ‘Determine the OU which doesn’t exist from input file’ is closed to new replies.