Regex html, getting input value

时间:2016-11-26 18:33:04

标签: html powershell parsing

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 个答案:

答案 0 :(得分: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