DSC Issue (Please help)

Good Evening,

I have a issue while creating multiple OUs and groups when using foreach. I am not sure if this is supported anymore.

I checked this site and I saw that this guy is creating multiple OUs and Groups with foreach

https://4sysops.com/archives/creating-an-active-directory-domain-with-powershell-dsc/

I have configuration data and in it I have like

DomainInfo = @{
        
        ADOU = 'Windows Server 2016','Users','Servers','Computers'
        ADGroup1  = 'Group1','Group2','Group3','Group4'
        ADGroup2  = 'Berlin Users','Berlin Servers','Berlin Groups','Berlin Users'
        ADGroup3  = 'Hamburg Users','Hamburg Servers','Hamburg  Users','Hamburg  Groups'
        }

When it comes to the code I tried both his way (This is his code)

@($ConfigurationData.NonNodeData.OrganizationalUnits).foreach( {
                xADOrganizationalUnit $_
                {
                    Ensure = 'Present'
                    Name = ($_ -replace '-')
                    Path = ('DC={0},DC={1}' -f ($ConfigurationData.NonNodeData.DomainName -split '\.')[0], ($ConfigurationData.NonNodeData.DomainName -split '\.')[1])
                    DependsOn = '[xADDomain]ADDomain'
                }
            })

and I tried like this as well

foreach ($OU in $DCData.ADOrganizationalUnit) {

     xADOrganizationalUnit ADOU {

         Name = ($_ -replace '-')
         Path = $DomainInfo.DomainDN
         ProtectedFromAccidentalDeletion = $true
         DependsOn = ''
         }
        }

The result is the same. I get conflict error.
xActiveDirectory\xADOrganizationalUnit : A duplicate resource identifier…Change the name of this resource so that it is unique with
in the node specification.esources have identical key properties but there are differences in the following non-key properties: ‘DependsOn’. Values ‘NULL’ do
n’t match values ‘’. Please update these property values so that they are identical in both cases. I have no duplicated values, I have only 1 xADOrganizationalUnit

Can someone please help

So, let’s take YOUR code. The problem is that you’ve got this:

xADOrganizationalUnit ADOU

The “ADOU” part needs to be unique for each instance of xADOrganizationalUnit. So you need to put a variable or something in there to provide a unique name each time. Right now, your ForEach is causing multiple instances of ADOU to be produced.

Thank you so much for your answer Mr. Don Jones,

I will change the code and try it again. I will reply back the result.

P.S.

Is the process the same for groups as well?

Yes as each setting needs its own name in DSC

Hi Guys,

It is working. Thank you so much. I have one more question. Is it possible to create whole folder structure on this way. Problem is when I need to choose DependsOn. You cannot depend on $_ How to avoid this if I need to create Root OU first and then sub OU’s depend on the first one. It is not working when I specify $_

You can’t do it inside a foreach loop, do the root OU outside of Foreach and then add Depends on inside the foreach.

Thank you so much guys, working perfectly.