daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Jan 20th, 2017 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This method will finish an IE download by forcing the 'ALT+S' key several times to ensure that the "Save" button gets
  2. # hit. It then waits a specified amount of time before returning so that the user can allow the file download to be
  3. # expected to have finished.
  4. Function Finish-Download([__ComObject]$ie, [int]$iterations, [int]$sleep) {
  5.     Write-Verbose "Finish-Download: Entered 'Finish-Download ie iterations'"
  6.     Write-Debug "Finish-Download: 'iterations': $iterations"
  7.     Write-Debug ("Finish-Download: 'ie.HWND': " + $ie.HWND)
  8.  
  9.     Write-Warning "Please do not make any mouse or keyboard interactions for this next step."
  10.  
  11.     Write-Verbose "Finish-Download: Locating 'ie' process"
  12.     $ieProc = Get-Process | Where-Object { $_.Name -eq "iexplore" -and $_.MainWindowHandle -eq $ie.HWND }
  13.  
  14.     Write-Verbose "Finish-Download: Attaching 'WScript.Shell' to 'ieProc'"
  15.     Write-Debug ("Finish-Download: 'ieProc.Id': " + $ieProc.Id)
  16.     $wScript = New-Object -ComObject WScript.Shell
  17.     if (-not $wScript.AppActivate($ieProc.Id)) {
  18.         Write-Error "Failed to attach WScript.Shell to appropriate process."
  19.         Return $false
  20.     }
  21.  
  22.     # For SOME reason, WScript takes a moment to activate the process. So we need to sleep. During this part it's
  23.     # important that you don't make any mouse/keyboard movements.
  24.     # Another hiccup: IE isn't a great browser, as a result sometimes the ALT+S command doesn't work properly, so
  25.     # we're sending it multiple times to make sure that it get's received by Internet Explorer.
  26.     Sleep 1
  27.     Write-Verbose "Finish-Download: Sending 'ALT' then 'ALT+S' $iterations times"
  28.     do {
  29.         Write-Debug "Finish-Download: 'iterations': $iterations"
  30.         $wScript.SendKeys("%")
  31.         $wScript.SendKeys("%S")
  32.         $iterations = $iterations - 1
  33.     } while ($iterations -gt 0)
  34.     # Note: this will save it to the default location. You can mess with WScript if you wish to use the "Save As"
  35.  
  36.     Sleep 1
  37.     Write-Warning "You may use your mouse and keyboard again."
  38.  
  39.     Write-Verbose "Finish-Download: Sleep for $sleep seconds to wait for download to complete"
  40.     Sleep $sleep
  41. }
  42.  
  43. # You'll have to mess with a lot of this for your specific web-page layout, but it should be fairly self-explanatory
  44. Function Download-File([string]$url, [string]$user, [string]$pass) {
  45.     try {
  46.         Write-Verbose "Download-File: Entered 'Download-File url user pass'"
  47.         Write-Debug "Download-File: 'url': $url"
  48.         Write-Debug "Download-File: 'user': $user"
  49.         Write-Debug ("Download-File: 'pass': " + $pass.Length.ToString() + " chars")
  50.         # This is the ID for the login text-box and the password text-box
  51.         $usernameElement = "login_field"
  52.         $passwordElement = "password"
  53.  
  54.         Write-Verbose "Download-File: Creating 'Wait-NavigationCompleted[__ComObject]'"
  55.         # Define a function to wait for navigation to finishe
  56.         Function Wait-NavigationCompleted([__ComObject]$ie) {
  57.             Write-Verbose "Wait-NavigationCompleted: Waiting for navigation to complete..."
  58.             do {
  59.                 Write-Debug ("Wait-NavigationCompleted: 'ie.Busy': " + $ie.Busy)
  60.                 sleep 1
  61.             }
  62.             until (-not ($ie.Busy))
  63.         }
  64.  
  65.         Write-Verbose "Download-File: Creating 'ie' object"
  66.         # Define our IE object
  67.         $ie = New-Object -ComObject InternetExplorer.Application
  68.  
  69.         # We NEED IE to be visible to download stuff appropriate, since we can't follow a specific URL to get to it. (If it's
  70.         # possible to do so, let me know and I can work up an example of parsing a URL out of the page to get it to be
  71.         # navigateable.)
  72.         $ie.Visible = $true
  73.         Write-Debug ("Download-File: 'ie.Visible': " + $ie.Visible.ToString())
  74.  
  75.         Write-Verbose "Download-File: Navigate to '$url'"
  76.         # Navigate to the root login page
  77.         $ie.Navigate($url)
  78.  
  79.         Wait-NavigationCompleted($ie)
  80.  
  81.         Write-Verbose "Download-File: Creating 'doc' object"
  82.         # Thanks to how programming works, we only need to set $doc once
  83.         $doc = $ie.Document
  84.  
  85.         try {
  86.             Write-Verbose "Download-File: Attempting to log in."
  87.             Write-Debug "Download-File: 'usernameElement': $usernameElement"
  88.             Write-Debug "Download-File: 'passwordElement': $passwordElement"
  89.             $doc.getElementById($usernameElement).value = $user
  90.             $doc.getElementById($passwordElement).value = $pass
  91.  
  92.             # Here you can either use:
  93.             # $doc.getElementById("button-id").click()
  94.             # IF it has a valid ID.
  95.             # In the case of the test website I used it doesn't, so we'll iterate all buttons on the page to find one that
  96.             # looks right
  97.             $doc.getElementsByName("commit") | % {
  98.                 if ($_.value -eq "Sign in") {
  99.                     $_.click()
  100.                 }
  101.             }
  102.  
  103.             Wait-NavigationCompleted($ie)
  104.         } catch {
  105.             Write-Information "Login failed, user likely already logged in."
  106.         }
  107.  
  108.         Write-Verbose "Download-File: Looking for button to click"
  109.         # This next part can get tricky, I'm not sure how EAM is set up, but you *may* be able to do this without using
  110.         # virtual mouse clicks. You would have to find what the element information is for the button you want to click
  111.         # (type, anything unique to it like the name). In my example, going to the GitHub repo is just a simple link:
  112.  
  113.         # Basically, you need to perform each step in this area. Make sure to `Wait-NavigationCompleted($ie)` between each
  114.         # one to ensure that you don't accidentally try to do something else when a page is loading. To check a check-box,
  115.         # you just have to:
  116.         $doc.getElementsByTagName("a") | % {
  117.             if ($_.innerText -ne $null -and $_.innerText.Contains("StackExchange")) {
  118.                 $_.click()
  119.             }
  120.         }
  121.  
  122.         Wait-NavigationCompleted($ie)
  123.  
  124.         Write-Verbose "Download-File: Clicking 'Download' button"
  125.         # To download a file:
  126.         $doc.getElementsByTagName("a") | % {
  127.             if ($_.innerText -ne $null -and $_.innerText.Contains("Download ZIP")) {
  128.                 $_.click()
  129.             }
  130.         }
  131.  
  132.         Wait-NavigationCompleted($ie)
  133.  
  134.         # Once you have hit the "download" button, we'll use WScript to send a few commands to finish that process off.
  135.         # Sleep long enough that the file will download. If it's remote, and the net can be slow, I would consider
  136.         # increasing this value a bit. (The second number is sleep time, the first is iterations.)
  137.         Finish-Download $ie 5 5
  138.  
  139.         Write-Verbose "Download-File: Clicking 'Sign out' button"
  140.         # You should logout so that you don't leave a hanging session next time you run the script (though I did design
  141.         # it to look for a hanging session and ignore it).
  142.         $doc.getElementsByTagName("button") | % {
  143.             if ($_.innerText -ne $null -and $_.innerText.Contains("Sign out")) {
  144.                 $_.click()
  145.             }
  146.         }
  147.  
  148.         Wait-NavigationCompleted($ie)
  149.  
  150.         Write-Verbose "Download-File: Killing 'ie' process"
  151.         # Kill the IE process
  152.         $ie.Quit()
  153.  
  154.         Return $true
  155.     } catch {
  156.         $Error.ForEach({ Write-Error $_ })
  157.         Return $false
  158.     }
  159. }
  160.  
  161. # Alter these to log what you want, valid values are "Continue" and "SilentlyContinue" for each
  162. $VerbosePreference = "Continue"
  163. $DebugPreference = "Continue"
  164. $ProgressPreferece = "Continue"
  165.  
  166. Write-Verbose "Root: Script started"
  167.  
  168. # Leave this one as it is
  169. $InformationPreference = "Continue"
  170.  
  171. # Define your web service to connect to and the login page, include `http://` or `https://`
  172. $webService = "https://github.com/login"
  173.  
  174. # Define your username and password
  175. $username = "USERNAME"
  176. $password = "PASSWORD"
  177.  
  178. Write-Information "Beginning file download"
  179. if (Download-File $webService $username $password) {
  180.     Write-Information "File downloaded successfully."
  181. } else {
  182.     Write-Error "File could not be downloaded."
  183. }
  184.  
  185. Exit
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top