PoshCode Logo PowerShell Code Repository

Get-ActivesyncUsers (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/698"></script>download | new post

Script to retreive all users with an active sync device partnership

  1. #Get all mailboxes that have an active sync partnership:
  2. $Mailboxes = Get-CASMailbox -Filter{HasActiveSyncDevicePartnership -eq $true}| select name, servername, DistinguisheDName, ActiveSyncMailboxPolicy
  3.  
  4. $NumberWiped = 0
  5. $NumberSent =0
  6. $TotalCount = 0
  7. Foreach ($mailbox in $Mailboxes)
  8. {
  9. $Name = $mailbox.Name
  10. $DName= $mailbox.distinguisheDName
  11. $Stats = Get-ActiveSyncDeviceStatistics -mailbox $DName
  12. if ($Stats -ne $null)
  13. {       
  14. $bodySummary = $bodySummary + "<br />User "+ $mailbox.Name +" is on " +$mailbox.ServerName + " using the " +$mailbox.ActiveSyncMailboxPolicy + " policy"
  15. $TotalCount ++
  16. }
  17. }
  18. $body = "<font color=blue>Executive Summary:<br /><br />There are " + $TotalCount +" mailboxes that have a current Active Sync partnership:<br />"
  19. $body$body + $bodySummary
  20. $bodydetail = $bodydetail + "<br /><br />Device Information:"
  21. Foreach ($mailbox in $Mailboxes)
  22. {
  23. $Name = $mailbox.Name
  24. $DName= $mailbox.distinguisheDName
  25. $Stats = Get-ActiveSyncDeviceStatistics -mailbox $DName
  26. if ($Stats.count -gt 1)
  27. {       
  28. $bodydetail = $bodydetail +"<br /><br />User "+ $mailbox.Name + " has the following " + $Stats.Count + " devices:<br />"
  29. }
  30. elseif($Stats.count -lt 2 -and $Stats -ne $null)
  31. {
  32. $bodydetail = $bodydetail +"<br /><br />User "+ $mailbox.Name + " has the following device:<br />"
  33. $bodydetail = $bodydetail + $Stats.count
  34. }
  35. if ($Stats.count -lt 2 -and $Stats -ne $null)
  36. {       
  37. if ($Stats.DeviceWipeRequestTime -ne $null)
  38. {
  39. if ($Stats.DeviceWipeAckTime -ne $null)
  40. {
  41. $NumberWiped ++
  42. $bodydetail = $bodydetail +"<br /><br />This device was wiped on " + $Stats.DeviceWipeAckTime.ToString()
  43. $bodydetail = $bodydetail +"<br />Click here to remove this active sync partnership:<a href= https://webmail.mt.gov/ExchActiveSync/RemoveActiveSyncConfirm.aspx?strIdentity=" + $Stats.Identity +"Remove Partnership>"
  44. }
  45. else
  46. {
  47. $NumberSent ++
  48. $bodydetail = $bodydetail +"><br /><font color=red><strong>This device was sent the wipe command on " + $Stats.DeviceWipeRequestTime.ToString() +"</strong></font color>"
  49. $bodydetail = $bodydetail +"<br />Click here to remove this active sync partnership:<a href= https://webmail.mt.gov/ExchActiveSync/RemoveActiveSyncConfirm.aspx?strIdentity=" + $Stats.Identity +">Remove Partnership"
  50. }
  51. }
  52. $bodydetail = $bodydetail + "<table><TR><TD>FirstSyncTime  </TD><TD>: " + $Stats.FirstSyncTime +"</TD></TR>"
  53. $bodydetail = $bodydetail + "<TR><TD>LastPolicyUpdateTime  </TD><TD>: " + $Stats.LastPolicyUpdateTime +"</TD></TR>"
  54. $bodydetail = $bodydetail + "<TR><TD>LastSyncAttemptTime   </TD><TD>: " + $Stats.LastSyncAttemptTime +"</TD></TR>"
  55. $bodydetail = $bodydetail + "<TR><TD>LastSuccessSync       </TD><TD>: " + $Stats.LastSuccessSync +"</TD></TR>"
  56. $bodydetail = $bodydetail + "<TR><TD>DeviceType            </TD><TD>: " + $Stats.DeviceType +"</TD></TR>"
  57. $bodydetail = $bodydetail + "<TR><TD>DeviceID              </TD><TD>: " + $Stats.DeviceID +"</TD></TR>"
  58. $bodydetail = $bodydetail + "<TR><TD>DeviceUserAgent       </TD><TD>: " + $Stats.DeviceUserAgent +"</TD></TR>"
  59. $bodydetail = $bodydetail + "<TR><TD>DeviceWipeSentTime    </TD><TD>: " + $Stats.DeviceWipeSentTime +"</TD></TR>"
  60. $bodydetail = $bodydetail + "<TR><TD>DeviceWipeRequestTime </TD><TD>: " + $Stats.DeviceWipeRequestTime +"</TD></TR>"
  61. $bodydetail = $bodydetail + "<TR><TD>DeviceWipeAckTime     </TD><TD>: " + $Stats.DeviceWipeAckTime +"</TD></TR>"
  62. $bodydetail = $bodydetail + "<TR><TD>DeviceModel           </TD><TD>: " + $Stats.DeviceModel +"</TD></TR>"
  63. $bodydetail = $bodydetail + "<TR><TD>DeviceFriendlyName    </TD><TD>: " + $Stats.DeviceFriendlyName +"</TD></TR>"
  64. $bodydetail = $bodydetail + "<TR><TD>DeviceOS              </TD><TD>: " + $Stats.DeviceOS +"</TD></TR>"
  65. $bodydetail = $bodydetail + "<TR><TD>DeviceOSLanguage      </TD><TD>: " + $Stats.DeviceOSLanguage +"</TD></TR>"
  66. $bodydetail = $bodydetail + "<TR><TD>DevicePhoneNumber     </TD><TD>: " + $Stats.DevicePhoneNumber +"</TD></TR>"
  67. $bodydetail = $bodydetail + "<TR><TD>Identity              </TD><TD>: " + $Stats.Identity +"</TD></TR></table><BR />"
  68. }
  69. elseif ($Stats.Count -gt 1)
  70. {
  71. foreach ($Stat in $Stats)
  72. {
  73. if ($Stat.DeviceWipeRequestTime -ne $null)
  74. {
  75. if ($Stat.DeviceWipeAckTime -ne $null)
  76. {
  77. $Wiped= ""
  78. $NumberWiped ++
  79. $bodydetail = $bodydetail +"<br /><br />This device was wiped on " + $Stat.DeviceWipeAckTime.ToString()
  80. }
  81. else
  82. {
  83. $NumberSent ++
  84. $bodydetail = $bodydetail +"<br /><STRONG><font color=red>This device was sent the wipe command on " + $Stat.DeviceWipeRequestTime.ToString()+"</STRONG></font color>"
  85. $bodydetail = $bodydetail +"<br />Click here to remove this active sync partnership:<a href= https://webmail.mt.gov/ExchActiveSync/RemoveActiveSyncConfirm.aspx?strIdentity=" + $Stats.Identity +">Remove Partnership"
  86. }
  87. }
  88. $bodydetail = $bodydetail + "<table><TR><TD>FirstSyncTime  </TD><TD>: " + $stat.FirstSyncTime +"</TD></TR>"
  89. $bodydetail = $bodydetail + "<TR><TD>LastPolicyUpdateTime  </TD><TD>: " + $stat.LastPolicyUpdateTime +"</TD></TR>"
  90. $bodydetail = $bodydetail + "<TR><TD>LastSyncAttemptTime   </TD><TD>: " + $stat.LastSyncAttemptTime +"</TD></TR>"
  91. $bodydetail = $bodydetail + "<TR><TD>LastSuccessSync       </TD><TD>: " + $stat.LastSuccessSync +"</TD></TR>"
  92. $bodydetail = $bodydetail + "<TR><TD>DeviceType            </TD><TD>: " + $stat.DeviceType +"</TD></TR>"
  93. $bodydetail = $bodydetail + "<TR><TD>DeviceID              </TD><TD>: " + $stat.DeviceID +"</TD></TR>"
  94. $bodydetail = $bodydetail + "<TR><TD>DeviceUserAgent       </TD><TD>: " + $stat.DeviceUserAgent +"</TD></TR>"
  95. $bodydetail = $bodydetail + "<TR><TD>DeviceWipeSentTime    </TD><TD>: " + $stat.DeviceWipeSentTime +"</TD></TR>"
  96. $bodydetail = $bodydetail + "<TR><TD>DeviceWipeRequestTime </TD><TD>: " + $stat.DeviceWipeRequestTime +"</TD></TR>"
  97. $bodydetail = $bodydetail + "<TR><TD>DeviceWipeAckTime     </TD><TD>: " + $stat.DeviceWipeAckTime +"</TD></TR>"
  98. $bodydetail = $bodydetail + "<TR><TD>LastPingHeartbeat     </TD><TD>: " + $stat.LastPingHeartbeat +"</TD></TR>"
  99. $bodydetail = $bodydetail + "<TR><TD>DeviceModel           </TD><TD>: " + $stat.DeviceModel +"</TD></TR>"
  100. $bodydetail = $bodydetail + "<TR><TD>DeviceIMEI            </TD><TD>: " + $stat.DeviceIMEI +"</TD></TR>"
  101. $bodydetail = $bodydetail + "<TR><TD>DeviceFriendlyName    </TD><TD>: " + $stat.DeviceFriendlyName +"</TD></TR>"
  102. $bodydetail = $bodydetail + "<TR><TD>DeviceOS              </TD><TD>: " + $stat.DeviceOS +"</TD></TR>"
  103. $bodydetail = $bodydetail + "<TR><TD>DeviceOSLanguage      </TD><TD>: " + $stat.DeviceOSLanguage +"</TD></TR>"
  104. $bodydetail = $bodydetail + "<TR><TD>DevicePhoneNumber     </TD><TD>: " + $stat.DevicePhoneNumber +"</TD></TR>"
  105. $bodydetail = $bodydetail + "<TR><TD>Identity              </TD><TD>: " + $stat.Identity +"</TD></TR></table><BR />"
  106. }
  107. }
  108. }
  109. function sendmail([string] $body)
  110. {
  111. $SmtpClient = new-object system.net.mail.smtpClient
  112. $MailMessage = New-Object system.net.mail.mailmessage
  113. $SmtpClient.Host = "smtp.host"
  114. $mailmessage.from = "From.Address@from.com"
  115. $mailmessage.To.add("to.address@to.com")
  116. $mailmessage.Subject = "Mailboxes that have an Active Sync Device Partnership"
  117. $MailMessage.IsBodyHtml = $False
  118. $mailmessage.Body = $body
  119. $MailMessage.IsBodyHtml = $TRUE
  120. $smtpclient.Send($mailmessage)
  121. }
  122. $body = $body + "<br /><br />There are $NumberWiped devices that have been wiped."
  123. $body = $body + "<br />There are $NumberSent devices that have a pending wipe command.</font color>"
  124. $Body = $Body + $BodyDetail
  125. sendmail($Body)
  126. $bodydetail = ""
  127. $Name = ""
  128. $bodySummary = ""

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me