Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- param(
- [Parameter(Mandatory=$True,Position=0)][string]$RootOU
- )
- Import-Module ActiveDirectory
- if(!(Get-PSSnapin |
- Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
- ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
- }
- $start = Get-Date
- $domain = Get-ADDomain
- $addomain = $domain.Forest
- $domaindn = $domain.DistinguishedName
- $logfile = ".\CreateMailboxes.log"
- $nl = "`r`n"
- $ou = "OU=Users,OU=$RootOU,$domaindn"
- function writelog([string]$message) {
- $timestamp = Get-Date
- $fc = "White"
- if($message.contains("ERROR")) {$fc ="Red"}
- Add-content $logfile -value "$timestamp : $message"
- Write-Host $message -ForegroundColor $fc
- }
- writelog "Creating mailboxes in $ou at $start"
- Write-Host $nl
- Get-ADUser -SearchBase $ou -LDAPfilter "(objectClass=user)" -properties DisplayName,SamAccountName,EmailAddress | ForEach-Object {
- Set-Variable -Name testmb -Value $null -Scope 0
- $username = $_.SamAccountName
- $upn = $username + "@" + $addomain
- $DisplayName = $_.DisplayName.Trim()
- $DisplayName = $DisplayName.Replace("Contoso","Microsoft")
- $testmb = Get-Mailbox $username -ErrorAction SilentlyContinue
- if($testmb -eq $null) {
- writelog "Creating mailbox $DisplayName"
- Enable-Mailbox $username -DisplayName $DisplayName -Alias $username
- }
- else {
- #writelog "Mailbox $name already exists, skipping."
- }
- }
- $end = Get-Date
- $elapsed = $end - $start
- Write-Host $nl
- writelog "Finished import at $end ($elapsed)"
Advertisement
Add Comment
Please, Sign In to add comment