Search code examples
powershellactivesync

Remove old/redundant activesync partnerships


I have used a script to get all users that have activesync enabled and list all of the connections. Many users have multiple entries where they have used a phone and upgraded, or re-enabled after a wipe.

I am looking to get rid of any entry above 30 days, only for users in a specific OU, or text file full of users.

I believe this code will work universally across the domain:

$DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")}

$DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}

but I only want to do it for either an OU, or txt list.

I can create a .txt list of either the UPN, or the username, which may be easier than looking for all users in an OU. How would I modify that code (or altogether better code?) to remove 30 day+ activesync connections for that txt list?

Text file option would be preferred for a better target.


Solution

  • I think I self answered, so posting for others.

    “==============================================================”
    “Start Mailbox Retrieve”
    “==============================================================”
    $mbx = get-casmailbox -resultsize unlimited | where {$_.activesyncenabled -eq $true} ;
    “==============================================================”
    “End Mailbox Retrieve”
    “==============================================================”
    $mbx | foreach {
    “Processing: “+$_.name
    $name = $_.name;
    $device = get-activesyncdevicestatistics -mailbox $_.identity | where {$_.LastSuccessSync -le (Get-Date).AddDays(“-30”)};
    if($device){
    {
    ”
    Device: “+$dev.DeviceType
    $csvRows += $dev
    }
    }
    }
    “==============================================================”
    “Start CSV Write”
    “==============================================================”
    $csvRows | Export-Csv “c:\ps\staledevices.csv” -NoType
    “==============================================================”
    “End CSV Write”
    “==============================================================”
    

    From http://techtalklive.org/ttlblog/removing-stale-activesync-devices/

    Then to remove:

    Import-Csv c:\ps\staledevices.csv |foreach {remove-activesyncdevice -identity $_.guid -confirm:$false}
    

    From http://techtalklive.org/ttlblog/removing-stale-activesync-devices/