Creating AD Group - How to pass this through?

Welcome Forums General PowerShell Q&A Creating AD Group - How to pass this through?

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #167974

    Participant
    Topics: 1
    Replies: 0
    Points: -8
    Rank: Member

    Hi everyone,

    So I'm a little bit new to PowerShell and I'm trying to create a loop to create Distribution Groups within my AD while first creating the set inputs.

    Here is my code so far:

    Import-Module ActiveDirectory
    Set-Location AD:

    $prefix = "Prefix-"

    $name = "Test-Name-1, Test-Name-2, Test-Name-3, Test-Name-4"

    #Append prefix to name

    $distGroupName = $name.Split(',') | ForEach-Object {

    "$prefix$_"

    }

    Foreach ($name in $distGroupName) {New-ADGroup -Name $_.name -DisplayName $_.name) -GroupCategory Distribution -GroupScope Global -Path "OU=Distribution,OU=EXAMPLE,DC=GLOBAL,DC=COM"}

     

    When I'm trying this I'm getting: Cannot validate argument on parameter 'Name'. the argument is null or empty. Provide an argument that is not null or empty, and then try the command again. 

    I'm trying to pass off the joined $prefix and $name in the array $distGroupName to New-ADGroup. How would I go about doing this?

     

    Thank you,

    David Thao

  • #167998

    Participant
    Topics: 6
    Replies: 12
    Points: 50
    Rank: Member

    Hi David!

     

     

     

    #I suggest checking the results of your variables!
    
    
    
    $distGroupName = $name.Split(',') | ForEach-Object {
    
    "$prefix$_"
    
    }
    
    
    
    write-verbose "$distGroupName" -verbose
    
    
    Foreach ($name in $distGroupName) {
    write-verbose "$name" -verbose
    pause
    New-ADGroup -Name $_.name -DisplayName $_.name) -GroupCategory Distribution -GroupScope Global -Path "OU=Distribution,OU=EXAMPLE,DC=GLOBAL,DC=COM"}

You must be logged in to reply to this topic.