Posh Script - AD Users - exclude OUs?

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Tim Pringle Tim Pringle 1 year, 10 months ago.

  • Author
    Posts
  • #22125
    Profile photo of Jake Sully
    Jake Sully
    Participant

    Hey Folks,

    I am working on a PowerShell script and could use some help. I honestly haven't made much progress. I'm first trying to figure out how to query AD and I need to query all of it, with the exception of a few OUs and I have to exclude accounts that are something like the "System" accounts, there's a list I have to exclude.

    I then need to retrieve several user attribs, and email out a CSV report. I need to do this and make the last column contain the users "Enabled/Disabled" status.

    Can someone kindly help me please?

    Thank you!

  • #22168
    Profile photo of Jack Neff
    Jack Neff
    Participant

    Jake bro you have to give it a little effort first. This can easily be found with 5 minutes on Google *cough*...I mean Bing. Please don't take advantage of folks kindness by letting them do your work.

  • #22169
    Profile photo of Jake Sully
    Jake Sully
    Participant

    Jack,

    I have been putting in effort, I did find things by searching online using all the search engines, but I'm not sure how to tie them together. I know how to search the domain for users, and gather user attributes, but I don't know how to exclude OUs along with accounts that start with specific "strings".

    I then am not sure how to email out what I get back. I'll keep working on it.

    I am trying to learn, not just have someone write this for me.

  • #22192
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey Jake,

    I don't have my server available to validate the exact syntax, so consider this pseudo code for some pointers.

    #Get all accounts apart from excluded ones
    $excludedAccounts = @('my','list','of','excluded','accounts')
    $nonExcludedAccounts = Get-ADUser -Filter * -Property * | Where SamAccountName -notin $excludedAccounts

    #Apart from OUs that begin with.........
    In order to check the OU where the user is located, I think you're going to have to access the DistinguishedName or CanonicalName attributes, and then parse them to identify the OU (the CN is the first item in it, not the OU). The AD cmdlets are not as pipeline friendly as some of the other cmdlets, so you can't combine Get-ADUser with Get-ADOrganizationalUnit. Alternatively, you could reverse the order wherby you use Get-OrganizationalUnit first, apply a filter, and then from those results, use Get-ADUser with a SearchBase attribute of the DN from the each item in the collection returned by Get-ADOrganizationalUnit.

    #Get some properties
    $userAttributes = $finalUserList | Select Property1,Property2

    #Export to CSV
    Use Export-CSV with $userAttributes

    #Mail
    Use Send-MailMessage with the file details.

You must be logged in to reply to this topic.