Skip to content

Instantly share code, notes, and snippets.

@tasgray
Created November 20, 2016 13:47
Show Gist options
  • Save tasgray/e292c4ef42baadebe7085972213925bf to your computer and use it in GitHub Desktop.
Save tasgray/e292c4ef42baadebe7085972213925bf to your computer and use it in GitHub Desktop.
#
# Manage Exchange Online Delegated Tenants.ps1
#
# Setup credentials
$cred = Get-Credential
Connect-MsolService -Credential $cred
# Get list of tenants & loop
Get-MsolPartnerContract -All | ForEach {
$tenantprefix = [string]$_.DefaultDomainName
# Configure the connection url - note the DelegatedOrg parameter
$ConnectionUri = "https://ps.outlook.com/powershell-liveid?DelegatedOrg=$tenantprefix"
# Connect to Exchange & import session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ConnectionUri -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
##
## Run some exchange cmdlets here
##
# Remove the session - avoid hitting 3 session limit
Remove-PSSession $Session
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment