PowerShell Script - Create Group if not exists

5.2k views Asked by At

Simple PowerShell script to create a computer group in AD. If the group exists, print already exists else create new. But something is wrong and its not creating a new group.

Import-Module ActiveDirectory
Import-Module Centrify.DirectControl.PowerShell

Clear-Variable -Name "Result"
Clear-Variable -Name "JSONOutput"
Clear-Variable -Name "ErrorMessage"
Clear-History

$username='<>';
$password='<>';
$computerGroup = 'Sample-New-Role';


# ************* SET CREDENTIALS *************************************
$Password = ConvertTo-SecureString $password -AsPlainText -Force
$global:Cred = New-Object System.Management.Automation.PSCredential($username,$Password)
Set-CdmCredential -Domain (Get-WmiObject Win32_ComputerSystem).Domain -Credential $Cred
$global:DomainController = Get-ADDomain -Current LocalComputer
Set-CdmPreferredServer -Domain (Get-WmiObject Win32_ComputerSystem).Domain -Server $global:DomainController.InfrastructureMaster
$global:OUPath = Get-ADOrganizationalUnit -Filter 'Name -like "Role Groups-Computer"'

# ************************ Create Zone ******************************
try{
    if(Get-ADGroup -filter {Name -eq $computerGroup} -ErrorAction Continue)
    {
            $Result = "Already_Exists"
    } else 
        {
            New-ADGroup -Name $computerGroup -GroupScope Global -GroupCategory Security -Path $global:OUPath -Credential $Cred
            $Result = 'Success'
        }
}
catch{
$ErrorMessage = $_.Exception
}

# ************************* Result *********************************
$JSONOutput = @{"result"=$Result;"error"=$ErrorMessage} | ConvertTo-Json -Compress
Write-Output $JSONOutput

Output: If Group already exist then just create new else print 'Already_Exists' It works fine if group already exists but failed and error out when new group. Instead of an error, it should create the group. Anything wrong with the condition?

{"error":{"Message":"Cannot find an object with identity: \u002709328-Sample-New-Role\u0027 ....

1

There are 1 answers

0
snowcoder On
Import-Module ActiveDirectory
Import-Module Centrify.DirectControl.PowerShell

Clear-Variable -Name "Result"
Clear-Variable -Name "JSONOutput"
Clear-Variable -Name "ErrorMessage"
Clear-History

$username='<>';
$password='<>';
$computerGroup = 'Sample-New-Role';


# ************* SET CREDENTIALS *************************************
$Password = ConvertTo-SecureString $password -AsPlainText -Force
$global:Cred = New-Object System.Management.Automation.PSCredential($username,$Password)
Set-CdmCredential -Domain (Get-WmiObject Win32_ComputerSystem).Domain -Credential $Cred
$global:DomainController = Get-ADDomain -Current LocalComputer
Set-CdmPreferredServer -Domain (Get-WmiObject Win32_ComputerSystem).Domain -Server $global:DomainController.InfrastructureMaster
$global:OUPath = Get-ADOrganizationalUnit -Filter 'Name -like "Role Groups-Computer"'

# ************************ Create Zone ******************************
try{
    if(Get-ADGroup -filter {Name -eq $computerGroup} -ErrorAction Continue)
    {
            $Result = "Already_Exists"
    } else 
        {
            New-ADGroup -Name $computerGroup -GroupScope Global -GroupCategory Security -Path $global:OUPath -Credential $Cred
            $Result = 'Success'
        }
}
catch{
$ErrorMessage = $_.Exception
}

# ************************* Result *********************************
$JSONOutput = @{"result"=$Result;"error"=$ErrorMessage} | ConvertTo-Json -Compress
Write-Output $JSONOutput