$response = Invoke-WebRequest -Uri www.google.at
$response | Get-Member
TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject
Name MemberType Definition
---- ---------- ----------
...
Headers Property System.Collections.Generic.Dictionary[string,string] Headers {get;}
Images Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}
InputFields Property Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}
Links Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}
ParsedHtml Property mshtml.IHTMLDocument2 ParsedHtml {get;}
RawContent Property string RawContent {get;set;}
RawContentLength Property long RawContentLength {get;}
RawContentStream Property System.IO.MemoryStream RawContentStream {get;}
Scripts Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}
StatusCode Property int StatusCode {get;}
StatusDescription Property string StatusDescription {get;}
通过 Get-Member
您将知道该对象支持哪些属性。然后您可以在给定对象上调用这些属性:
$response.StatusCode
200
如果要选择多个属性,可以使用Select-Object
:
$response | select statuscode, statusdescription
StatusCode StatusDescription
---------- -----------------
200 OK
当然,您可以将结果存储Select-Object
在变量中。
更多或更少相同可以用于Invoke-RestMethod
:
Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ | Get-Member
TypeName: System.Xml.XmlElement
Name MemberType Definition
---- ---------- ----------
...
comments Property System.Object[] comments {get;}
creator Property System.Xml.XmlElement creator {get;}
description Property System.Xml.XmlElement description {get;}
encoded Property System.Xml.XmlElement encoded {get;}
...
您可以选择/使用的属性取决于响应正文中返回的格式。在上面的示例中,您有一个 XML 正文,但它也可以是 JSON 或其他内容。