<html>
<head>
<title>Allow ActiveSync Devices</title>
<HTA:APPLICATION
APPLICATIONNAME = "ActiveSync Users"
ID = "="
VERSION = "1.4"
SINGLEINSTANCE = "yes"/>
<style type = "text/css">
div.footer{
position:absolute;
bottom:9px;
}
div.debug{
position:absolute;
width:250px;
bottom:5px;
left:50px;
}
</style>
</head>
<script language = "VBScript">
Option Explicit
' On Error Resume Next
Dim strResult, strCombo, strExit, strDebug, strScript, strReadLine, strTemp, strTemp2
Dim strDeviceType, strDeviceID, strUserID, strUserName, strAllowed, setTimer, x, y
Dim objFSO, objFile, objShell, objCheckBox
Dim arrUserID(), arrUserName(), arrDeviceID(), arrDeviceType(), chkDevices()
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ASCII = 0
Const Unicode = -1
Const Overwrite = True
Const ReadOnly = False
Const HideWindow = 0
Const ShowWindow = 1
Const WaitToFinish = True
Const DebugIt = True
strExit = "<input id = runbutton type = ""button"" value = ""Exit"" onClick = ""btnExit_OnClick"">"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Sub Window_OnLoad
Window.ResizeTo 640,480
Window.MoveTo 150,150
strCombo = "<i>Loading users, please wait...</i>"
divCombo.InnerHTML = strCombo
divResult.InnerHTML = "<center><br><br><img src = ""activesync2.png"" height = ""150"" width = ""150""></center>"
Set objFile = objFSO.OpenTextFile("./RunPowerShell.bat",ForWriting,Overwrite)
strScript = "powershell -ExecutionPolicy RemoteSigned ./ActiveSyncUsers.ps1" & vbCrLf & _
"rem powershell -ExecutionPolicy RemoteSigned ./ActiveSyncUsers.ps1"
objFile.Write strScript
objFile.Close
strScript = ""
GetActiveSyncUsers
Populate_strCombo
divExit.InnerHTML = strExit
End Sub
Sub Populate_strCombo
strCombo = "<select id=""cboUsers"" name=""cboUsers"" onchange=""cboUsers_OnChange"">" & vbcrlf
For x = 0 To UBound(arrUserName)
strCombo = strCombo & "<option value = """ & x & """>" & arrUserName(x) & "</option>" & vbCrLf
Next
strCombo = strCombo & "</select>"
divCombo.InnerHTML = strCombo
strResult = ""
divResult.InnerHTML = strResult
strDebug = ""
divDebug.InnerHTML = strDebug
DebugCode strResult
End Sub
Sub cboUsers_OnChange
setTimer = Window.setTimeout("UpdateDevices", 100, "VBScript")
End Sub
Sub UpdateDevices
Window.ClearTimeout(setTimer)
divExit.InnerHTML = ""
strResult = "<i>Retrieving ActiveSync devices for " & arrUserName(cboUsers.Value) & "<br>Please wait...</i><br><center><img src = ""activesync2.png"" width = ""150"" height = ""150""></center>"
divResult.InnerHTML = strResult
GetActiveSyncDevices arrUserID(cboUsers.Value)
strResult = strResult & "<i><font color=""red"" size=""1"">*If no devices are selected then all devices will be allowed</font></i><br><br>" & vbCrLf
strResult = strResult & "<input type=""button"" value=""Submit"" name=""btnSubmit"" onClick=""btnSubmit_OnClick"">" & vbCrLf
strResult = strResult & " <input type=""button"" value=""Cancel"" name=""btnCancel"" onclick=""btnCancel_OnClick"">"
divResult.InnerHTML = strResult
DebugCode strResult
strCombo = "Please select the devices you would like to allow<br>for " & arrUserName(cboUsers.Value) & "<br><br>"
divCombo.InnerHTML = strCombo
divExit.InnerHTML = strExit
End Sub
Sub btnSubmit_OnClick
strAllowed = ""
For x = 0 To UBound(arrDeviceID)
Set objCheckBox = Document.GetElementById("chkDevices" & x)
If objCheckBox.Checked Then
If strAllowed = "" Then
strAllowed = objCheckBox.Value
Else
strAllowed = strAllowed & "," & objCheckBox.Value
End If
End If
Next
If strAllowed = "" Then
strAllowed = "all devices"
strScript = "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" & vbCrLf & _
"Set-CasMailbox " & arrUserID(cboUsers.Value) & " -ActiveSyncAllowedDeviceIDs:$null"
'RunPowershell strScript
Else
strScript = "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" & vbCrLf & _
"Set-CasMailbox " & arrUserID(cboUsers.Value) & " -ActiveSyncAllowedDeviceIDs " & strAllowed
'RunPowershell strScript
End If
End Sub
Sub GetActiveSyncUsers
Document.body.style.cursor = "wait"
strScript = "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" & vbCrLf & _
"Get-CASMailbox -resultsize unlimited | where {$_.ActiveSyncEnabled} | Select DisplayName,SamAccountName | fl | out-file ./ActiveSyncUsers.txt"
RunPowershell strScript
Set objFile = objFSO.OpenTextFile("./ActiveSyncUsers.txt",ForReading,ReadOnly,Unicode)
x = 1
ReDim arrUserID(x), arrUserName(x)
Do Until objFile.AtEndOfStream
strReadLine = objFile.ReadLine
If strReadLine <> "" Then
If Left(strReadLine,11) = "DisplayName" Then
ReDim Preserve arrUserName(x)
strReadLine = Replace(strReadLine,"DisplayName","")
strReadLine = Replace(strReadline,":","")
arrUserName(x) = Trim(strReadLine)
ElseIf Left(strReadLine,14) = "SamAccountName" Then
ReDim Preserve arrUserID(x)
strReadLine = Replace(strReadLine,"SamAccountName","")
strReadLine = Replace(strReadline,":","")
arrUserID(x) = Trim(strReadLine)
x = x + 1
End If
End If
Loop
objFile.Close
'Alphabetize array
For x = UBound(arrUserName) -1 To 0 Step -1
For y = 0 to x
If arrUserName(y) > arrUserName(y + 1) Then
strTemp = arrUserName(y + 1)
strTemp2 = arrUserID(y + 1)
arrUserName(y + 1) = arrUserName(y)
arrUserID(y + 1) = arrUserID(y)
arrUserName(y) = strTemp
arrUserID(y) = strTemp2
End If
Next
Next
arrUserName(0) = "Select a user"
arrUserID(0) = "Select a user"
Document.body.style.cursor = "default"
End Sub
Sub GetActiveSyncDevices(Identity)
Document.body.style.cursor = "wait"
strScript = "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" & vbCrLf & _
"Get-ActiveSyncDeviceStatistics -Mailbox " & Identity & " | Select DeviceID, DeviceType | fl | out-file ./ActiveSyncUsers.txt"
RunPowershell strScript
Set objFile = objFSO.OpenTextFile("./ActiveSyncUsers.txt",ForReading,ReadOnly,Unicode)
strResult = ""
x = 0
ReDim arrDeviceID(x)
ReDim arrDeviceType(x)
Do Until objFile.AtEndOfStream
strReadLine = objFile.ReadLine
If Left(strReadLine,8) = "DeviceID" Then
ReDim Preserve arrDeviceID(x)
strReadLine = Replace(strReadLine,"DeviceID","")
strReadLine = Replace(strReadline,":","")
arrDeviceID(x) = Trim(strReadLine)
strResult = strResult & "<input type=""checkbox"" name=""chkDevices" & x & """ value=""" & arrDeviceID(x) & """>"
ElseIf Left(strReadLine,10) = "DeviceType" Then
ReDim Preserve arrDeviceType(x)
strReadLine = Replace(strReadLine,"DeviceType","")
strReadLine = Replace(strReadline,":","")
arrDeviceType(x) = Trim(strReadLine)
strResult = strResult & arrDeviceType(x) & " - " & arrDeviceID(x) & "<br>" & vbCrLf
x = x + 1
End If
Loop
objFile.Close
If strResult = "" Then strResult = "No Devices"
divResult.InnerHTML = strResult
DebugCode strResult
Document.body.style.cursor = "default"
End Sub
Sub RunPowershell(Script)
Set objFile = objFSO.OpenTextFile("./ActiveSyncUsers.ps1",ForWriting,Overwrite,Unicode)
objFile.Write Script
objFile.Close
' objShell.Run "RunPowerShell.bat",ShowWindow,WaitToFinish
objShell.Run "Powershell -ExecutionPolicy RemoteSigned ./ActiveSyncUsers.ps1",ShowWindow,WaitToFinish
' objShell.Run "Powershell -command ""& {./ActiveSyncUsers.ps1}""",ShowWindow,WaitToFinish
End Sub
Sub btnCancel_OnClick
Populate_strCombo
End Sub
Sub btnExit_OnClick
divCombo.InnerHTML = "<i>Cleaning up, please wait...</i>"
divResult.InnerHTML = ""
divExit.InnerHTML = ""
divDebug.InnerHTML = ""
objFSO.DeleteFile("./ActiveSyncUsers.ps1")
objFSO.DeleteFile("./ActiveSyncUsers.txt")
objFSO.DeleteFile("./RunPowerShell.bat")
Set objCheckBox = Nothing
Set objShell = Nothing
Set objFile = Nothing
Set objFSO = Nothing
window.close()
End Sub
Sub DebugCode(HTML)
If DebugIt = True Then
divDebug.InnerHtml = "<xmp>" & HTML & "</xmp>"
End If
End Sub
</script>
<body bgcolor = "white">
<div id = "divCombo" name = "divCombo"> </div>
<div id = "divResult" name = "divResult"> </div>
<div id = "divDebug" name = "divDebug" class = "debug"> </div>
<div id = "divExit" name = "divExit" class = "footer"> </div>
</body>
</html>