Thursday, July 15, 2010

Exchange 2007/2010 iPhone activesync discovery script

Update ** There is one problem with the script in this post in that its doesn't pick up the updates to the O/S as pointed out in http://www.hedonists.ca/2010/07/22/blocking-the-iphone-part-i. So the only 100% way would be to parse the IIS logs where you can grab the same information about the devices. I'll try to post a script that does parsing that should work on 2003 up. Thanks also to Dennis who sent another version of the script I've added to the download that give enhanced reporting** ** New scripted posted for IIS logs http://gsexdev.blogspot.com/2010/08/parsing-iis-log-activesync-traffic-for.html **

On January 9, 2007 Apple released their first iphone and unleashed on the world what is arguably one of the most successful user mobile devices on the planet (I'm not arguing technical merit here just end user experience). Kind of like a virus these phones have spread throughout corporations and into every nook and cranny some with the IT departments blessing others with their scorn. Whether you like or hate the devices if your in charge of managing an Exchange server it can be useful to see how many of these devices are out there and what version of the Apple IOS they are running. This can help you keep abreast of any issues or limitations these devices might impose on your users or server.

On Exchange 2007/2010 getting information about what devices have been registered via ActiveSync is exceeding easy thanks to the Get-ActiveSyncDeviceStatistics cmdlet. With this cmdlet there are two distinct properties that are of interest when trying to report on iPhones. The first is the DeviceType property which gets set to iPhone for any iPhone. The second is the DeviceUserAgent which holds information about what version of IOS is running on the phone and in the later versions of the IOS holds information about what type of iPhone it is (eg 3G,3GS,4 etc).

Now this is where we strike the first problem like all good software the version information that you can get out of the DeviceUserAgent doesn't relate directly to the version of IOS the phone is running so some parsing and translation is needed. This is where the good old Internet helps out firstly on Wikipedia all the IOS versions have been documented at http://en.wikipedia.org/wiki/Apple_iOS_version_history . So this page gives us a IOS version to SDK build version this still doesn't quite get us to what is being stored in the deviceUserAgent property this needs to be translated with a little more code which looks like

$inparray = $inputvar.Split(',')
$v1 = $inparray[1].Substring(0,1)
$v2 = $inparray[1].Substring(1,1)
$v3 = $inparray[1].Substring(2,($inparray[1].Length-2))
$useragent = "{0:D2}" -f (([int][char]$v2)-64)

essentially this reformats the alpha character to a numerical represntation from 0-26. This then allows the IOS version to be matched. The actually phone version seems to only work on later version of the IOS and I've based it on information from a few forum posts so this is a little imcomplete I also didn't really have any information on how the iPad appears so this script i dont think will detect one but Im intrested in any feedback anyone has about other devices or codes they notice.

So what the rest of the script does is basically using the Get-Mailbox and Get-ActiveSyncDeviceStatistics cmdlet is it enumerates all iPhones activesync registrations for all users and uses some custom objects to build a Html report and then send it to a nomintated email address configured in the following varibles via SMTP using the configured server.

$sendAlertTo = "user@domain.com"
$sendAlertFrom = "Senderfromaddress@domain.com"
$SMTPServer = "servername"

These three varibles need to be configured before you will get a result out of the script an alternate method to sending the report as an email is just to output the result to a HTML file using Out-file eg you would modify the script like

$appleCollection | ConvertTo-HTML -head $tableStyle –body $body | out-file c:\iphreport.htm

I've put a download of this script here the code itself looks like

$sendAlertTo = "user@domain.com"
$sendAlertFrom = "Senderfromaddress@domain.com"
$SMTPServer = "servername"

$appleCollection = @()
$appleverhash = @{ }
$hndSethash = @{ }

function addtoIOShash($inputvar){
$inparray = $inputvar.Split(',')
$v1 = $inparray[1].Substring(0,1)
$v2 = $inparray[1].Substring(1,1)
$v3 = $inparray[1].Substring(2,($inparray[1].Length-2))
$useragent = "{0:D2}" -f (([int][char]$v2)-64)
$apverobj = "" | select IOSVersion,IOSReleaseDate,ActiveSyncUserAgent,AppleBuildCode
$apverobj.IOSVersion = $inparray[0]
$apverobj.IOSReleaseDate = $inparray[2]
$apverobj.AppleBuildCode = $inparray[1]
$apverobj.ActiveSyncUserAgent = $v1 + $useragent + "." + $v3
$appleverhash.add($apverobj.ActiveSyncUserAgent,$apverobj)

}



#IOSVersion,Deviceid,ReleaseDate
addtoIOShash("1,1A543,Jun-07")
addtoIOShash("1.0.1,1C25,Jul-07")
addtoIOShash("1.0.2,1C28,Aug-07")
addtoIOShash("1.1,3A100,Sep-07")
addtoIOShash("1.1,3A101,Sep-07")
addtoIOShash("1.1.1,3A109,Sep-07")
addtoIOShash("1.1.1,3A110,Sep-07")
addtoIOShash("1.1.2 ,3B48,Nov-07")
addtoIOShash("1.1.3,4A93,Jan-08")
addtoIOShash("1.1.4,4A102,Feb-08")
addtoIOShash("1.1.5,4B1,Jul-08")
addtoIOShash("2,5A347,Jul-08")
addtoIOShash("2.0.1,5B108,Aug-08")
addtoIOShash("2.0.2,5C1,Aug-08")
addtoIOShash("2.1,5F136,Sep-08")
addtoIOShash("2.1,5F137,Sep-08")
addtoIOShash("2.1,5F138,Sep-08")
addtoIOShash("2.2,5G77,Nov-08")
addtoIOShash("2.2.1,5H11,Jan-09")
addtoIOShash("3,7A341,Jun-09")
addtoIOShash("3.0.1,7A400,Jul-09")
addtoIOShash("3.1,7C144,Sep-09")
addtoIOShash("3.1,7C145,Sep-09")
addtoIOShash("3.1,7C146,Sep-09")
addtoIOShash("3.1.2,7D11,Oct-09")
addtoIOShash("3.1.3,7E18,Feb-09")
addtoIOShash("3.2,7B367,Apr-10")
addtoIOShash("4.0,8A293,Jun-10")


$hndSethash.add("1C2","IPhone 3G")
$hndSethash.add("2C1","IPhone 3GS")
$hndSethash.add("3C1","IPhone 4")

Get-Mailbox -ResultSize Unlimited | ForEach-object {
$Mb = $_
Get-ActiveSyncDeviceStatistics -Mailbox:$_.Identity | foreach-object {

if($_.DeviceType -eq "iPhone"){
$userRepobj = "" | select UserName,emailAddress,IphoneType,IOSVersion,IOSReleaseDate,ActiveSyncUserAgent,ActiveSyncDeviceUserAgent,AppleBuildCode
if ($_.DeviceUserAgent -ne $null){
$apcode = $_.DeviceUserAgent
$userRepobj.ActiveSyncDeviceUserAgent = $_.DeviceUserAgent
$apcodearray = $apcode.split("/")
if ($apcodearray[0].length -gt 12){
$userRepobj.IphoneType = $hndSethash[$apcodearray[0].Substring(12,3)]
}
$userRepobj.IOSVersion = $appleverhash[$apcodearray[1]].IOSVersion
$userRepobj.IOSReleaseDate = $appleverhash[$apcodearray[1]].IOSReleaseDate
$userRepobj.ActiveSyncUserAgent = $appleverhash[$apcodearray[1]].ActiveSyncUserAgent
$userRepobj.AppleBuildCode = $appleverhash[$apcodearray[1]].AppleBuildCode
$userRepobj.UserName = $Mb.Name
$userRepobj.emailAddress = $Mb.WindowsEmailAddress
$appleCollection += $userRepobj
}
}
}

}

$tableStyle = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
}
TH{border-width: 1px;
padding: 10px;
border-style: solid;
border-color: black;
background-color:#66CCCC
}
TD{border-width: 1px;
padding: 2px;
border-style: solid;
border-color: black;
background-color:white
}
</style>
"@

$body = @"
<p style="font-size:25px;family:calibri;color:#ff9100">
$TableHeader
</p>
"@



$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.host = $SMTPServer
$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.To.Add($sendAlertTo)
$MailMessage.From = $sendAlertFrom
$MailMessage.Subject = "iPhone Registrration Report"
$MailMessage.IsBodyHtml = $TRUE
$MailMessage.body = $appleCollection | ConvertTo-HTML -head $tableStyle –body $body
$SMTPClient.Send($MailMessage)

Monday, July 05, 2010

Multi-User quick create Powershell form for Exchange 2007

One of the more mind numbing tasks that Exchange administrators perform is creating new users and mailboxes. If you've ever been asked to create 5-10 users at the same time and are staring at a number of very repedative mouse clicks especially if you detests wizards then this script can help out. This is version 2 of this script i created a number of years ago that allowed creating just one user at at time this improves on this original idea by using a Datagrid instead so allowing you to enter multiple users and being able to create the users with just one click (well hopefully). Its also quite usefull in development enviroments where you might want to create a large number of users at once. The script itself is fairly simple in that it just wrappers a simple Winform around the create mailbox command and users a Datagrid for multiline input.

I've put a download of the code here the script itself looks like

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

Function createmailbox{
foreach($row in $dgDataGrid.rows){
if ($row.Cells[0].Value -ne $null){
$psSecurePasswordString = new-object System.Security.SecureString
foreach($char in $row.Cells[5].Value.ToCharArray())
{
$psSecurePasswordString.AppendChar($char)
}
$result = New-mailbox -UserPrincipalName $row.Cells[0].Value -alias $row.Cells[4].Value -database $MBhash1[$msMailStoreDrop.SelectedItem.ToString()] `
-Name $row.Cells[3].Value -OrganizationalUnit $OUhash1[$ouOuNameDrop.SelectedItem.ToString()] -password $psSecurePasswordString `
-FirstName $row.Cells[1].Value -LastName $row.Cells[2].Value -DisplayName $row.Cells[3].Value
if($result -ne $null){
[system.windows.forms.messagebox]::Show("Mailbox " + $result + " Created")
}
else{
[system.windows.forms.messagebox]::Show("Error createing " + $row.Cells[0].Value + " check cmdline")
}

}
}

}

$OUhash1 = @{ }
$MBhash1 = @{ }

$form = new-object System.Windows.Forms.form
$form.Text = "Exchange 2007 Quick User Create Form"
$form.size = new-object System.Drawing.Size(800,800)

$msTable = New-Object System.Data.DataTable

$msTable.TableName = "GroupName"
$msTable.Columns.Add("UPN-AccountName")
$msTable.Columns.Add("FirstName")
$msTable.Columns.Add("LastName")
$msTable.Columns.Add("DisplayName")
$msTable.Columns.Add("Alias")
$msTable.Columns.Add("Password")


# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGridView
$dgDataGrid.Location = new-object System.Drawing.Size(10,10)
$dgDataGrid.size = new-object System.Drawing.Size(750,200)
$dgDataGrid.AutoSizeRowsMode = "AllHeaders"
$form.Controls.Add($dgDataGrid)

$dgDataGrid.DataSource = $msTable


# Add OU Drop Down
$ouOuNameDrop = new-object System.Windows.Forms.ComboBox
$ouOuNameDrop.Location = new-object System.Drawing.Size(100,260)
$ouOuNameDrop.Size = new-object System.Drawing.Size(230,30)
$ouOuNameDrop.Items.Add("/Users")
$OUhash1.Add("/Users","Users")
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = '(objectClass=organizationalUnit)'
$searcher.PropertiesToLoad.Add("canonicalName")
$searcher.PropertiesToLoad.Add("Name")
$searcher1 = $searcher.FindAll()
foreach ($person in $searcher1){
[string]$ent = $person.Properties.canonicalname
$OUhash1.Add($ent.substring($ent.indexof("/"),$ent.length-$ent.indexof("/")),$ent)
$ouOuNameDrop.Items.Add($ent.substring($ent.indexof("/"),$ent.length-$ent.indexof("/")))
}
$form.Controls.Add($ouOuNameDrop)


# Add OU DropLable
$ouOuNamelableBox = new-object System.Windows.Forms.Label
$ouOuNamelableBox.Location = new-object System.Drawing.Size(10,260)
$ouOuNamelableBox.size = new-object System.Drawing.Size(100,20)
$ouOuNamelableBox.Text = "OU Name"
$form.Controls.Add($ouOuNamelableBox)

# Add Server Drop Down
$snServerNameDrop = new-object System.Windows.Forms.ComboBox
$snServerNameDrop.Location = new-object System.Drawing.Size(100,290)
$snServerNameDrop.Size = new-object System.Drawing.Size(130,30)
get-mailboxserver | ForEach-Object{$snServerNameDrop.Items.Add($_.Name)}
$snServerNameDrop.Add_SelectedValueChanged({
$msMailStoreDrop.Items.Clear()
get-mailboxdatabase -Server $snServerNameDrop.SelectedItem.ToString()| ForEach-Object{$msMailStoreDrop.Items.Add($_.Name)
$MBhash1.add($_.Name,$_.ServerName + "\" + $_.StorageGroup.Name + "\" + $_.Name)
}
})
$form.Controls.Add($snServerNameDrop)

# Add Server DropLable
$snServerNamelableBox = new-object System.Windows.Forms.Label
$snServerNamelableBox.Location = new-object System.Drawing.Size(10,290)
$snServerNamelableBox.size = new-object System.Drawing.Size(100,20)
$snServerNamelableBox.Text = "ServerName"
$form.Controls.Add($snServerNamelableBox)

# Add MailStore Drop Down
$msMailStoreDrop = new-object System.Windows.Forms.ComboBox
$msMailStoreDrop.Location = new-object System.Drawing.Size(100,320)
$msMailStoreDrop.Size = new-object System.Drawing.Size(130,30)
$form.Controls.Add($msMailStoreDrop)

# Add MailStore DropLable
$msMailStorelableBox = new-object System.Windows.Forms.Label
$msMailStorelableBox.Location = new-object System.Drawing.Size(10,320)
$msMailStorelableBox.size = new-object System.Drawing.Size(100,20)
$msMailStorelableBox.Text = "Mail-Store"
$form.Controls.Add($msMailStorelableBox)


# Add Create Button

$crButton = new-object System.Windows.Forms.Button
$crButton.Location = new-object System.Drawing.Size(10,360)
$crButton.Size = new-object System.Drawing.Size(150,23)
$crButton.Text = "Create Mailboxs"
$crButton.Add_Click({CreateMailbox})
$form.Controls.Add($crButton)

$form.topmost = $true
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()