0

I have this piece of pwershell code, i can't make this work, i already saw several links about this but nothing works. I'm on an Ubuntu machine

I need to get the form token from that url (http://h.com/heya/login):

$result = Invoke-WebRequest -Uri 'http://hey.com/heya/login' -SessionVariable tok
$found = $result -match 'name="token" value="(.*?)"'
$token = $matches[1]

Write-Host $token # this is empty

Update: The code works on powershell console, but for me didn't worked with the command powershell -File file.ps1, so i thought the code was wrong. After i updated it worked like were supose

1 Answer 1

2

Recommendation: Upgrade to the latest PowerShell Alpha. It looks like you're running an old version, based upon the comments on this answer.

It works fine for me on Windows 10 Anniversary Update.

PS C:\Users\TrevorSullivan> $result = Invoke-WebRequest -Uri 'http://migueldvl.com/heya/login' -SessionVariable tok
$found = $result -match 'name="token" value="(.*?)"'
$token = $matches[1]

Write-Host $token # this is empty
80565711d0a74e6c0d29792ec2d029dc

PS C:\Users\TrevorSullivan> $PSVersionTable

Name                           Value                                                                                             
----                           -----                                                                                             
PSVersion                      5.1.14393.206                                                                                     
PSEdition                      Desktop                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                           
BuildVersion                   10.0.14393.206                                                                                    
CLRVersion                     4.0.30319.42000                                                                                   
WSManStackVersion              3.0                                                                                               
PSRemotingProtocolVersion      2.3                                                                                               
SerializationVersion           1.1.0.1                                                                                           

enter image description here

EDIT: To accommodate the updated question, I also tested this on Ubuntu 16.04 Xenial Xerus, under a Docker environment. It seems to work there, too.

enter image description here

PS /> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.12
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

It also works perfectly fine when I use the -File parameter on the PowerShell binary. Ignore the odd output -- that's a fault of the PowerShell host, I believe.

enter image description here

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

9 Comments

Are you calling the script from PowerShell Core on Ubuntu, or are you calling it on a Windows system with PowerShell Desktop Edition?
Oh okay, I haven't tested it with PowerShell Core. Sorry I should have tried that -- I totally glossed over that in your original question (looks like you edited it right about the time I posted my answer). :)
@Miguel I just tested on Ubuntu 16 using a Docker container, running PowerShell Core 6.0.0 Alpha 12. Seems to work for me. What version of PowerShell Core are you specifically running? Might want to upgrade.
@Miguel I was responding to your first comment, which looks like is now gone. You had mentioned Ubuntu in that first comment on my answer. :) Happy to help -- try the latest alpha and see if that works, or post back with the specific version you're using.
@Miguel Hang on, let me try the -File parameter.
|

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.