0

I have been tasked with creating a bunch of security groups for Active Directory on a 2016 Windows Server. At the moment I have this code

$CSVLocation = Read-Host -Prompt "Please enter the path of CSV file"
$Groups = Import-CSV $CSVLocation

foreach ($Group in $Groups) {
    $Groupname = Get-ADGroup -Identity $Group.Group
    if ($Groupname -eq $null) {
        New-ADGroup -Name $Group.Group -Path $group.GroupLocation -GroupScope $Group.GroupType
    }
    else {
        echo "Group existes"
    }
}

This is code is trying to create a group if that group doesn't exist and if it does then skip the entry in the CSV. As of this moment, all it does it pump out Get-ADGroup errors about how it can't find the group and then skips the creation of it.

The CSV format is like such:

Group,GroupType,GroupLocation
Group01,Universal,"OU=Test,DC=Example,DC=Local"
Group02,Universal,"OU=Test,DC=Example,DC=Local"
Group03,Universal,"OU=Test,DC=Example,DC=Local"

Error Message:

Get-ADGroup : Cannot find an object with identity: 'AU-CTX-RDP' under: 'DC=Example,DC=local'.
At C:\Users\Administrator\Desktop\Scripts\Import Groups.ps1:10 char:14
+ $Groupname = Get-ADGroup -Identity $Group.Group
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (AU-CTX-RDP:ADGroup) [Get-ADGroup], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADGroup
6
  • How does you CSV file look like? You might post 2 or 3 example lines. (formatted as code please ;-) ) if ($Groupname -eq $null) does not work as you might think it does. The better way would be if (-not ($Groupname)) Commented Mar 27, 2018 at 23:01
  • I have added in some of the CSV formatting. Commented Mar 27, 2018 at 23:19
  • Hmmm ... ok, Goup01 ... Group03 have to be either a distinguished name, a GUID, a SID or a SamAccountName. If you get errors it might be helpful to post the error message as well. (the complete error message, formatted as code as well) Commented Mar 27, 2018 at 23:25
  • Done i should say im fairly new to AD and Powershell in general which i guess is fairly obvious at this point. Commented Mar 27, 2018 at 23:47
  • Great. Take a look at my answer and test it please. We all started once upon a time. ;-) Commented Mar 27, 2018 at 23:54

2 Answers 2

2

If you query for a not existing group you get a terminatig error. So the script execution would stop. To avoid this you can use -ErrorAction SilentlyContinue. This way it should work actually

$CSVLocation = Read-Host -Prompt "Please enter the path of CSV file"
$Groups = Import-CSV $CSVLocation

foreach ($Group in $Groups) {
    if (-not (Get-ADGroup  -Filter "Name -eq '$($group.Group)'" -ErrorAction SilentlyContinue)) {
        New-ADGroup -Name $Group.Group -Path $group.GroupLocation -GroupScope $Group.GroupType  
    }
    else {
        "Group '$($Group.Group)' already exists"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works well for the first group when it gets to second it errors out the same way.
Sorry ... I should have tested it before. Changed the code. Should work now.
0

Run this script to create bulk users in Powershell, Task will be completed within 30 seconds

New-ADGroup "-Name -GroupScope -GroupSecurity -GroupName" -Path “OU=OUWhereIStoreMyGroups" -Description

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.