DSC Issue (Please help)

Welcome Forums DSC (Desired State Configuration) DSC Issue (Please help)

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

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #117052

    Participant
    Points: 32
    Rank: Member

    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

  • #117111

    Keymaster
    Points: 1,673
    Helping HandTeam Member
    Rank: Community Hero

    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.

  • #117159

    Participant
    Points: 32
    Rank: Member

    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?

  • #117589

    Participant
    Points: 70
    Rank: Member

    Yes as each setting needs its own name in DSC

  • #118261

    Participant
    Points: 32
    Rank: Member

    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 []$_

  • #118273

    Participant
    Points: 809
    Helping Hand
    Rank: Major Contributor

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

  • #118834

    Participant
    Points: 32
    Rank: Member

    Thank you so much guys, working perfectly.

You must be logged in to reply to this topic.