start-collectOnPremFullMailboxAccess.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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
function start-collectOnPremFullMailboxAccess { <# .SYNOPSIS This function exports all of the mailbox folders from the on premises environment with custome permissions. .DESCRIPTION Trigger function.. .PARAMETER EXCHANGESERVER *OPTIONAL* *REQUIRED with enableHybridMailFlow:TRUE* This parameter specifies that local Exchange on premises installation utilized for hybrid mail flow enablement. Exchange server is no required for migrations unlss enable hyrbid mail flow is required. .PARAMETER EXCHANGECREDENTIAL *OPTIONAL* *REQUIRED with ExchangeServer specified* This is the credential utilized to connect to the Exchange server remote powershell instance. Exchange Organization Adminitrator rights are recommended. .PARAMETER EXCHANGEAUTHENTICATIONMETHOD *OPTIONAL* *DEFAULT: BASIC* This specifies the authentication method for the Exchage on-premsies remote powershell session. .PARAMETER LOGFOLDERPATH *REQUIRED* This is the logging directory for storing the migration log and all backup XML files. If running multiple SINGLE instance migrations use different logging directories.. .OUTPUTS Logs all activities and backs up all original data to the log folder directory. Moves the distribution group from on premieses source of authority to office 365 source of authority. .EXAMPLE Start-collectOnPremFolderPermissions -exchangeServer Server -exchangeCredential $credential #> #Portions of the audit code adapted from Tony Redmon's project. #https://github.com/12Knocksinna/Office365itpros/blob/master/ReportPermissionsFolderLevel.PS1 #Don't tell him - he can get grumpy at times. [cmdletbinding()] Param ( [Parameter(Mandatory = $true)] [string]$logFolderPath, [Parameter(Mandatory = $false)] [string]$exchangeServer=$NULL, [Parameter(Mandatory = $false)] [pscredential]$exchangeCredential=$NULL, [Parameter(Mandatory = $false)] [ValidateSet("Basic","Kerberos")] [string]$exchangeAuthenticationMethod="Basic", [Parameter(Mandatory = $false)] [boolean]$retryCollection=$FALSE, [Parameter(Mandatory = $false)] $bringMyOwnMailboxes=$NULL ) $windowTitle = "Start-collectOnPremFullMailboxAccess" $host.ui.RawUI.WindowTitle = $windowTitle #Delare global variables. $global:logFile=$NULL #This is the global variable for the calculated log file name [string]$global:staticFolderName="\AuditData\" #Declare function variables. $auditMailboxes=@() [array]$auditFullMailboxAccess=@() [int]$forCounter=0 [int]$powershellCounter = 0 [int]$mailboxCounter=0 [int]$totalMailboxes=0 $commandStartTime = get-date $commandEndTime = $NULL [int]$kerberosRunTime = 4 $xmlFiles = @{ onPremRecipientFullMailboxAccess= @{"Value" = "onPremRecipientFullMailboxAccess.xml" ; "Desscription" = "XML file of discovered permissions"} onPremMailboxList= @{"Value" = "onPremMailboxListFullMailboxAccess.xml" ; "Description" = "XML file of all mailboxes to be processed"} onPremRecipientProcessed= @{"Value" = "onPremRecipientProcessedFullMailboxAccess.xml" ; "Description" = "XML file of the last mailbox processed"} } #Static variables utilized for the Exchange On-Premsies Powershell. $onPremExchangePowershell = @{ exchangeServerConfiguration = @{"Value" = "Microsoft.Exchange" ; "Description" = "Defines the Exchange Remote Powershell configuration"} exchangeServerAllowRedirection = @{"Value" = $TRUE ; "Description" = "Defines the Exchange Remote Powershell redirection preference"} exchangeServerURI = @{"Value" = "https://"+$exchangeServer+"/powershell" ; "Description" = "Defines the Exchange Remote Powershell connection URL"} exchangeServerURIKerberos = @{"Value" = "http://"+$exchangeServer+"/powershell" ; "Description" = "Defines the Exchange Remote Powershell connection URL"} exchangeOnPremisesPowershellSessionName = @{ "Value" = "ExchangePowershell" ; "Description" = "Exchange On-Premises powershell session name."} } new-LogFile -groupSMTPAddress OnPremFullMailboxAccessPermissions -logFolderPath $logFolderPath out-logfile -string "Output bound parameters..." #Output all parameters bound or unbound and their associated values. write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore) write-hashTable -hashTable $onPremExchangePowershell write-hashTable -hashTable $xmlFiles function session-toImport { if ($exchangeAuthenticationMethod -eq "Basic") { try { Out-LogFile -string "Calling New-PowerShellSession" $sessiontoImport=new-PowershellSession -credentials $exchangecredential -powershellSessionName $onPremExchangePowershell.exchangeOnPremisesPowershellSessionName.value -connectionURI $onPremExchangePowershell.exchangeServerURI.value -authenticationType $exchangeAuthenticationMethod -configurationName $onPremExchangePowershell.exchangeServerConfiguration.value -allowredirection $onPremExchangePowershell.exchangeServerAllowRedirection.value -requiresImport:$TRUE } catch { out-logfile -string $_ Out-LogFile -string "ERROR: Unable to create powershell session." -isError:$TRUE } } elseif ($exchangeAuthenticationMethod -eq "Kerberos") { try { Out-LogFile -string "Calling New-PowerShellSession" $sessiontoImport=new-PowershellSession -credentials $exchangecredential -powershellSessionName $onPremExchangePowershell.exchangeOnPremisesPowershellSessionName.value -connectionURI $onPremExchangePowershell.exchangeServerURIKerberos.value -authenticationType $exchangeAuthenticationMethod -configurationName $onPremExchangePowershell.exchangeServerConfiguration.value -allowredirection $onPremExchangePowershell.exchangeServerAllowRedirection.value -requiresImport:$TRUE } catch { out-logfile -string $_ Out-LogFile -string "ERROR: Unable to create powershell session." -isError:$TRUE } } else { out-logfile -string "Major issue creating on-premsies Exchange powershell session - unknown - ending." -isError:$TRUE } try { out-logFile -string "Attempting to import powershell session." import-powershellsession -powershellsession $sessionToImport -isAudit:$TRUE } catch { out-logFile -string "Unable to import powershell session." out-logfile -string $_ -isError:$TRUE -isAudit:$TRUE } try { out-logFile -string "Attempting to set view entire forest to TRUE." enable-ExchangeOnPremEntireForest -isAudit:$TRUE } catch { out-logFile -string "Unable to set view entire forest to TRUE." out-logfile -string $_ -isError:$TRUE -isAudit:$TRUE } } if (($bringMyOwnMailboxes -ne $NULL )-and ($retryCollection -eq $TRUE)) { out-logfile -string "You cannot bring your own mailboxes when you are retrying the collection." out-logfile -string "If mailboxes were previously provided - rerun command with just retry collection." -iserror:$TRUE -isAudit:$TRUE } #Call powershell sesssion. session-toImport #Define the log file path one time. $logFolderPath = $logFolderPath+$global:staticFolderName try { out-logfile -string "Determining mailboxes to process." if ($retryCollection -eq $FALSE) { if ($bringMyOwnMailboxes -eq $NULL) { out-logFile -string "Obtaining all on premises mailboxes." try { $auditMailboxes = get-mailbox -resultsize unlimited -errorAction STOP | select-object Identity,primarySMTPAddress } catch { out-logfile -string "Unable to capture on premises mailboxes." out-logfile $_ -isError:$TRUE -isAudit:$TRUE } #Exporting mailbox operations to csv - the goal here will be to allow retry. $fileName = $xmlFiles.onPremMailboxList.value $exportFile=Join-path $logFolderPath $fileName $auditMailboxes | export-clixml -path $exportFile } else { foreach ($mailbox in $bringMyOwnMailboxes) { out-logfile -string ("Processing mailbox: "+$mailbox) try { $auditMailboxes += get-mailbox -identity $mailbox -errorAction STOP | select-object identity,primarySMTPAddress } catch { out-logfile -string $_ out-logfile -string "Unable to locate a specified mailbox in bring your own mailboxes." -isError:$TRUE } } #Exporting mailbox operations to csv - the goal here will be to allow retry. $fileName = $xmlFiles.onPremMailboxList.value $exportFile=Join-path $logFolderPath $fileName $auditMailboxes | export-clixml -path $exportFile } } elseif ($retryCollection -eq $TRUE) { out-logfile -string "Retry operation - importing the mailboxes from previous export." try{ $fileName = $xmlFiles.onPremMailboxList.value $importFile=Join-path $logFolderPath $fileName $auditMailboxes = import-clixml -path $importFile } catch{ out-logfile -string "Retry was specified - unable to import the XML file." out-logfile -string $_ -isError:$TRUE -isAudit:$true } out-logfile -string "Import the count of the last mailbox processed." try { $fileName = $xmlFiles.onPremRecipientProcessed.value $importFile=Join-path $logFolderPath $fileName $mailboxCounter=Import-Clixml -path $importFile #The import represents the last mailbox processed. #It's permissions were already exported - add 1 to start with the next mailbox in the list. $mailboxCounter=$mailboxCounter+1 out-logfile -string ("Next recipient to process = "+$mailboxCounter.toString()) } catch { out-logfile -string "Unable to read the previous mailbox processed." out-logfile -string $_ -isError:$TRUE -isAudit:$true } out-logfile -string "Importing the previously exported permissions." try { $fileName=$xmlFiles.onPremRecipientFullMailboxAccess.value $importFile=Join-path $logFolderPath $fileName $auditFullMailboxAccess = import-clixml -Path $importFile } catch { out-logfile -string "Unable to import the previously exported permissions." -isError:$TRUE -isAudit:$true } } } catch { out-logFile -string "Unable to get mailboxes." out-logfile -string $_ -isError:$TRUE } #For each mailbox - we will iterate and grab the folders for processing. $ProgressDelta = 100/($auditMailboxes.count); $PercentComplete = (($mailboxCounter / $auditMailboxes.count)*100); $mailboxNumber = 0 $totalMailboxes=$auditMailboxes.count #Here we're going to use a for loop based on count. #This is to support a retry operation. for ($mailboxCounter ; $mailboxCounter -lt $totalMailboxes ; $mailboxCounter++) { #Drop the mailbox into a working variable. $mailbox = $auditMailboxes[$mailboxCounter] $commandEndTime = get-Date if (($forCounter -gt 500) -and (($commandEndTime - $commandStartTime).totalHours -lt $kerberosRunTime)) { start-sleepProgress -sleepstring "Powershell pause at 500 operations - total operation time less than ." -sleepSeconds 5 -sleepParentID 1 -sleepID 2 $forCounter=0 out-logfile -string (($commandEndTime - $commandStartTime).totalhours).tostring() } elseif ($forCounter -gt 500) { start-sleepProgress -sleepstring "Powershell pause at 500 operations - evaluate powershell session reset." -sleepSeconds 5 -sleepParentID 1 -sleepID 2 $forCounter=0 $commandStartTime = get-Date if ($exchangeAuthenticationMethod -eq "Kerberos") { out-logfile -string "Kerberos authentication utilized - reset powershell session." disable-allPowerShellSessions session-toImport } } else { $forCounter++ } out-logfile -string ("Processing recipient = "+$mailbox.primarySMTPAddress) out-logfile -string ("Processing recipient number: "+$mailboxCounter.toString()+" of "+$totalMailboxes.tostring()) $mailboxNumber++ $progressString = "Recipient Name: "+$mailbox.primarySMTPAddress+" Recipient Number: "+$mailboxCounter+" of "+$totalMailboxes Write-Progress -Activity "Processing recipient" -Status $progressString -PercentComplete $PercentComplete -Id 1 $PercentComplete += $ProgressDelta try { $auditFullMailboxAccess+=get-mailboxPermission -identity $mailbox.identity | where {($_.isInherited -ne $TRUE) -and ($_.user -notlike "NT Authority\Self")} } catch { out-logfile -string "Error obtaining folder statistics." out-logfile -string $_ -isError:$TRUE -isAudit:$TRUE } $fileName = $xmlFiles.onPremRecipientFullMailboxAccess.value $exportFile=Join-path $logFolderPath $fileName $auditFullMailboxAccess | Export-Clixml -Path $exportFile $fileName = $xmlFiles.onPremRecipientProcessed.value $exportFile=Join-path $logFolderPath $fileName $mailboxCounter | export-clixml -path $exportFile } write-shamelessPlug disable-allPowerShellSessions } |