PowerShell – Group creation script

#-------------[deklarations]--------------------------
#source
$departments=Get-Content '\\hostname\scripts\departments.txt'
#logs
$successLog='\\hostname\logs\groups-done.LOG'
$errorLog='\\hostname\logs\groups-error.LOG'
$unreachableLog='\\hostname\logs\groups-unreachable.LOG'
$reportLog='\\hostname\logs\groups-report.txt'
#params
$header=''
$sharename='example'

$groupName
#-------------[functions]--------------------------
function CreateGroup {
     param (
        [string] $groupName)

        if(-not (Get-ADGroup -Filter "Name -eq '$($groupName)'" -ErrorAction SilentlyContinue)) {
            New-ADGroup -Name $groupName -GroupScope 'DomainLocal' -GroupCategory 'Security' -Description 'fileserver' -Path 'OU=EXAMPLE-groups,DC=example,DC=local'
            Write-host "Group: $groupName CREATED." -foreground 'green'
            $groupName >> $successLog
            return $groupName, "success"
        } else {
            Write-host "Group '$($groupName)' already exist" -foreground 'red'
            $groupName >> $errorLog
            return $groupName, "already exist"
        }        
}

foreach($department in $departments) {
    $groupName=("fs" + "-" + $shareName + "-" + $department + "-rw").ToLower()
    CreateGroup($groupName)
}
#-------------krolaki.eu--------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *