Heres the script with a bit of modification to create the groups automaticly if they dont already exist.
Code:
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin
Add-pssnapin Quest.ActiveRoles.ADManagement
Function decideEmail([string]$homeEmail,[string]$workEmail)
{
#Check for email, home email wins
if($homeEmail -ne '')
{
return $homeEmail
}#end if
if($workEmail -ne '')
{
return $workEmail
}#end if
else{return $null}
}#end function decideEmail
function get-dn ([string]$SAMName,[string]$type)
{
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
switch($type)
{
contact{
$searcher.filter = "(&(objectClass=contact)(cn=$SAMName))"
}
group{
$searcher.filter = "(&(objectClass=group)(cn=$SAMName))"
}
user{
$searcher.filter = "(&(objectClass=user)(sAMAccountName=$SAMName))"
}
computer{
$searcher.filter = "(&(objectClass=computer)(name=$SAMName))"
}
}
$user = $searcher.findall()
if ($user.count -gt 1){
$count = 0
foreach($i in $user){
write-host $count ": " $i.path
$count = $count + 1
}
$selection = Read-Host "Please select item: "
return $user[$selection].path.toString()
}
else{
return $user[0].path
}
}
function CheckandCreateGroup([string]$GroupName)
{
$check = get-dn $groupName group
if($check -eq $null)
{
new-qadgroup -ParentContainer 'OU=Creation,DC=DOMAIN,DC=local' -name $GroupName -samAccountName $GroupName -grouptype 'Distribution' -groupscope 'Universal'
sleep 10
enable-DistributionGroup -Identity (get-dn $GroupName group).substring(7) -Alias ($groupname.replace(" ","")) -DisplayName $GroupName
}
}#end fuction creategroups
Function AssignSecurityGroups([string]$fullName,[string]$yearGroup,[string]$RegGroup)
{ .
CheckandCreateGroup("Parents")
$yearGroup = "Parents "+$yearGroup
CheckandCreateGroup($yearGroup)
$RegGroup = "Parents "+$RegGroup
CheckandCreateGroup($RegGroup)
$ParentsGroupDN = get-dn "Parents" group
$YeargroupDN = get-dn $yearGroup group
$regGroupDN = get-dn $regGroup group
$contactDN = get-dn $fullName contact
add-QADGroupMember -identity $ParentsGroupDN.substring(7) -member $contactDN.substring(7)
Add-QADGroupMember -identity $YearGroupDN.substring(7) -member $contactDN.substring(7)
Add-QADGroupMember -identity $regGroupDN.substring(7) -member $contactDN.substring(7)
}
Function runImport([string]$filePath)
{
#Import CSV and iterate though
Import-CSV $filePath | foreach-object{
#Create year and reg Group vars. Can only be done on first run
if($_.Name -ne '')
{
$Script:reg = $_.reg
$Script:year = $_.Year
}
#Check that the contact has Parental Responsibility
if($_.'Parental Responsibility' -eq 'T')
{
$email = decideEmail $_.'home email' $_.'work email';
#If contact has an e-amil address create contact card
if($email -ne $null)
{
#check if already existing contact
$check = get-dn $_.'full Name' contact
if($check -eq $null)
{
New-MailContact -Name $_.'Full Name' -ExternalEmailAddress $email -DisplayName $_.'Full Name' -FirstName $_.forename -LastName $_.surname -OrganizationalUnit 'ou=Creation,dc=DOMAIN,dc=local' -PrimarySmtpAddress $email
}
else
{
Set-MailContact -identity $_.'Full Name' -ExternalEmailAddress $email -DisplayName $_.'Full Name' -PrimarySmtpAddress $email
}
AssignSecurityGroups $_.'full Name' $Year $Reg;
if($check -eq $null){sleep 10}
Set-MailContact -Identity $_.'Full Name' -HiddenFromAddressListsEnabled:$True
}
} #end if
} #end for
} #end function runImport
#Run Import
runImport 's:\temp\contacts.csv';