如何使用Powershell访问宁静的Web服务?

编程入门 行业动态 更新时间:2024-10-24 04:46:30
本文介绍了如何使用Powershell访问宁静的Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要集成一个现有的powershell脚本,以通过返回json的静态Web服务更新其状态.我对Powershell有点陌生,但是我发现System.Net.WebRequest对象可以执行以下操作.

I need to integrate an existing powershell script to update it's status via a restful web service that returns json. I'm a bit new to powershell but I was able to find the System.Net.WebRequest object do something like the following.

$a = [System.Net.WebRequest]::Create("intranet/service/object/") $a.Method = "GET" $a.GetResponse()

返回对象的json数组

which returns a json array of objects

[ {id:1}, {id:2}] // etc

我不确定从这里开始以及如何将其解析为本地数据类型.我也希望能够发布和删除.

I'm not sure where to go from here and how to parse this into a native datatype. I'd like to be able to post and delete as well.

有指针吗?还有任何json/rest库或Command-let吗?

Any pointers? And are there any json/rest libraries or command-lets?

推荐答案

您需要的是PowerShell 3 及其 Invoke-RestMethod , ConvertTo-Json 和 ConvertFrom-Json cmdlet .您的代码最终看起来像:

What you want is PowerShell 3 and its Invoke-RestMethod, ConvertTo-Json, and ConvertFrom-Json cmdlets. Your code will end up looking like:

$ stuff = invoke-RestMethod -Uri $ url -Method Get;

$stuff = invoke-RestMethod -Uri $url -Method Get;

,甚至不需要在生成的$ stuff =>上调用 ConvertFrom-Json ,它已经是可用的非字符串格式了.

and there shouldn't even be a need to invoke ConvertFrom-Json on the resulting $stuff => it's already in a usable non-string format.

对于POST | PUT,只需使用PowerShell哈希和数组来构造数据,然后在将其传递给invoke-RestMethod或invoke-WebRequest之前调用 ConvertTo-Json :

As for POSTs|PUTs, simply use PowerShell hashes and arrays to structure your data and then call ConvertTo-Json on it before passing it to invoke-RestMethod or invoke-WebRequest:

调用WebRequest -Uri $ url -ContentType application/json-方法发布-Body $ objectConvertedToJson

invoke-WebRequest -Uri $url -ContentType application/json -Method Post -Body $objectConvertedToJson

请参见 technet.microsoft/en-us/Library /hh849971.aspx 了解详情.

更多推荐

如何使用Powershell访问宁静的Web服务?

本文发布于:2023-11-08 14:13:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   宁静   Powershell   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!