You really don't want to do it that way.
Offered without explanation. No error logic. Not pseudo-code. But not tested.
## csv file header: Name,Members
$csvRecords = Import-Csv "file.csv"
$dl = @{}
Foreach( $csvRecord in $csvRecords )
{
If( -not $dl.Contains( $csvRecord.Name ) )
{
$dl.$( $csvRecord.Name ) = 1
}
}
Foreach( $key in $dl.Keys )
{
New-DistributionGroup -Name $key -Type
Distribution
)
Foreach( $csvRecord in $csvRecords )
{
$members = $csvRecord.Members.Split( ',' )
Add-AdGroupMember -Identity $csvRecord.Name
-Members $members
$members = $null
}
$dl = $null
$csvRecords = $null
From: [email protected] [mailto:[email protected]] On
Behalf Of Heaton, Joseph@Wildlife
Sent: Wednesday, June 4, 2014 5:28 PM
To: '[email protected]'
Subject: [NTSysADM] Powershell question - DL creation
I need to create 50 DLs, and add multiple members to each DL. I have one CSV
file to do this, at the moment. I have 2 columns, first is the DL name, and
the 2nd is a list of e-mail addresses that need to be in that DL, separated by
commas in the cell.
I know I can use this command to create the DLs:
Import-Csv C:\DLs.csv | ForEach { New-DistributionGroup -Name $_.Name -Alias
$_.Alias -Type "Distribution"}
And I know I can use this command to add members to a DL:
Import-CSV FileName.csv | ForEach {Add-DistributionGroupMember -Identity
"GROUP-NAME" -Member $_.Name}
However, I don't know if it is possible to use the bottom command in
conjunction with the first, to create the group and add the members. I also
don't know if I can use the single cell with multiple e-mail addresses in it,
separated by commas. (i.e. [email protected]<mailto:[email protected]>,
[email protected]<mailto:[email protected]>,
[email protected]<mailto:[email protected]>)
I am going to continue to dig on the internet, but I was hoping someone here
might have an idea, as well.
Thanks,
Joe Heaton
Enterprise Server Support
Information Technology Operations Branch
Data and Technology Division
CA Department of Fish and Wildlife
1807 13th Street, Suite 201
Sacramento, CA 95811
Desk: (916) 323-1284