0
votes

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
2
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)) - Olaf
I have added in some of the CSV formatting. - christian
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) - Olaf
Done i should say im fairly new to AD and Powershell in general which i guess is fairly obvious at this point. - christian
Great. Take a look at my answer and test it please. We all started once upon a time. ;-) - Olaf

2 Answers

2
votes

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"
    }
}
0
votes

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