Powershell Invoke-WebRequest登录 [英] Powershell Invoke-WebRequest login

查看:286
本文介绍了Powershell Invoke-WebRequest登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用powershell中提供的示例来调用WebRequest我的代码时,我遇到了问题:

$wr = Invoke-WebRequest -URi "http://localhost:51880/Users/Login.aspx" -SessionVariable SMSession
$SMSession
$dbForm = $wr.Forms[0]
$dbForm
$dbForm.fields

$dbForm.Fields["Login1_Username"] = "johnsmith"
$dbForm.Fields["Login1_Password"] = "password1"



$r = invoke-WebRequest -Uri "http://localhost:51880/Users/Login.aspx"  -WebSession $SMSession -Method Post -Body $dbForm.Fields

我收到一条错误消息,提示无法绑定参数'Uri'.无法将值隐蔽到System.Uri.

我已经看过了,尽管此代码有不同的示例,但似乎没有一个适合我.有人可以帮帮我,我不明白这最后一部分在做什么($ r)似乎在做的就是再次调用同一页面而不是实际登录.

谢谢

解决方案

我遇到了同样的问题,我认为我的解决方案也应该对您有用.

首先,安装Fiddler.然后,在提琴手运行时,手动登录网站.您应该在提琴手跟踪中看到登录请求-如果在跟踪上查看"WebForms"选项卡,则应该看到其中填充了用户名"和密码"字段.

由于某些原因,手动登录时,字段名称不同!在powerShell显示为的位置中,字段名称具有"$" 字符 "_" 个字符,例如ContentPlaceHolder $ txtUserName,而不是PowerShell显示的ContentPlaceHolder_txtUserName.

因此,我没有为powerShell声称是表单一部分的Username和password字段分配值,而是删除了这些字段,创建了与Fiddler中显示的相同名称的新字段,然后填充了这些新字段.

此外,由于"$"是powerShell中的特殊字符,因此您必须在其前面加上`才能将其简单地用作美元符号,以防止powerShell认为您是在指变量名的开头./p>

请参见下面的代码:-

$webRequest = Invoke-WebRequest -URi "http://YourWebsite/Login.aspx" -SessionVariable SMSession 

$dbForm = $webRequest.Forms[0] 

# For some reason, the field values returned have underscores instead of $ signs! Need to delete this, then recreate
# with the correct values, using ` to allow $ to be passed without treating it as a variable.
$dbForm.fields.Remove("ContentPlaceHolder_txtUserName")
$dbForm.fields.Remove("ContentPlaceHolder_txtPassword")

$dbform.fields.Add("ContentPlaceHolder`$txtUserName", "admin")
$dbform.fields.Add("ContentPlaceHolder`$txtPassword", "Password2")

#$dbform.fields

$r = invoke-WebRequest -Uri ("http://YourWebsite/Login.aspx") -WebSession $SMSession -Method Post -Body $dbForm.Fields 

$r.RawContent

I'm having an issue using the example given in powershell for invoke-WebRequest my code below:

$wr = Invoke-WebRequest -URi "http://localhost:51880/Users/Login.aspx" -SessionVariable SMSession
$SMSession
$dbForm = $wr.Forms[0]
$dbForm
$dbForm.fields

$dbForm.Fields["Login1_Username"] = "johnsmith"
$dbForm.Fields["Login1_Password"] = "password1"



$r = invoke-WebRequest -Uri "http://localhost:51880/Users/Login.aspx"  -WebSession $SMSession -Method Post -Body $dbForm.Fields

I'm getting an error saying "Cannot bind parameter 'Uri'. Cannot covert value to System.Uri.

I've had a look about and although there are different examples of this code none seem to work for me. Could someone please help me, i don't under stand what this final part is doing ($r) all it seems to be doing is calling the same page again and not actually logging in.

Thanks

解决方案

I had the very same issue and I think my solution should work for you too.

First, install Fiddler. Then while fiddler is running, login to the website manually. You should see the login request in the fiddler trace - If you look at the WebForms tab on the trace you should see the Username and Password fields populated there.

For some reason, when logging in manually, the field names were different! The field names had "$" character in places that powerShell had shown as "_" characters, e.g. ContentPlaceHolder$txtUserName, not ContentPlaceHolder_txtUserName that PowerShell displayed.

So, instead of assigning values to the Username and password fields that powerShell claimed were part of the form, I deleted those, created new fields with the same names shown in Fiddler, then populated those new fields.

Also, since "$" is a special character within powerShell, you need to prefix that with ` in order to use it simply as a dollar sign, to prevent powerShell thinking you are referring to the start of a variable name.

See code below :-

$webRequest = Invoke-WebRequest -URi "http://YourWebsite/Login.aspx" -SessionVariable SMSession 

$dbForm = $webRequest.Forms[0] 

# For some reason, the field values returned have underscores instead of $ signs! Need to delete this, then recreate
# with the correct values, using ` to allow $ to be passed without treating it as a variable.
$dbForm.fields.Remove("ContentPlaceHolder_txtUserName")
$dbForm.fields.Remove("ContentPlaceHolder_txtPassword")

$dbform.fields.Add("ContentPlaceHolder`$txtUserName", "admin")
$dbform.fields.Add("ContentPlaceHolder`$txtPassword", "Password2")

#$dbform.fields

$r = invoke-WebRequest -Uri ("http://YourWebsite/Login.aspx") -WebSession $SMSession -Method Post -Body $dbForm.Fields 

$r.RawContent

这篇关于Powershell Invoke-WebRequest登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆