Announcement

Collapse
No announcement yet.

Powershell help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Powershell help

    I have a Powershell script to list all disable AD users in a domain. I need to alter it to exclude certain OU's, but have no idea how. Any thoughts? The script (from MS) is below. Thanks

    $strFilter = "(&(objectCategory=User)(userAccountControl:1.2.84 0.113556.1.4.803:=2))"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter

    $colProplist = "name"
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i )}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)
    {$objItem = $objResult.Properties; $objItem.name}

  • #2
    Re: Powershell help

    Maybe you should try using a where statement.

    http://www.vistax64.com/powershell/1...xclude-ou.html
    Please give points where appropriate

    <I dont create ready scripts for you, but I'm willing to point you in the right direction>

    Comment


    • #3
      Re: Powershell help

      You can use the "Like" operator in an If statement.
      Create an Array containing the distinguished Name of the OUs you want to exclude. Start each distinguished Name with a wildcard *, then inclose each value in quotes and separate each value in the array with a comma.


      PS script: "Find disabled users"
      Code:
      $arrExcludedOus = "*,OU=Company Managers,DC=domain,DC=local", "*,OU=Company Engineers,DC=domain,DC=local", "*,OU=Test,DC=domain,DC=local"
      
      $strFilter = "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))"
      
      $objDomain = New-Object System.DirectoryServices.DirectoryEntry
      
      $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
      $objSearcher.SearchRoot = $objDomain
      $objSearcher.PageSize = 500
      $objSearcher.Filter = $strFilter
      
      $colProplist = "name"
      foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
      
      $colResults = $objSearcher.FindAll()
      
      foreach ($objResult in $colResults)
      {
         $objItem = $objResult.Properties
         $bCorrectOU = $true
         #check if object is not in the excluded OUs.
         foreach ($Ou in $arrExcludedOus)
         {
           if ($objItem.adspath -like $Ou)
           {$bCorrectOU = $false; break}
         }
      
         if ($bCorrectOU -eq $true) {$objItem.name}
      }
      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment

      Working...
      X