Author Posts

January 1, 2012 at 12:00 am

by sponsen at 2013-04-03 06:47:54

I recently created a bunch of ADGroups using powershell, and came over what i think should be a fetaure – and was wondering if i had forgotten something, before I post this as a PowerShell feature suggestion 🙂

So, lets say that yo have gotten a large CSV file that contains Name,SamAccountName,Mail
From this CSV, you shal make ADGroups. Allright, Name and SamAccountName is OK – they have parameters ByPropertyName, but the other attributes must be written via -OtherAttributes.

And here is the issue.. -OtherAttributes dont allow pipeline input.. Since this is such a wide-covering parameter, i find it strange that the AD team have not implemented pipelining here.. Or am I missing anything here?

What would you do to the following one-liner to input the mail values to each ADGroup?

Import-Csv C:\MassiveGroupDB.csv | New-ADGroup -OtherAttributes ???? -GroupCategory Distribution -GroupScope Universal -Path "OU=Distribution Groups,DC=domain,DC=com"

by ArtB0514 at 2013-04-03 07:17:29

One liners are severely overrated. The only way you can achieve what you want to do is with a foreach clause. The easiest way to create an OtherAttributes collection is by way of a hash table (also lookup splatting).

Note that this is just an example; it is not necessarily complete, and has definitely not been tested to verify that it works.
foreach ($Group in (Import-Csv C:\MassiveGroupDB.csv)) {
$Props = $Group | Get-Member -MemberType *Property | Select -ExpandProperty Name
$Props = $Props | Where {$_.Name -notmatch "^Name$|^SamAccountName$|^OtherAttributeToIgnore$"}
$Properties = @{}
$Props | Foreach {$Properties.Add($_,$Group.$_)}
New-ADGroup -OtherAttributes $Properties -GroupCategory Distribution -GroupScope Universal -Path "OU=Distribution Groups,DC=domain,DC=com"