Last active
September 6, 2016 18:59
-
-
Save thomasrayner/d81fa3a3f1de2074330696e9963f8aaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#don't store names and members in variables like this | |
#don't name variables like this | |
#handle hashtable "already contains key" errors better than this | |
#don't do this | |
$list1 = 'list1 name' | |
$list1mem = @('list 1 members') | |
$list2 = 'list2 name' | |
$list2mem = @('list 2 members') | |
$vars = get-variable | ? { $_.name -match 'list' } | |
$varpairs = @{} | |
foreach ($var in $vars) | |
{ | |
$dig = [regex]::matches($var.Name,'\d').value | |
$unique = $vars | ? { $_.Name -match $dig } | |
$oldeap = $erroractionpreference; $erroractionpreference = 'silentlycontinue' | |
$varpairs.add($unique[0].Value,$unique[1].Value) | |
$erroractionpreference = $oldeap | |
} | |
$varpairs | ft -AutoSize | |
foreach ($addme in $varpairs.GetEnumerator()) | |
{ | |
$alias = $addme.Key -replace -join'[^a-zA-Z]','' | |
$groupparams = @{ | |
'Name' = $addme.Key; | |
'Members' = $addme.Value; | |
'Alias' = $alias; | |
'PrimarySMTPAddress' = "$($alias)@domain.tld"; | |
'SamAccountName' = $alias; | |
'OrganizationalUnit' = 'domain.tld/OU'; | |
'DisplayName' = $addme.Key; | |
'CopyOwnerToMember' = $false; | |
'Type' = 'Distribution' | |
} | |
New-DistributionGroup @groupparams | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment