Miguel Miguel - 13 days ago 9
HTML Question

Regex html, getting input value

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://migueldvl.com/heya/login):

$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

Answer

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