Skip to content

Instantly share code, notes, and snippets.

@Crosse
Last active July 14, 2016 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Crosse/8d6a13bfaf3395be77e288cfdbfafdfb to your computer and use it in GitHub Desktop.
Save Crosse/8d6a13bfaf3395be77e288cfdbfafdfb to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Performs operations in a restartable fashion against Office365.
.DESCRIPTION
This script takes care of ensuring that connections to Office365 are valid
before processing mailboxes, will automatically reconnect to Office365 when
the session is broken, and allows an administrator to "recover" to either a
specific two-letter username prefix or continue from a specific mailbox when
the process dies.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[ValidatePattern("[a-zA-Z]{2}")]
[string]
# The first two letters of the username to start with. The default is "aa".
$StartingPrefix = "aa",
[Parameter(Mandatory=$false)]
[string]
# The full username of the mailbox to start with. Processing will continue from there.
$StartingMailbox = "",
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]
# The Office365 credentials to use.
$Credential,
[Parameter(Mandatory=$false)]
[System.Management.Automation.Runspaces.PSSession]
# An already-existing connection to Office365 that should be used.
# Probably not tested enough to really use.
$Session = $null
)
<#
.SYNOPSIS
Creates a new Office365 session.
#>
function New-Office365Session {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]
# The credentials to use to connect to Office365.
$Credential
)
Write-Verbose "Destroying any previous sessions"
Get-PSSession | ? { $_.ComputerName -match 'outlook.com' -and $_.ConfigurationName -eq 'Microsoft.Exchange' } | Remove-PSSession
$msoExchangeURL = “https://outlook.office365.com/powershell-liveid”
Write-Verbose "Creating new session to $msoExchangeURL"
$oldPref = $VerbosePreference
$VerbosePreference = "SilentlyContinue"
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $msoExchangeURL -ErrorAction Stop `
-Credential $Credential -Authentication Basic -AllowRedirection -Verbose:$false
Write-Verbose "Importing session"
$null = Import-PSSession $session -Prefix Office365 -AllowClobber -Verbose:$false
$VerbosePreference = $oldPref
return $session
}
$a = [int][char]'a'
$z = [int][char]'z'
if (![String]::IsNullOrEmpty($StartingMailbox)) {
$StartingPrefix = $StartingMailbox.Substring(0,2)
}
$totalPrefixes = [Math]::Pow(($z-$a+1), 2)
$processedPrefixes = 0
$processedMailboxes = 0
foreach ($1 in $a..$z) {
foreach ($2 in $a..$z) {
$prefix = ("{0}{1}" -f [char]$1, [char]$2)
$complete = [Int32]($processedPrefixes/$totalPrefixes * 100)
Write-Progress -Id 1 -Activity "Processing mailboxes" -Status "Progress ($processedPrefixes of $totalPrefixes):" `
-CurrentOperation "Prefix '$prefix'" -PercentComplete $complete
if ($prefix -lt $StartingPrefix) {
#Write-Verbose "Skipping $prefix (comes before requested starting prefix of $StartingPrefix)"
$processedPrefixes++
continue
}
if ($Session -eq $null -or $Session.State -ne "Opened") {
$Session = New-Office365Session -Credential $Credential
}
Write-Verbose "Getting all mailboxes for prefix '$prefix'"
$mboxes = Get-Office365Mailbox -ResultSize Unlimited $prefix* -ErrorAction SilentlyContinue | Sort-Object Name
$processedMailboxes = 0
foreach ($mbox in $mboxes) {
if ($mbox -eq $null) { continue }
$pctComplete = [Int32]($processedMailboxes/$mboxes.Count * 100)
Write-Progress -Id 2 -ParentId 1 -Activity "Processing mailboxes starting with $prefix" `
-Status "Progress ($processedMailboxes of $($mboxes.Count)):" -CurrentOperation $mbox.Name -PercentComplete $pctComplete
if (![String]::IsNullOrEmpty($StartingMailbox) -and $mbox.Name -lt $StartingMailbox) {
Write-Verbose "Skipping $($mbox.Name) (comes before requested starting mailbox $StartingMailbox)"
$processedMailboxes++
continue
}
$StartingMailbox = ""
if ($Session -eq $null -or $Session.State -ne "Opened") {
$Session = New-Office365Session -Credential $Credential
}
###############################################
### ###
### Do not alter the code above this point! ###
### ###
###############################################
# DO YOUR PROCESSING RIGHT HERE.
# Call another function or put your code directly
# between the comment blocks.
###############################################
### ###
### Do not alter the code below this point! ###
### ###
###############################################
}
Write-Progress -Id 2 -ParentId 1 -Activity "Processing mailboxes starting with $prefix" -Status "Progress:" -Completed
$processedPrefixes++
}
}
Write-Progress -Id 1 -Activity "Processing mailboxes" -Status "Progress:" -Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment