Author Posts

November 13, 2014 at 1:23 pm

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:

[img]https://caesarsint-my.sharepoint.com/personal/ryoung_caesarsint_com/_layouts/15/guestaccess.aspx?guestaccesstoken=iw56v14bSqkikSbfDZ9tF8P9QRJ0R9WwpJqxEIo5mxo%3d&docid=086b8143312ef4cfcbbbbeaa0c24d3f94[/img]

[b]Goal #1[/b] = 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.

[b] Goal 2:[/b]

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!

November 13, 2014 at 6:30 pm

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.

November 14, 2014 at 7:31 am

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

November 14, 2014 at 8:32 am

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

November 16, 2014 at 5:05 pm

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