SHARE
TWEET

Untitled

a guest Sep 18th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Specifying credentials and connecting to Office 365 module
  2. $Credential = Get-Credential
  3. $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
  4.  
  5. Connect-MsolService -Credential $Credential
  6.  
  7. # Getting a list of tenant IDs (clients) used to connect to their environment
  8. $Tenants = (Get-MsolPartnerContract)
  9. $JobsInProgress = $null
  10.  
  11. $TenantsRemaining = 0 # Tickets every time a new tenant is being processed
  12.  
  13. # Running command against all tenants
  14.  
  15. # Keeping track of how long each tenant takes
  16. $StopWatch = [System.Diagnostics.StopWatch]::StartNew()
  17.  
  18. ForEach ($Tenant in $Tenants) {
  19.     # Get primary domain for the tenant
  20.     $Domain = (Get-MsolDomain -TenantId $Tenant.TenantId.Guid `
  21.             | Where-Object { `
  22.                 $_.Name -NotLike "*onmicrosoft.com" -and `
  23.                 $_.Name -NotLike "*microsoftonline.com" })[0].Name
  24.  
  25.     Start-Job -Name $Tenant.Name -ScriptBlock {
  26.         $Domain = $args[1]
  27.         $Credential = $args[2]
  28.  
  29.         # Authenticating to Exchange Online for the tenant
  30.         $Session = New-PSSession `
  31.             -ConfigurationName Microsoft.Exchange `
  32.             -ConnectionUri https://outlook.office365.com/powershell-liveid?DelegatedOrg=$Domain `
  33.             -Credential $Credential `
  34.             -Authentication Basic `
  35.             -AllowRedirection
  36.    
  37.  
  38.         try {
  39.             Import-PSSession $Session `
  40.                 -ErrorAction 'silentlycontinue' `
  41.                 -DisableNameChecking `
  42.                 | Out-Null
  43.        
  44.             $ExistingRule = Get-TransportRule | Where-Object { $_.Name -eq 'Block Microsoft License Verification' }
  45.  
  46.             # Remove existing rule
  47.             if ($ExistingRule) {
  48.                 Remove-TransportRule $ExistingRule.Name -Confirm:$False
  49.             }
  50.             else {
  51.                 New-TransportRule `
  52.                     -Name 'Block Microsoft License Verification' `
  53.                     -SubjectContainsWords 'Microsoft License Verification' `
  54.                     -DeleteMessage $True `
  55.                     | Out-Null
  56.             }
  57.  
  58.             Remove-PSSession $Session
  59.         }
  60.         catch {
  61.             Write-Error $_.Exception.Message
  62.         }
  63.     } -ArgumentList @($Domain, $Credential)
  64. }
  65.  
  66. # Waiting for jobs to finish
  67. do {
  68.     Get-Job | Where-Object State -eq 'Completed' | Remove-Job -Force
  69.     $JobsInProgress = Get-Job
  70.  
  71.     $ElapsedSeconds = [math]::Round($StopWatch.Elapsed.TotalSeconds, 2)
  72.     $TenantsRemaining = $JobsInProgress.Length
  73.  
  74.     Write-Output "Jobs have been running for $ElapsedSeconds seconds... $TenantsRemaining tenants remaining."
  75.  
  76.     Get-Job | Receive-Job # Check for errors
  77.  
  78.     # Removing failed jobs
  79.     Get-Job | Where-Object State -ne 'Running' | Remove-Job -Force
  80.  
  81.     Start-Sleep -Seconds 10
  82. } until ($JobsInProgress.Length -eq 0)
  83.  
  84. Get-PSSession | Remove-PSSession
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top