PowerShellからHTTP POSTリクエストを送信する

コンソールでforismatic.comからの引用(または日曜大工)のトピックを読んだ後、好奇心から、PowerShellで同じことを繰り返したいと思いました。 それが起こったことです
 function get-random-quote() { $apiUrl = 'http://www.forismatic.com/api/1.0/' $client = new-object System.Net.WebClient $client.Headers.Add("Content-Type", "application/x-www-form-urlencoded") $client.Encoding = [System.Text.Encoding]::UTF8 [xml]$quote = $client.UploadString($apiUrl, 'method=getQuote&format=xml' ) $quote.forismatic.quote } 


以下の説明と使用例

この関数は、XMLオブジェクトの引用を返します。 次のように使用します。
 PS C:\Windows\system32> $quote = get-random-quote ______________________________________________________________________________________________________________________________________________________________________ PS C:\Windows\system32> $quote quoteText quoteAuthor senderName senderLink --------- ----------- ---------- ----------    —   .    ______________________________________________________________________________________________________________________________________________________________________ PS C:\Windows\system32> $quote.quoteAuthor    


説明はわずかです。


私の観点から、ここで最も興味深いことは、XMLへの変換です。 結果は、標準のPowerShellメソッドで作業できるオブジェクトです。たとえば、
 $client = new-object System.Net.WebClient $client.Encoding = [System.Text.Encoding]::UTF8 [xml]$habr = $client.DownloadString('http://habrahabr.ru/rss/9c1806bb61d9b6612943104ddbf830d9/') $habr.rss.channel.item 


タイプのxml要素のコレクションを返します。

title : title
guid : guid
link : habrahabr.ru/blogs/lenta/92187
description : description
pubDate : Tue, 27 Apr 2010 15:44:41 GMT
author : Mithgol
category : { , , , ...}

title : title
guid : guid
link : habrahabr.ru/blogs/startup/92152
description : description
pubDate : Tue, 27 Apr 2010 15:23:21 GMT
author : AynurEntre
category : {, , -, ...}
....


これは、たとえば次のようにフィルタリングできます。

$habr.rss.channel.item | where { $_.author -eq 'mithgol' } | select link, category

Source: https://habr.com/ru/post/J92206/


All Articles