locked
Help with Desired state configuration RRS feed

  • Question

  • I have a issue while creating multiple OUs and groups when using foreach in desired state configuration. In my config data I have part like this
    Data = @{
    ADOU = 'Windows Server 2012','Windows Server 2012 R2','Windows Server 2016','Windows Server 2019'
    }
    This is the part of the code in the script that I use to create those OUs
    foreach ($OU in $Data.ADOrganizationalUnit) {
    
    xADOrganizationalUnit OU {
    
    Name = ($_ -replace '-')
    Path = $DomainInfo.DomainDN
    ProtectedFromAccidentalDeletion = $true
    }
    }
    Does not matter what I do I get this
    xActiveDirectory\xADOrganizationalUnit : A duplicate resource identifier......Change the name of this resource so that it is unique with in the node..

    Thursday, January 24, 2019 2:09 PM

Answers

  • Hi,

    You cannot use the same name for each instance of xADOrganizationalUnit. OU part must be unique, so you could use variable or something else that will give you unique name each time, like $_.

    foreach ($OU in $Data.ADOrganizationalUnit) {
    
    xADOrganizationalUnit $_ {
    
    }
    -----------------------------------------------------------------------------------------------------------
    If you found this post helpful, please give it a "Helpful" vote. 
    Please remember to mark the replies as answers if they help.

    • Marked as answer by MiniMarks Thursday, January 24, 2019 3:56 PM
    Thursday, January 24, 2019 2:31 PM
  • You have to do it outside of the foreach loop. Create root OU's outside of the foreach and then add DependsOn on inside the foreach.

    -----------------------------------------------------------------------------------------------------------
    If you found this post helpful, please give it a "Helpful" vote. 
    Please remember to mark the replies as answers if they help.

    • Marked as answer by MiniMarks Thursday, January 24, 2019 3:56 PM
    Thursday, January 24, 2019 3:24 PM

All replies

  • Hi,

    You cannot use the same name for each instance of xADOrganizationalUnit. OU part must be unique, so you could use variable or something else that will give you unique name each time, like $_.

    foreach ($OU in $Data.ADOrganizationalUnit) {
    
    xADOrganizationalUnit $_ {
    
    }
    -----------------------------------------------------------------------------------------------------------
    If you found this post helpful, please give it a "Helpful" vote. 
    Please remember to mark the replies as answers if they help.

    • Marked as answer by MiniMarks Thursday, January 24, 2019 3:56 PM
    Thursday, January 24, 2019 2:31 PM
  • Hello NM

    many thanks for provided answer. You are right. The reason that I used OU is because we cannot Dependon on the variable. I want to create a structure of the OUs and sub-OUs and if I use variable then Dependon part will fail. Is there a workaround for this?

    Thursday, January 24, 2019 2:59 PM
  • You have to do it outside of the foreach loop. Create root OU's outside of the foreach and then add DependsOn on inside the foreach.

    -----------------------------------------------------------------------------------------------------------
    If you found this post helpful, please give it a "Helpful" vote. 
    Please remember to mark the replies as answers if they help.

    • Marked as answer by MiniMarks Thursday, January 24, 2019 3:56 PM
    Thursday, January 24, 2019 3:24 PM