Friday, June 11, 2010

PowerShell: Exchange 2007: Create Mail Enabled Contacts from a Spreadsheet

Script is below. This script can be modified to work with almost anything. What it demonstrates is reading from a spreadsheet and enacting commands against the data.

Script follows: -


#Fields contained in spreadsheet:
#FirstName LastName FullName JobTitle Division BU Department StreetAddress Suburb State
#PostCode Country Telephone Mobile Fax UserID Password Email PerkinsEmail
#HomMailServer AccountType ErrorCount ErrorMessages


#
#
$LIST=IMPORT-CSV C:\BULKUPDATE.CSV
#
# Go through EACH item in the list (Header row is treated as variable names by default)
#
FOREACH ($USER in $LIST) {
#
#
$Firstname=$USER.Firstname
$Lastname=$USER.Lastname + " (Perkins)"
$Fullname=$USER.FullName + " (Perkins)"
$BU=$USER.BU
$USERID=$USER.USERID
$ForwardingAddress=$USER.PerkinsEmail


write-Host
write-Host "Full name: " $FullName
write-Host "Firstname: " $Firstname
write-Host "UserID: " $UserID
write-Host "Forwarding address: " $ForwardingAddress
write-Host

Write-Host "Press the c key to continue to create a contact with the above variables"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
if ($x.Character -ne "c")
{
write-Host $x.Character
exit
}
#
#
# Enter your commands here that will work with the variables above.
New-MailContact -Name $Fullname -DisplayName $FullName -FirstName $Firstname -LastName $Lastname -Alias $UserID -ExternalEmailAddress $ForwardingAddress -OrganizationalUnit "User-Contacts-Imported" | Set-MailContact -HiddenFromAddressListsEnabled $true

#
#
}



Other commands that have proven useful with this routine are: -
  • GET-USER $MailboxUserName | SET-MAILBOX -ForwardingAddress $ForwardingAddress

Here is another sample script which creates resource mailboxes and turns off the autoaccept agent. This is good for resource mailboxes where the users want to directly update the calendar. Note: It is not best practice to update calendars directly however this is what some people like.


#
#
$LIST=IMPORT-CSV C:\TAGPFIMPORT2.CSV
#
# Go through EACH item in the list (Header row is treated as variable names by default)
#
FOREACH ($USER in $LIST) {

$MailboxName=$USER.MailboxName
$Alias=$USER.Alias
$UPN=$Alias + "@tollgroup.local"
$ResourceType=$USER.ResourceType
$Database=$USER.Database
$OUName=$USER.OU
$Database=$USER.Database
$ResourceAdmin1=$USER.ResourceAdmin1
$ResourceAdmin2=$USER.ResourceAdmin2
$ResourceAdmin3=$USER.ResourceAdmin3
$AutomateProcessing=$USER.AutomateProcessing
$DeleteNonCalendarItems=[System.Convert]::ToBoolean($USER.DeleteNonCalendarItems)

write-Host
write-Host "Mailbox name: " $MailboxName
write-Host "UPN: " $UPN
write-Host "Resource Type: " $ResourceType
write-Host "ResourceAdmin1: " $ResourceAdmin1
write-Host "ResourceAdmin2: " $ResourceAdmin2
write-Host "ResourceAdmin3: " $ResourceAdmin3
write-Host "AutomateProcessing: " $AutomateProcessing
write-Host "DeleteNonCalendarItems: " $DeleteNonCalendarItems
write-Host "Database: " $Database
write-Host "OU: " $OUName
write-Host

Write-Host "Press the c key to continue to create a resource mailbox with the above variables"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
if ($x.Character -ne "c")
{
write-Host $x.Character
exit
}
#
#
# Enter your commands here that will work with the variables above.
write-Host New-Mailbox -UserPrincipalName $UPN -database $Database -Name $MailboxName -OrganizationalUnit $OUName -DisplayName $MailboxName -ResetPasswordOnNextLogon $TRUE Set-Mailbox -Type $ResourceType
New-Mailbox -UserPrincipalName $UPN -database $Database -Name $MailboxName -OrganizationalUnit $OUName -DisplayName $MailboxName -ResetPasswordOnNextLogon $TRUE Set-Mailbox -Type $ResourceType

Get-Mailbox $MailboxName Add-MailboxPermission -AccessRights FullAccess -User $ResourceAdmin1
Get-Mailbox $MailboxName Add-MailboxPermission -AccessRights FullAccess -User $ResourceAdmin2
Get-Mailbox $MailboxName Add-MailboxPermission -AccessRights FullAccess -User $ResourceAdmin3
write-host SET-MAILBOXCALENDARSETTINGS $MailboxName -AutomateProcessing $AutomateProcessing -DeleteNonCalendarItems $DeleteNonCalendarItems

SET-MAILBOXCALENDARSETTINGS $MailboxName -AutomateProcessing $AutomateProcessing -DeleteNonCalendarItems $DeleteNonCalendarItems


}

No comments:

Post a Comment