Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- param(
- [ValidateScript({Test-Path $_ -PathType Leaf})]
- [Parameter(Mandatory=$True,Position=1)][string]$CSV
- )
- 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 = ".\CreateResMailboxes.log"
- $nl = "`r`n"
- $ou = "OU=General Mailboxes,OU=Exchange,$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 "Importing resource mailboxes from $csv at $start"
- Write-Host $nl
- Import-Csv $csv | ForEach-Object {
- Set-Variable -Name testmb -Value $null -Scope 0
- $upn = $_.SamAccountName + "@" + $addomain
- $name = $_.DisplayName
- $alias = $_.Alias
- $email = $_.PrimarySmtpAddress
- $testmb = Get-Mailbox $alias -ErrorAction SilentlyContinue
- if($testmb -eq $null) {
- writelog "Creating mailbox $name ($email)"
- New-Mailbox -name $name -DisplayName $name -Shared -Alias $alias -UserPrincipalName $upn -PrimarySmtpAddress $email -OrganizationalUnit $ou
- }
- else {
- writelog "Mailbox $name ($email) 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
Advertisement