Here get-adgroups returns false when querying from script while it returns true when run manually using the very same PowerShell ISE Window. Please see the following code which produces the error. Group, OU and DN exist. Quite likely no Typo. Reproducible by manually running the command (see further below), which works fine.
Import-Module ActiveDirectory
$Group="ProductInternalInstallProductOnNextLogin"
$BaseDN="OU=Product,DC=int,DC=Domain,DC=de"
write-host "get-adgroup -Filter DistinguishedName -eq CN=$Group,$BaseDN"
$Result=get-adgroup -Filter {(DistinguishedName -eq "CN=$Group,$BaseDN")}
if($Result)
{
write-host "Group $Group found"
}
else
{
write-host "Group $Group not found, trying to create $Group"
New-ADGroup -path "$BaseDN" -GroupScope Global -name $Group
if (!$?)
{
write-host "ERROR creating new group $Group"
exit
}
}
This results in the following output where you can see the error:
____________________________________________________________________________________________________________________________________________________________________________________________________________________
PS C:\Users\MyName.INT> G:\DevPath\Tools\PowerShell-Scripte\Unbenannt2.ps1
get-adgroup -Filter DistinguishedName -eq CN=ProductInternalInstallProductOnNextLogin,OU=Product,DC=int,DC=Domain,DC=de
Group ProductInternalInstallProductOnNextLogin not found, trying to create ProductInternalInstallProductOnNextLogin
New-ADGroup : Die angegebene Gruppe ist bereits vorhanden
Bei G:\DevPath\Tools\PowerShell-Scripte\Unbenannt2.ps1:13 Zeichen:16
+ New-ADGroup <<<< -path "$BaseDN" -GroupScope Global -name $Group
+ CategoryInfo : NotSpecified: (CN=ProductInte...nt,DC=Domain,DC=de:String) [New-ADGroup], ADException
+ FullyQualifiedErrorId : Die angegebene Gruppe ist bereits vorhanden,Microsoft.ActiveDirectory.Management.Commands.NewADGroup
ERROR creating new group ProductInternalInstallProductOnNextLogin
____________________________________________________________________________________________________________________________________________________________________________________________________________________
How can New-ADGroup fail if I'm only running it in case the group is not there? PowerShell is running in German here, so the error message "New-ADGroup : Die angegebene Gruppe ist bereits vorhanden" means "This group already exists".
To verify this, I ran this manually in the console, where it works out fine:
PS C:\Users\MyName.INT> write-host "the following command was run manually from the commandline of the PowerShellISE"
$Result=get-adgroup -Filter {(DistinguishedName -eq "CN=ProductInternalInstallProductOnNextLogin,OU=Product,DC=int,DC=Domain,DC=de")}
write-host $Result
which produces the correct output:
the following command was run manually from the commandline of the PowerShellISE
CN=ProductInternalInstallProductOnNextLogin,OU=Product,DC=int,DC=Domain,DC=de
In my struggling I tried also
try {get-adgroups [...]} catch {new-adgroup[...]}
but that didn't work out either.