Powershell 删除旧的/冗余的activesync伙伴关系

Powershell 删除旧的/冗余的activesync伙伴关系,powershell,activesync,Powershell,Activesync,我使用了一个脚本来获取所有启用了activesync的用户并列出所有连接。许多用户有多个条目,其中他们使用了手机并升级,或在擦除后重新启用 我期待着摆脱任何条目超过30天,只有在一个特定的OU用户,或文本文件中的用户充分 我相信此代码将在整个域中普遍适用: $DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -l

我使用了一个脚本来获取所有启用了activesync的用户并列出所有连接。许多用户有多个条目,其中他们使用了手机并升级,或在擦除后重新启用

我期待着摆脱任何条目超过30天,只有在一个特定的OU用户,或文本文件中的用户充分

我相信此代码将在整个域中普遍适用:

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

$DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}
但我只想为OU或txt列表执行此操作

我可以创建UPN或用户名的.txt列表,这可能比在OU中查找所有用户更容易。如何修改该代码(或更好的代码?)以删除该txt列表的30天以上activesync连接


文本文件选项将是更好的目标首选。

我想我自己回答了,所以为其他人发帖

“==============================================================”
“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”
“==============================================================”

然后删除:

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

我想我自己回答了,所以为别人发帖

“==============================================================”
“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”
“==============================================================”

然后删除:

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