Clear ActiveSync Stale Partnerships – ExchangeOnline

This script is similar to the Clear ActiveSync Stale Partnerships  that I have published in the past for onpremise version of exchange.

https://gallery.technet.microsoft.com/scriptcenter/Clear-ActiveSync-Stale-2a459bd3

As organizations are migrating to office 365 at a rapid pace so are my scripts 🙂

Good thing about office 365 scripts is that there are less things to modify(example organizational settings), these just run by admin credenitials & auto detect your organization.

Download and extract it from below link:

https://gallery.technet.microsoft.com/scriptcenter/Clear-ActiveSync-Stale-fbae8ee5

Edit .ps1 file and update variable section:

 

#########################variables and logs#################

$log = Write-Log -Name “Clearpartnership” -folder logs -Ext log

$days = (get-date).adddays(-60)

$smtpserver = “smtp.labtest.com”

$from = “RemoveActiveSyncpartnerShip@labtest.com”

$to = “ReportsandLogs@labtest.com”

$erroremail = “ReportsandLogs@labtest.com”

#####################################################

 

Log will be generated for processed users & will be placed inside the log folder as well as sent to the specified $to recipient.

Open Powershell & change directory to script path, run the script –> it will prompt you for credentials.

 

Ones authenticated you are done, it will remove the partnerships that are not used since last 60 days (which you have defined in variable section).

 

PowerShell
<#     
    .NOTES 
    =========================================================================== 
     Created on:       8/10/2017 1:23 PM 
     Created by:       Vikas Sukhija(http://SysCloudPro.com) 
     Organization:      
     Filename:         ClearActiveSyncPShip.ps1 
    =========================================================================== 
    .DESCRIPTION 
        This Script will clear activesync partnership that are older than 60 days 
#> 
#################Load Functions and modules############### 
function Write-Log 
{ 
    [CmdletBinding()] 
    param 
    ( 
        [Parameter(Mandatory = $true)] 
        [array]$Name, 
        [Parameter(Mandatory = $true)] 
        [string]$Ext, 
        [Parameter(Mandatory = $true)] 
        [string]$folder 
    ) 
     
    $log = @() 
    $date1 = get-date -format d 
    $date1 = $date1.ToString().Replace("/""-") 
    $time = get-date -format t 
     
    $time = $time.ToString().Replace(":""-") 
    $time = $time.ToString().Replace(" """) 
     
    foreach ($n in $name) 
    { 
         
        $log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext" 
    } 
    return $log 
} 
function LaunchEOL 
{ 
    param 
    ( 
        $Credential 
    ) 
     
    $UserCredential = $Credential 
     
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection 
     
    Import-PSSession $Session -Prefix "EOL" -AllowClobber 
} 
Function RemoveEOL 
{ 
     
    $Session = Get-PSSession | where { $_.ComputerName -like "outlook.office365.com" } 
    Remove-PSSession $Session 
     
} 
#########################variables and logs################### 
$log = Write-Log -Name "Clearpartnership" -folder logs -Ext log 
$days = (get-date).adddays(-60) 
$smtpserver = "smtp.labtest.com" 
$from = "RemoveActiveSyncpartnerShip@labtest.com" 
$to = "ReportsandLogs@labtest.com" 
$erroremail = "ReportsandLogs@labtest.com" 
############connect to Exchange online ###################### 
try 
{ 
    LaunchEOL  
} 
catch 
{ 
    write-host "$($_.Exception.Message)" -foregroundcolor red 
    Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Exchange online Sharedmbx connection Error" -Body $($_.Exception.Message) 
    break 
} 
################################################################ 
 
$casm = get-EOLcasmailbox -resultsize unlimited | where { $_.HasActiveSyncDevicePartnership -eq $true } 
Write-Host "Loaded all ACtiveSYnc mailboxes" -ForegroundColor Green 
$date = Get-Date 
Add-content $log "$date" 
Add-content $log "Loaded all ACtiveSYnc mailboxes" 
 
$casm | foreach-object{ 
    $user = $_.name 
    $devices = Get-EOLMobileDeviceStatistics -Mailbox $_.Identity | Where-Object { ($_.LastSuccessSync -le $days-and ($_.FirstSyncTime -le $days)} 
    Write-Host "Processing....................$user" -ForegroundColor Magenta 
    if ($devices -ne $null) 
    { 
        $devices | foreach{ 
            $deviicemod = $_.DeviceModel 
            $devicetype = $_.DeviceType 
            $LastSuccessSync = $_.LastSuccessSync 
            $guid = [string]$_.Guid 
            $usrname = $user 
             
            Write-host  "processing....$usrname....$devicetype....$deviicemod....$LastSuccessSync...$guid" -foreground green 
             
            Add-content $log "processing....$usrname....$devicetype....$deviicemod....$LastSuccessSync...$guid" 
             
            Remove-EOLMobileDevice -identity ([string]$_.Guid) -confirm:$false 
        } 
    } 
} 
$date = Get-Date 
Add-content $log "$date" 
RemoveEOL 
timeout 10 
Send-MailMessage -SmtpServer $smtpserver -From $from -To $to -Subject "Remove ActiveSync Partnership" -Body "Remove ActiveSync Partnership" -Attachments $log 
####################################################################
Thanks for reading
Sukhija Vikas

 

http://SysCloudPro.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s