Powershell通过登录调用webrequest

时间:2016-01-08 21:08:17

标签: powershell login webrequest

我有一个工地,我正在尝试“浏览”。

用这个做完......

$login = Invoke-WebRequest -Uri 'http://www.privateworksite.com' -SessionVariable GetIP
$form = $login.Forms[0]
$Form.Fields['ctl00$ContentPlaceHolder1$RadTextBox7']= "*******"  <-- User ID goes here
$Form.Fields['ctl00$ContentPlaceHolder1$RadButton1'] = "submit"  <-- Submit button
$afterfirstclick Invoke-WebRequest -Uri 'http://www.privateworksite.com' -WebSession $GetIP -Method POST -Body $form.Fields

现在我在页面上,我需要点击出现的新按钮。我试图添加一个-sessionvariable $ afterfirstclick但是我得到了可怕的PS Red行声明你不能将-WebSession和-SessionVariable放在一起。

添加信息: 那么如何点击$ afterfirstclick中生成的按钮?这是我第一次点击返回后按钮所具有的。

ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl04$GECBtnExpandColumn  <-- a new button to create 
a drop down table with the info I need. The table is not there prior to the 
clicking the button so I need to click this button.

我已经全身心投入并且曾经有关于登录网站的演示/帖子,但之后你如何在网站上移动?

我试图将$ afterfirstclick加入-WebSession,bust ... 还试图用下一个字段重复整个事情来点击按钮,这只会导致新网站不是当前的“延续”。

从哪里开始?

哦,网址永远不会改变,所以我无法提交威盛网址。

1 个答案:

答案 0 :(得分:4)

好的找到了解决方案!我需要将$afterfirstclick提供给下一个invoke-webrequest。我觉得这很愚蠢,不是那么明显。

最终结果......

$url = "http://private.com"  #<-- setup the URL
$tool = Invoke-WebRequest -Uri 'http://private.com' -UseDefaultCredentials -SessionVariable fb #<--create the site in a variable

write-host "1111111111111111" #<--just a marker to seperate the two requests so I can see the form.fields clearly
$Form = $tool.Forms[0] #<--first invoke form.fields
$Form.Fields #<--just printing them so I can see them
$Form.Fields['ctl00$ContentPlaceHolder1$RadTextBox7']= "USERID" #<--entering USERID into the form
$Form.Fields['ctl00$ContentPlaceHolder1$RadButton1'] = "submit" #<--clicking the submit button

$mainPage = Invoke-WebRequest  ("$url" + $Form.Action) -WebSession $fb -Body $Form.Fields -Method Post #<--create the new page by calling the page with the fields
$mainPage.RawContent | out-file h:\response.html #<--sending it to a file so I can see the first page and second page side by side

write-host "2222222222222222" #<--just a marker to seperate the two requests so I can see the form.fields clearly
$Form2 = $mainPage.Forms[0] #<--pulling the new page form.fields NOTICE I am using the previous variable for the first invoke
$Form2.Fields #<--printing fields to take a look at what needs to be included in the next round
$Form2.Fields['ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl04$GECBtnExpandColumn'] = "submit" #<--The newly generated button that needed to be clicked to continue

$mainPage2 = Invoke-WebRequest  ("$url" + $Form2.Action) -WebSession $fb -Body $Form2.Fields -Method Post -UseDefaultCredentials #<--OK BIG ONE!! First the form.field needs to be correct as well as using the original -websession!!!
$mainPage2.RawContent | out-file h:\response1.html #<--output the second resulting page to compare

来观看第二个请求。它必须使用第一个invoke来创建新的form.fields,但仍然使用原始的-websession变量。

不知道为什么但是这样可以这样工作,但我用这个用于另一个网站深入到它所以它是坚实的,只是不断重复链。您甚至可以使用foreach循环和所有已知的form.fields