chthomas

Automate Apple ID Verification

Oct 6th, 2014
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Author/Cobbler: Chris Thomas
  3. Organization: Bloomfield Hills Schools
  4. Email Address: chthomas@bloomfield.org
  5.  
  6. Creation Date: 10/01/14
  7. Last Revision: 10/06/14
  8.  
  9. This script was cobbled together in order to verify Apple ID's in preparation for a large iPad rollout.
  10. It draws from the scripts and ideas of others and uses three modules you'll likely have to download.
  11.  
  12. You'll want a CSV with a header row of:
  13.  
  14.     username,exchangepwd,appleidpwd
  15.  
  16. It will import the Exchange email address and password from your CSV (edit on lines 51-55), then
  17. connect with Exchange Web Services (EWS) to the Client Access Server (CAS) URL (edit on line 105) and
  18. pull the verfication URL from the Inbox, then use AutoBrowse to open an Internet Explorer browser session
  19. with that verification URL, then tab around using WASP and login as the same Apple ID email address and
  20. the Apple ID password from your CSV (edit on lines 51-55).
  21.  
  22. I'm sure there are more elegant ways to accomplish this, but ferme la bouche. ;-)
  23.  
  24. INSPIRATION: Communication with Apple iTunes Store and WebSite
  25. http://d-fens.ch/2013/04/28/communication-with-apple-itunes-store-and-website/
  26.  
  27. MAIN "BORROWED" SCRIPT: Parsing out URL's in the body of a Message with EWS and Powershell
  28. http://gsexdev.blogspot.com/2013/10/parsing-out-urls-in-body-of-message.html
  29.  
  30. EWS MODULE: Microsoft Exchange Web Services Managed API 2.0 (edit on line 48)
  31. http://www.microsoft.com/en-us/download/details.aspx?id=35371
  32.  
  33. AUTOBROWSE MODULE: AutoBrowse - automate even the most annoying webpage (edit on line 45)
  34. http://autobrowse.start-automating.com/
  35. http://gallery.technet.microsoft.com/AutoBrowse-ec4f4384
  36.  
  37. WASP MODULE: Windows Automation Snapin for PowerShell (edit on line 42)
  38. http://wasp.codeplex.com/
  39. #>
  40.  
  41. ## Import WASP
  42. Import-Module 'C:\Users\chthomas\Documents\WindowsPowerShell\Modules\WASP\WASP.dll'
  43.  
  44. ## Import AutoBrowse
  45. Import-Module 'C:\Users\chthomas\Documents\WindowsPowerShell\Modules\AutoBrowse\AutoBrowse.psm1'
  46.  
  47. ## Load EWS API
  48. Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll'
  49.  
  50. ## Define batch variables
  51. $batch = "batch1"
  52. $batchImport = "C:\appleids_" + $batch + ".csv"
  53.  
  54. $batchSuccess = "C:\appleids_" + $batch + "_success.csv"
  55. $batchFailure = "C:\appleids_" + $batch + "_failure.csv"
  56.  
  57. ## Get the Mailbox to Access from the 1st commandline argument
  58. $MailboxName = $args[0]
  59.  
  60. ## Set Exchange Version
  61. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
  62.  
  63. ## Create Exchange Service Object
  64. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
  65.  
  66. ## Set Credentials to use
  67. $psCred = @(Import-Csv $batchImport) | ForEach-Object {
  68.  
  69. $creds = New-Object System.Net.NetworkCredential($_.username.ToString(),$_.exchangepwd.ToString())
  70. $service.Credentials = $creds
  71.  
  72. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
  73. ## Code From http://poshcode.org/624
  74. ## Create a compilation environment
  75. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
  76. $Compiler=$Provider.CreateCompiler()
  77. $Params=New-Object System.CodeDom.Compiler.CompilerParameters
  78. $Params.GenerateExecutable=$False
  79. $Params.GenerateInMemory=$True
  80. $Params.IncludeDebugInformation=$False
  81. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
  82.  
  83. $TASource=@'
  84.  namespace Local.ToolkitExtensions.Net.CertificatePolicy{
  85.    public class TrustAll : System.Net.ICertificatePolicy {
  86.      public TrustAll() {
  87.      }
  88.      public bool CheckValidationResult(System.Net.ServicePoint sp,
  89.        System.Security.Cryptography.X509Certificates.X509Certificate cert,
  90.        System.Net.WebRequest req, int problem) {
  91.        return true;
  92.      }
  93.    }
  94.  }
  95. '@
  96. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
  97. $TAAssembly=$TAResults.CompiledAssembly
  98.  
  99. ## We now create an instance of the TrustAll and attach it to the ServicePointManager
  100. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
  101. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
  102. ## end code from http://poshcode.org/624
  103.  
  104. ## Set the URL of the CAS (Client Access Server) to use
  105. $uri=[system.URI] "https://<FQDN of Exchange>/ews/exchange.asmx"
  106. $service.Url = $uri
  107.  
  108. ## Optional section for Exchange Impersonation
  109. $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
  110. # Bind to the Inbox Folder
  111. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
  112. $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
  113.  
  114. #Define ItemView to retrive just 1 Item
  115. $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1)
  116. $fiItems = $service.FindItems($Inbox.Id,$ivItemView)
  117. [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
  118. foreach($Item in $fiItems.Items){
  119.     #Process Item
  120.     "Processing : " + $Item.Subject
  121.     $dupChk = @{}
  122.     $RegExHtmlLinks = "<a href=\`"(.*?)\`">"
  123.     $matchedItems = [regex]::matches($Item.Body, $RegExHtmlLinks,[system.Text.RegularExpressions.RegexOptions]::Singleline)
  124.     foreach($Match in $matchedItems){
  125.         $SplitVal = $Match.Value.Split('"')
  126.         if($SplitVal.Count -gt 0){
  127.             $ParsedURI=[system.URI]$SplitVal[1]
  128.             if($ParsedURI.Host -eq "id.apple.com"){
  129.                 if(!$dupChk.Contains($ParsedURI.AbsoluteUri)){
  130.                     #Write-Host -ForegroundColor Green  "AppleURL    : " + $ParsedURI.AbsoluteUri
  131.                     $dupChk.add($ParsedURI.AbsoluteUri,0)
  132.                    
  133.                     #Define the Apple ID email address and Apple ID password
  134.                     $appleID = $_.username.ToString()
  135.                     $appleIDPwd = $_.appleidpwd.ToString()
  136.  
  137.                     #Open a browser session with the Apple ID verification URL
  138.                     Open-Browser -Url "$ParsedURI.AbsoluteUri" -Visible
  139.                     $ie = Select-Window IEXPLORE | Select -First 1 | Set-WindowActive
  140.                     Start-Sleep 5
  141.                    
  142.                     #Scrape site and skip the tabs if the Apple ID has already been verified
  143.                     $p = Invoke-WebRequest "$ParsedURI"
  144.                    
  145.                     if($p.ParsedHtml.body.outerText -like "*has already been verified*"){
  146.                        
  147.                         $alreadyBeenVerified = "$appleID,alreadyBeenVerified"
  148.                         $alreadyBeenVerified | Out-File $batchSuccess -Append
  149.                         Write-Host $alreadyBeenVerified
  150.  
  151.                         #Close the browser before the next Apple ID is loaded
  152.                         Select-Window IEXPLORE | Remove-Window
  153.                     }
  154.                     else{
  155.                                            
  156.                         $ie = Select-Window IEXPLORE | Select -First 1 | Set-WindowActive
  157.                         Start-Sleep -Milliseconds 500
  158.  
  159.                         #Press Alt+D to ensure focus starts in the address bar
  160.                         $ie | Send-Keys "%d"
  161.                         Start-Sleep -Milliseconds 500
  162.  
  163.                         #Tab 26 times "because reasons"
  164.                         1..26 | % {
  165.                             $ie | Send-Keys "{TAB}"
  166.                             Start-Sleep -Milliseconds 250
  167.                             }
  168.  
  169.                         #Enter the Apple ID email address and password
  170.                         $ie | Send-Keys "$appleID"
  171.                         Start-Sleep -Milliseconds 500
  172.                         $ie | Send-Keys "{TAB}"
  173.                         Start-Sleep -Milliseconds 500
  174.                         $ie | Send-Keys "$appleIDPwd"
  175.                         Start-Sleep -Milliseconds 500
  176.                         $ie | Send-Keys "{ENTER}"
  177.                         Start-Sleep 5
  178.  
  179.                         #Check if the verification succeeded, display it on screen and log it
  180.                         $verifiedURL = $ie | Get-Browser | Select -expand LocationURL
  181.                    
  182.                         if($verifiedURL -like "https://id.apple.com/IDMSEmailVetting/authenticate.html*"){
  183.                            
  184.                             $hasBeenVerified = "$appleID,hasBeenVerified"
  185.                             $hasBeenVerified | Out-File $batchSuccess -Append
  186.                             Write-Host $hasBeenVerified
  187.  
  188.                             #Close the browser before the next Apple ID is loaded
  189.                             Select-Window IEXPLORE | Remove-Window
  190.                            
  191.                         }
  192.                         elseif($verifiedURL -eq $ParsedURI){
  193.                             $somethingWentWrong = "Something went wrong with $appleID"
  194.                             $somethingWentWrong | Out-File $batchFailure -Append
  195.                             Write-Host $somethingWentWrong
  196.  
  197.                             #Close the browser before the next Apple ID is loaded
  198.                             Select-Window IEXPLORE | Remove-Window
  199.                         }
  200.                         #Check if the tabs messed up again, display it on screen and log it
  201.                         else{
  202.                             $somethingWentWrong = "$appleID,failure"
  203.                             $somethingWentWrong | Out-File $batchFailure -Append
  204.                             Write-Host $somethingWentWrong
  205.  
  206.                             #Close the browser before the next Apple ID is loaded
  207.                             Select-Window IEXPLORE | Remove-Window
  208.                         }
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.     }
  214. }
  215. }
Add Comment
Please, Sign In to add comment