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?

3
  • 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. Commented Jul 15, 2016 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. Commented Jul 15, 2016 at 16:07
  • I wrote an approach to doing this natively using their API, let me know if this helps. Commented Jul 15, 2016 at 16:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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...

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.