Create-Mailboxes.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<#PSScriptInfo
 
.VERSION 1.5
 
.GUID 2905a44a-0932-41e9-9d09-b6339a9f0143
 
.AUTHOR Mike Galvin twitter.com/digressive
 
.COMPANYNAME
 
.COPYRIGHT (C) Mike Galvin. All rights reserved.
 
.TAGS Exchange Mailbox Active Directory Syncronization
 
.LICENSEURI
 
.PROJECTURI https://gal.vin/2017/06/07/powershell-create-mailboxes
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES Exchnage Management PowerShell module. Active Directory Management PowerShell module.
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
    .SYNOPSIS
    Create Exchange mailboxes for users with no mailbox contained within an OU tree.
 
    .DESCRIPTION
    Create Exchange mailboxes for users with no mailbox contained within an OU tree.
 
    This script will:
     
    Create mailboxes for users contained witin an OU tree.
    You can configure the database and retention policy to use.
    Output and e-mail a log file.
 
    This script is designed to be run locally on an Exchange Server.
     
    .PARAMETER Ou
    The AD Organisational Unit (including child OUs) that contains the users to create Exchange Mailboxes for.
 
    .PARAMETER Datab
    The Exchange database to create the mailboxes in.
 
    .PARAMETER Rp
    The retention policy that should be applied to the users.
     
    .PARAMETER Compat
    Use this switch if you are using Exchange 2010.
 
    .PARAMETER L
    The path to output the log file to.
    The file name will be "Create-Mailboxes.log"
     
    .PARAMETER SendTo
    The e-mail address the log should be sent to.
 
    .PARAMETER From
    The from address the log should be sent from.
 
    .PARAMETER Smtp
    The DNS name or IP address of the SMTP server.
 
    .PARAMETER User
    The user account to connect to the SMTP server.
 
    .PARAMETER Pwd
    The password for the user account.
 
    .PARAMETER UseSsl
    Connect to the SMTP server using SSL.
 
    .EXAMPLE
    Create-Mailboxes.ps1 -Ou "OU=NewUsers,OU=Dept,DC=contoso,DC=com" -Datab "Mail DB 2" -Rp "1-Month-Deleted-Items" -L E:\scripts -Sendto me@contoso.com -From Exch01@contoso.com -Smtp smtp.live.com -User Exch01@contoso.com -Pwd P@ssw0rd -UseSsl
 
    This will create mailboxes for users that do not already have one in the OU NewUsers and all child OUs.
    It will create the mailbox using Mail DB 2 and apply the retention policy "1-Month-Deleted-Items".
    A log will be output to E:\scripts and e-mail using a secure connection.
#>


[CmdletBinding()]
Param(
    [parameter(Mandatory=$True)]
    [alias("Ou")]
    $OrganisationalUnit,
    [parameter(Mandatory=$True)]
    [alias("Datab")]
    $Database,
    [alias("Rp")]
    $Retention,
    [alias("L")]
    $LogPath,
    [alias("SendTo")]
    $MailTo,
    [alias("From")]
    $MailFrom,
    [alias("Smtp")]
    $SmtpServer,
    [alias("User")]
    $SmtpUser,
    [alias("Pwd")]
    $SmtpPwd,
    [switch]$UseSsl,
    [switch]$Compat)

## Count number of users in ou specified without a mailbox
$UsersNo = Get-ADUser -SearchBase $OrganisationalUnit -Filter * -Properties mail | Where-Object {$_.mail -eq $null} | Measure-Object

## If users exist without mailboxes run the script
If ($UsersNo.count -ne 0)
{

    ## If logging is configured, start log
    If ($LogPath)
    {
        $LogFile = "Create-Mailboxes.log"
        $Log = "$LogPath\$LogFile"

        ## If the log file already exists, clear it
        $LogT = Test-Path -Path $Log
        If ($LogT)
        {
            Clear-Content -Path $Log
        }

        Add-Content -Path $Log -Value "****************************************"
        Add-Content -Path $Log -Value "$(Get-Date -Format G) Log started"
        Add-Content -Path $Log -Value ""
    }

    ## If compat is configured load the old Exchange PS Module, if not load the current one.
    If ($Compat)
    {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

        ## Logging
        If ($LogPath)
        {
            Add-Content -Path $Log -Value "$(Get-Date -Format G) Loaded PSSnapin for Exchange 2010"
        }
    }

    Else
    {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

        ## Logging
        If ($LogPath)
        {
            Add-Content -Path $Log -Value "$(Get-Date -Format G) Loaded PSSnapin for Exchange 2013/16"
        }
    }

    ## Find the users in the OU specified
    $Users = Get-ADUser -SearchBase $OrganisationalUnit -Filter *

    ## For each user that does not have a mailbox, create one and set the retention policy if it has been specified
    ForEach ($User in $Users)
    {
        If ($(Get-ADUser $User -Properties mail).mail -eq $null)
        {
            Enable-Mailbox -Identity $User.SamAccountName -Database $Database -RetentionPolicy $Retention

            ## Logging
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Creating mailbox for User: $($User.SamAccountName) in Database: $Database with Retention Policy: $Retention"
            }
        }
    }

    ## If log was configured stop the log
    If ($LogPath)
    {
        Add-Content -Path $Log -Value ""
        Add-Content -Path $Log -Value "$(Get-Date -Format G) Log finished"
        Add-Content -Path $Log -Value "****************************************"

        ## If email was configured, set the variables for the email subject and body
        If ($SmtpServer)
        {
            $MailSubject = "Create Mailboxes Log"
            $MailBody = Get-Content -Path $Log | Out-String

            ## If an email password was configured, create a variable with the username and password
            If ($SmtpPwd)
            {
                $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($SmtpPwd | ConvertTo-SecureString -AsPlainText -Force)

                ## If ssl was configured, send the email with ssl
                If ($UseSsl)
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds
                }

                ## If ssl wasn't configured, send the email without ssl
                Else
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -Credential $SmtpCreds
                }
            }

            ## If an email username and password were not configured, send the email without authentication
            Else
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer
            }
        }
    }
}

## End