Powershell Setting Active Sync Based on group membership
#Start-Transcript c:\activesyncerror.txt
# Set Report File
echo MM > "c:\active.csv"
$outfile = "c:\active.csv"
#Users to query
$user = Get-CASMailbox -resultsize unlimited
foreach ($userfound in $user)
{
$userdn = $userfound.distinguishedName
$usersm = $userfound.SamAccountName
$ADuser=[ADSI]"LDAP://$userdn"
$res = $aduser.memberOf
#$res
#Group to query membership off
$group = get-group "pda users"
$groupdn = $group.distinguishedName
$ADGroup=[ADSI]"LDAP://$Groupdn"
#Check if user is a member of the group
#if not then Active Sync is disabled for the account
if ($res -contains $ADGroup.distinguishedName)
{Write-output "$usersm Is a member of $group , Enabled" >> $outfile
#Set-CASMailbox $users.SamAccountName -ActiveSyncEnabled $true
Write-Host $usersm "Enabled"
}
if ($res -notcontains $ADGroup.distinguishedName)
{Write-output "$usersm Is not a member of $group , Disabled" >> $outfile
Set-CASMailbox $usersm â€"ActiveSyncEnabled $false -whatif
Write-host $usersm "Disabled"
}
}
#Stop-Transcript
$users.count
notepad $outfile
# Set Report File
echo MM > "c:\active.csv"
$outfile = "c:\active.csv"
#Users to query
$user = Get-CASMailbox -resultsize unlimited
foreach ($userfound in $user)
{
$userdn = $userfound.distinguishedName
$usersm = $userfound.SamAccountName
$ADuser=[ADSI]"LDAP://$userdn"
$res = $aduser.memberOf
#$res
#Group to query membership off
$group = get-group "pda users"
$groupdn = $group.distinguishedName
$ADGroup=[ADSI]"LDAP://$Groupdn"
#Check if user is a member of the group
#if not then Active Sync is disabled for the account
if ($res -contains $ADGroup.distinguishedName)
{Write-output "$usersm Is a member of $group , Enabled" >> $outfile
#Set-CASMailbox $users.SamAccountName -ActiveSyncEnabled $true
Write-Host $usersm "Enabled"
}
if ($res -notcontains $ADGroup.distinguishedName)
{Write-output "$usersm Is not a member of $group , Disabled" >> $outfile
Set-CASMailbox $usersm â€"ActiveSyncEnabled $false -whatif
Write-host $usersm "Disabled"
}
}
#Stop-Transcript
$users.count
notepad $outfile


Comments