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.
The Problem with your Where-Object filterscript is twofold:
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"}
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: