setting OU's as a filter string

This topic contains 5 replies, has 4 voices, and was last updated by  JasonL 1 month ago.

  • Author
    Posts
  • #89849

    JasonL
    Participant

    Hi all,

    I am trying to clean up some of my scripting attempts when interrogating the AD with powershell..
    i normally do the following

    Import-Module ActiveDirectory
    Get-ADuser -Searchbase 'OU=Users,OU=DirectoryX,OU=DirectoryY,DC=domainX,DC=Local' -Filter {enabled -eq $True}
    

    and this has worked reasonably well. However the actual pipline(ing) is now getting quite long..is there a way to set a variable eg:

    Import-Module ActiveDirectory
    $Search = ('OU=Users,OU=DirectoryX,OU=DirectoryY,DC=domainX,DC=Local' -Filter {enabled -eq $True})
    Get-ADuser -Searchbase $Search
    

    and following on from that:

    Select-Object Name,SamAccountName ...etc
    

    to be a variable too?
    eg

    $SO = (Select-Object Name,SamAccountName ...etc
    

    Hopefully, that makes sense as to what I'm after...have already been able to configure:

    $Date = ((Get-Date).ToString('dd-MM-yyyy'))
    

    to good effect, and wanted to expand knowledge and scripting!

    Thanks

    JasonL

  • #89855

    Sam Boutros
    Participant

    use splatting:

    $myOptions = @{
        Searchbase = 'OU=Users,OU=DirectoryX,OU=DirectoryY,DC=domainX,DC=Local'
        Filter = {enabled -eq $true}
    }
    Get-ADuser @myOptions
    
    • #89857

      JasonL
      Participant

      Thanks for your reply Sam, will give it a go!

      To expand on that, would this then also be true:

      
      $myOptions = @{
          Searchbase = 'OU=Users,OU=DirectoryX,OU=DirectoryY,DC=domainX,DC=Local'
          Filter = {enabled -eq $true}
      }
      $myobject =@{
      Select-Object Name,SamAccountName...
      }
      
      Get-ADUser $myOptions | $myobject | Out-File -file $file
      
      

      To Select the relevant data required?

      Thanks

      JasonL

  • #89866

    Alex Aymonier
    Participant

    Hey Jason,

    When splatting you are pre-defining the parameters and their values in a hash table which is made up of key=value pairs and this gets fed to the cmdlet using the @ symbol. You can't specify a cmdlet in that hash table. Notice each cmdlet has its own hash table being passed to it.

    $myOptions = @{
        Searchbase = 'OU=Users,OU=DirectoryX,OU=DirectoryY,DC=domainX,DC=Local'
        Filter = {enabled -eq $true}
    }
    $myobject =@{
        Property = 'Name','SamAccountName'
    }
    
    Get-ADUser @myOptions | Select-Object @myobject
    
  • #89870

    postanote
    Participant

    What you are asking , is really easy to rove out.
    Did you try this already?

    I mean you can try anything as long as it is not trying to change anything, and even if that, you could always use the -WhatIf parameter to prevent any changes yet still see all the results.

    Even with this...
    Searchbase = 'OU=Users,OU=DirectoryX,OU=DirectoryY,DC=domainX,DC=Local'

    ...If not used in side the splat, you can make more dynamic by using variables.
    For example, pick any user and ask for the Searchbase to get the base DN for say Domain Users.

    ($PrimaryGroup = (Get-ADUser -Filter * -Properties *)[0].PrimaryGroup)

    $myOptions = @{
    Searchbase = "$PrimaryGroup"
    Filter = {enabled -eq $true}
    }
    $myobject =@{
    Select-Object Name,SamAccountName
    }

    Get-ADUser $myOptions | $myobject | Out-File -file $file

    As for is you can really do anything, again just a mater of giving it a shot. I mean, it's a 50/50 proposition. IT either works or it doesn't.

    As for the your block about. If you have this open in the PoSH ISE of VSCode, you will see right away that you have an issue by all the red squiggly lines that show up and of course running it will generate the normal ugly red blob, well unless you changed your error colors.

    • #89905

      JasonL
      Participant

      Hi guys, have amended my script, and all works as expected, many thanks...now I nave a 'neater' script – Thanks Sam,Alex and postanote!

You must be logged in to reply to this topic.