Tuesday, 5 June 2012

Reporting on Apple IOS ActiveSync Devices, v2

For a while I've known that my original Reporting on Apple IOS ActiveSync Devices script needed optimising as it takes hours to parse over large IIS logs.

After spending some time looking at ways to improve it, I came across Log Parser 2.2, which is exactly the right tool for this job as it enables parsing all kinds of log files, including IIS logs, very, very quickly.

I've re-written the script to take advntage of Log Parser. This new code will analyze log files in just a few seconds, and provide the same output as the old script.

Obviously you will need to have Log Parser 2.2 installed for this script to work.

# Hash table of hardware types
$HWVersions = @{
 "iPad" = "iPad";
 "iPad1" = "iPad";
 "iPad2" = "iPad 2";
 "iPad3" = "iPad 3";
 "iPhone" = "iPhone";
 "iPhone1" = "iPhone 3G";
 "iPhone2" = "iPhone 3GS";
 "iPhone3" = "iPhone 4";
 "iPhone4" = "iPhone 4S";
 "iPhone5" = "iPhone 5";
 "iPhone6" = "iPhone 5S";
 "iPod" = "iPod Touch";
 "iPod2" = "iPod Touch 2";
 "iPod3" = "iPod Touch 3";
 "iPod4" = "iPod Touch 4";
 "iPod5" = "iPod Touch 4";
}

# Hash table of IOS versions
$IOSVersions = @{
 "508.11" = "2.2.1";
 "701.341" = "3.0";
 "701.400" = "3.0.1";
 "702.367" = "3.2";
 "702.405" = "3.2.1";
 "702.500" = "3.2.2";
 "703.144" = "3.1";
 "703.146" = "3.1";
 "704.11" = "3.1.2";
 "705.18" = "3.1.3";
 "801.293" = "4.0";
 "801.306" = "4.0.1";
 "801.400" = "4.0.2";
 "802.117" = "4.1";
 "802.118" = "4.1";
 "803.148" = "4.2.1";
 "803.14800001" = "4.2.1";
 "805.128" = "4.2.5";
 "805.200" = "4.2.6";
 "805.303" = "4.2.7";
 "805.401" = "4.2.8";
 "805.501" = "4.2.9";
 "805.600" = "4.2.10";
 "806.190" = "4.3";
 "806.191" = "4.3";
 "807.4" = "4.3.1";
 "808.7" = "4.3.2";
 "808.8" = "4.3.2";
 "810.2" = "4.3.3";
 "810.3" = "4.3.3";
 "811.2" = "4.3.4";
 "812.1" = "4.3.5";
 "901.334" = "5.0";
 "901.405" = "5.0.1";
 "901.406" = "5.0.1";
 "902.176" = "5.1";
 "902.179" = "5.1";
 "902.206" = "5.1.1";
 "902.208" = "5.1.1";
 "1001.403" = "6.0";
 "1001.405" = "6.0";
 "1001.406" = "6.0";
 "1001.407" = "6.0";
 "1001.523" = "6.0";
 "1001.525" = "6.0";
 "1001.550" = "6.0";
 "1001.551" = "6.0";
 "1001.8426" = "6.0";
 "1001.8500" = "6.0";
 "1002.141" = "6.1";
 "1002.142" = "6.1";
 "1002.143" = "6.1";
 "1002.144" = "6.1";
 "1002.145" = "6.1.1";
 "1002.146" = "6.1.2";
 "1002.329" = "6.1.3";
 "1002.350" = "6.1.4";
 "1101.465" = "7.0";
 "1101.470" = "7.0.1";
 "1101.47000001" = "7.0.1";
 "1101.501" = "7.0.2";

}

# Set an alias to Log Parser 2.2
Set-Alias LogParser "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe"

# Logfile paths
$Logpaths  = ("\\exchange01\LogFiles\W3SVC1","\\exchange02\LogFiles\W3SVC1")

# Create an array to store the parsed data
$CsvData = @()

# Loop through the log file paths and parse each log file using Log Parser
ForEach ($Path in $Logpaths) {
 Write-Host $Path
 $Files = Get-ChildItem $Path
 $Logfile = ($Files | Sort-Object -Property LastWriteTime -Descending)[1].FullName
 Write-Host "Importing log file $Logfile"
 $CsvData = $CsvData + (LogParser -i:IISW3C -o:CSV -stats:OFF -headers:OFF "SELECT DISTINCT cs-username AS UserName, cs(User-Agent) AS UserAgent FROM $Logfile WHERE cs(User-Agent) LIKE 'Apple-%' ")
}

# Sort the output and remove any duplicates
$CsvData = $CsvData | Sort-Object | Get-Unique

# Create an array to store the output
$Output = @()

# Loop through each line in the output and expand the useragent into detailed device information
ForEach ($Line in $CsvData) {
  $Object = "" | Select-Object Username,Useragent,Device,Version
  ($Object.Username,$Object.Useragent) = $Line.Split(",")
 ($null,$Object.Device,$Object.Version) = $Object.Useragent.Split("-/")
 If ($Object.Device -match "\w+\dC\d") {
  $Object.Device = $HWVersions.Get_Item($($Object.Device -replace "(\w+\d)C\d",'$1'))
 } Else {
  $Object.Device = $HWVersions.Get_Item($Object.Device)
 }
 $Object.Version = $IOSVersions.Get_Item($Object.Version)
  $Output = $Output + $Object
}

# Write out to the CSV file
$Output | Export-Csv -Path .\Apple-Device-Versions.csv -Force -NoTypeInformation
 
Download Script

- Ben

12 comments:

  1. Very good information, can you be kind enough to update HW Versions and IOS Versions with iPhone5 and iOS6 variants.

    ReplyDelete
  2. Script updated for iPhone 5 and iOS 6.

    - Ben

    ReplyDelete
  3. Updated for new IOS versions.

    - Ben

    ReplyDelete
  4. Here are two newer ones for your script:

    "1002.329" = "6.1.3"
    "1002.350" = "6.1.4"

    Great job!
    -Steve

    ReplyDelete
  5. Curious. Where did you find the details on the build numbers. So far this is the only site/post I found so many shared.

    ReplyDelete
  6. Here is the one for iOS 7 gold master:

    "1101.465" = "7.0"

    -Steve

    ReplyDelete
  7. Hi,

    Very good script! Thank you!

    But how would I edit it to be able to include last sync time?
    Need to get all my devices that have synced since for example, last week.

    Thank you!

    //Patrik

    ReplyDelete
  8. Here are a couple of new bits of info to incorporate into the script,

    "iPhone6" = "iPhone 5S";

    "1101.465" = "7.0"
    "1101.470" = "7.0.1"

    ReplyDelete
  9. Thanks for all the feedback - I've updated the script for the new hardware platform and newest IOS versions.

    - Ben

    ReplyDelete
  10. Ben,

    Any update on the new IOS versions?

    Thanks,
    Dillon

    ReplyDelete
  11. I've been maintaining a pretty up-to-date build list at my site for quite some time now, for reference:
    http://justworks.ca/blog/4c1-the-obscure-ios-user-agent-strings

    ReplyDelete