简体   繁体   English

如何使用PowerShell Invoke-WebRequest发布

[英]How to use PowerShell Invoke-WebRequest Post

I know we are supposed to add as many details as possible, but I do not know much about how this works, so my details will be minimum. 我知道我们应该添加尽可能多的详细信息,但是我对此工作原理并不了解,因此我的详细信息将是最少的。 I am not sure if this is the correct/best way to do this, so please go easy on me. 我不确定这是否是正确的/最佳方法,所以请放轻松。 Here is my situation: 这是我的情况:

I want to get information from a webpage. 我想从网页获取信息。 I can't give you the webpage because it's internal to a company. 我无法给您该网页,因为它是公司内部的。 Basically the webpage has a form with a bunch of text-boxes and a couple buttons (one being submit). 基本上,该网页具有一个带有一堆文本框和几个按钮(一个正在提交)的表单。 I want to programmatically (with PowerShell - or maybe something else like Python if it would work better) web request the page to submit a piece of information and get the results. 我想以编程方式(使用PowerShell-或其他类似Python的工具(如果能更好地工作))请求网页提交信息并获取结果。

I basically have a list of names that I need to loop through. 我基本上有一个列表,我需要遍历。 Done manually, each name would be pasted into one of the text-boxes on the page, the submit button would be clicked, and the results would pop up. 手动完成,每个名称将被粘贴到页面上的一个文本框中,单击提交按钮,然后将弹出结果。

I want to loop through the list of names and perform a post webrequest on each item, then grab the results. 我想遍历名称列表,并对每个项目执行发布Webrequest,然后获取结果。 Can this be done with PowerShell? 可以使用PowerShell完成吗?

I have been messing around with Invoke-WebRequest, but I am not entirely sure how it works. 我一直在搞乱Invoke-WebRequest,但是我不完全确定它是如何工作的。 I am pretty sure that the webpage can have post requests performed on it because when I execute 我非常确定网页上可以执行发布请求,因为当我执行时

$req = Invoke-WebRequest -URI https://www.foobar.com -Method Post

I do not get any errors about the webpage not accepting posts. 关于网页不接受帖子,我没有任何错误。 Any advice? 有什么建议吗?

Here is some relevant code from the webpage: 这是网页上的一些相关代码:

<button class="Button k-button k-button-first SearchButton" data-categorytext="#SearchByValue, #ServerRequestValue" data-summary="" id="btnSearch_SearchBy">Search</button>
<script>
    jQuery(function(){jQuery("#btnSearch_SearchBy").kendoButton({});});
</script>

The textbox that I need to provide information to 我需要向其提供信息的文本框

<input class="k-textbox SearchField" data-summary="Server Name like " id="ServerNameSearchValue" name="ServerNameSearchValue" style="width: 300px;" type="search" value="SOME_SERVER_NAME_FROM_MY_LIST" />

When the form is submitted, a window pops up on the page with the results. 提交表单后,页面上会弹出一个窗口,显示结果。 The page does not reload or anything, so would I need to perform a get request on the page after I perform the post request to get the information from the new window? 页面不会重新加载或执行任何操作,因此在执行发布请求以从新窗口获取信息之后,是否需要在页面上执行get请求?

You need to specify the POST parameters to send in a Hasthable: 您需要指定POST参数以发送Hasthable:

# Name is the field name on the form
$params = @{Name = 'Some Name'}
$results = Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $params

You can get the comboboxes/textboxes by looking at the generated HTML and adding them to the array like this: 您可以通过查看生成的HTML并将它们添加到数组中来获得组合框/文本框,如下所示:

$params = @{TextBox1 = 'Some Name'; TextBox2 = 'Some Name'}

You can also use a GET request with Invoke-WebRequest, then parse the HTML to get the textbox names and build the array above with the names and values. 您还可以将GET请求与Invoke-WebRequest一起使用,然后解析HTML以获取文本框名称,并使用名称和值构建上面的数组。 That's more complex but valuable if you want to make your code more generic. 如果您想使代码更通用,那会更复杂,但很有价值。

However, no matter what, you will have to set the values of those comboboxes/textboxes in your Powershell script. 但是,无论如何,您都必须在Powershell脚本中设置这些组合框/文本框的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM