Categories

Bulk create groups–provider

The provider version of our bulk creation script for groups becomes

function new-securitygroup {            
[CmdletBinding()]            
param (            
 [parameter(ValueFromPipeline=$true,             
   ValueFromPipelineByPropertyName=$true)]            
 [string]$name,            
             
  [parameter(ValueFromPipeline=$true,             
   ValueFromPipelineByPropertyName=$true)]            
 [string]$description,            
             
  [parameter(ValueFromPipeline=$true,             
   ValueFromPipelineByPropertyName=$true)]            
 [string]$ou,            
             
 [parameter(ParameterSetName="U")]            
 [switch]$universal,            
             
 [parameter(ParameterSetName="G")]            
 [switch]$global,            
             
 [parameter(ParameterSetName="DL")]            
 [switch]$domainlocal            
)            
BEGIN {            
# set constants for group types            
$globalgroup = 0x00000002            
$domainlocalgroup = 0x00000004            
$security = 0x80000000            
$universalgroup = 0x00000008            
}            
PROCESS {            
$target = [ADSI]"LDAP://$ou"            
            
switch ($psCmdlet.ParameterSetName) {            
 "DL" {$grouptype = $security -bor $domainlocalgroup}            
 "G"  {$grouptype = $security -bor $globalgroup }            
 "U"  {$grouptype = $security -bor $universalgroup }            
 default {Write-Host "Error!!! Should not be here" }            
}            
            
New-Item -Path AD:\$ou -Name "cn=$name" -ItemType group `
-Value @{samAccountName=$name; GroupType = $grouptype; Description = $description}             
            
} # end process block            
}


We only need to set the group type once



Still works as



import-csv .\testgroups.csv | new-securitygroup -universal

Leave a Reply