1

I have this script that successfully logs into a website and performs a query:

$credential = Get-Credential

$postParams = @{username=$credential.UserName;password=$credential.GetNetworkCredential().password}

Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/sessions/?Login -Method POST -Body $postParams -SessionVariable cookie

$query = Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/mods/searchresults/? -WebSession $cookie

What I am trying to do is do a similar thing with another website, Trello. This is the script:

$credential = Get-Credential

$postParams = @{method='password';'factors[user]'=$credential.UserName;'factors[password]'=$credential.GetNetworkCredential().password}

Invoke-WebRequest -Uri https://trello.com/1/authentication -Method POST -Body $postParams -SessionVariable cookie

$result = Invoke-WebRequest -uri https://trello.com/ -WebSession $cookie

However, result variable displays a page as if the user was not logged in, so I'm assuming the session isn't properly saving. How can I fix this?

Brendan McCoy
  • 80
  • 1
  • 6
  • What do you want to do with Trello? They have an API which would be a lot easier to use than trying to login through their webpage. If you give me an idea of what you want to do, I can help. – FoxDeploy Jul 15 '16 at 16:03
  • I'm writing PowerShell script that will use the API, but it want I don't want to hardcode my key or any tokens into it, nor do I want to force anyone using the script to have to remember such things. So my goal is to use webrequests to log into the website and fetch the account's API key and generate a token, so that the API-using script need just be given credentials from CLI. – Brendan McCoy Jul 15 '16 at 16:07
  • I wrote an approach to doing this natively using their API, let me know if this helps. – FoxDeploy Jul 15 '16 at 16:45

2 Answers2

3

If you want to work with remote services on the web, the easiest way to do this is by using their API. So here's how to go that route.

Step 1 signup for the API here: https://trello.com/app-key

Copy down this key as your $trelloKey in the code below.

Step 2 Download this Show-OAuthWindow function which has been customized to work with Trello.

$trellokey ='redacted'

$r = Show-OAuthWindow "https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=$trellokey"

$me = invoke-restmethod "https://api.trello.com/1/members/me/boards?key=$trellokey&token=$global:code"

When you run this code, you'll get a login window. Sign in and then close the form.

enter image description here

Sign in and and click to authorize this token, which we need to use later in our code.

Your token might be longer, this token here was VERY short because of limited perms

When this completes, you'll see this in the console

>we retrieved a code, check within $code for the value

This function returns a global variable of $global:code which contains the authorization token you need to provide when you request any info. For Example, to see information about your user account, the final line stores info about your user account, using the /members/me endpoint. Here's how this request is done:

$endpoint = "https://api.trello.com/1/members/me/boards"
$auth = "?key=$trellokey&token=$global:code"
$request = $endpoint + $auth
Invoke-RESTMethod $request

enter image description here

You can now use any of the API endpoints listed in their catalog to do whatever you'd like with Trello. Simply replace the endpoint with the URL for the info you want, and away you go.

This is a supported and standard way of interacting with web resources, so your efforts will be well spent.

If you DID get this working with web page automation, you would be at the mercy of Trello. If they made changes to their page, you might have to rewrite all of your code.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • when the window popups with the code, powershell waits until I close it, then I get: we retrieved a code, check within $code for the value invoke-restmethod : The request was aborted: Could not create SSL/TLS secure channel. – user310291 Aug 24 '18 at 21:37
  • ok fixed with https://stackoverflow.com/questions/41618766/powershell-invoke-webrequest-fails-with-ssl-tls-secure-channel but – user310291 Aug 24 '18 at 21:43
2

Depending on how the site handles the login, you may have to use Invoke-RestMethod. Here is an example of how you would log in to this site via PowerShell:

$u="username" 
$p="password"

#1. Get page:
$page = Invoke-WebRequest "https://stackoverflow.com/users/login" -SessionVariable so

#2. Fill in login form:
$form = $page.Forms["login-form"]
$form.Fields["email"] = $u
$form.Fields["password"] = $p

#3. Submit login form:
$page = Invoke-RestMethod "https://stackoverflow.com/users/login" -Body $form -Method $form.Method -WebSession $so

#4. Do something else...
morgb
  • 2,252
  • 2
  • 14
  • 14
  • On the website's form, when you press "Log in", it sends the HTTP post with the 3 parameters that I sent in my Invoke-WebRequest that makes the post. Before my current version I did it the way you describe, but I always got a "Cannot POST /login" error, despite the login page using a POST form. – Brendan McCoy Jul 15 '16 at 16:15