Setting variable based on AD OU

Welcome Forums General PowerShell Q&A Setting variable based on AD OU

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
4 years ago.

  • Author
    Posts
  • #20582

    Participant
    Points: 0
    Rank: Member

    Hello folks, I'm on the cusp of 'getting it', and have hit a small roadblock with variables. Hoping to get some guidance please :). Here's my AD structure:

    Goal #1 = produce a variable for each environment, be it DEV, QA, Stage or Prod

    #determine the servers environment
    Function GetEnvironment{
        Import-Module ActiveDirectory
        Get-ADComputer -SearchBase "OU=DSCdev,DC=mtl,DC=contoso,DC=com" -Filter * |
        select -ExpandProperty DistinguishedName
    }
    
    $production = GetEnvironment | where {$_.DistinguishedName -ccontains ",OU=Prod"}
    

    Problem: I don't think i'm using "where" correctly / running the last line is above is accepted, but when I look for result I get nothing.

    Goal 2:

    I want to add the outcome as an additional row/string to the below hashtable:

    #Pull list of WebServers and GUIDs into hash table
    $WebServerConfigData = @{
        AllNodes = @(
            foreach ($node in $WebServers) {
                @{NodeName = $node.Name; NodeGUID = $node.objectGUID; NodeRole = 'WebServer'}   #### insert environment on this line based on env roles
            }
        )
    }
    

    This is all to be used in DSC, I'm deliberately keeping the searchbase higher because I'd like to keep the scalability.

    Thanks!

  • #20586

    Participant
    Points: 2
    Rank: Member

    The Problem with your Where-Object filterscript is twofold:

    1. When you ran Get-ADComputer you piped that to Select -expandproperty Distinguishedname. The output of this will be a string with no distinguishedname property to compare against. Just get rid of the Select portion of the command and you'll be fine. Alternatively you can drop the property from the $_ in the filterscript to handle this:

    where {$_ -ccontains ",OU=Prod"}

    2. You need to use -like instead of -ccontains. -Contains and -CContains are used for finding objects in a collection. You are just comparing strings so use like:

    $production = GetEnvironment | where {$_.DistinguishedName -like "*,OU=Prod*"}

    In the first part are you just trying to get a list of all the computers in each child OU? How about something like this:

    $OUs = Get-ADOrganizationalUnit -SearchBase 'OU=Elementary,OU=Student,OU=Computers,DC=lakota,DC=spi' -Filter * -SearchScope OneLevel | foreach {
        [PsCustomObject]@{
            Name = $_.Name
            Computers = (Get-ADComputer -SearchBase $_ -Filter *)
        }
    }

    This will give you an array of OUs by name each with all the computer objects that you can iterate through later.

    For the second goal, do you have a particular problem? It's unclear what exactly you are going for here.

  • #20598

    Participant
    Points: 0
    Rank: Member

    So if I take what you put and do this:

    1) execute your array on an OU of mine, which works nicely by the way
    2) $OUs | Where-Object {$_.Computers -like "007"}

    Powershell takes the command, but doesn't output any information (even if I verbose it). I know I have a computer with 007 in the name.

    Confused I am

  • #20599

    Participant
    Points: 0
    Rank: Member

    found the problem, didn't notice your *'s in "like". really needs those wildcards eh?

  • #20617

    Participant
    Points: 2
    Rank: Member

    That's right, you need a wildcard in a like comparison.

The topic ‘Setting variable based on AD OU’ is closed to new replies.