📜 ⬆️ ⬇️

Sending HTTP POST requests from PowerShell

After reading the Topical Quotes from forismatic.com in the console (or fortune with your own hands) out of curiosity, I wanted to repeat the same thing on PowerShell. That's what happened
 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 } 


Below explanations and example of use

The function returns an XML object quotation. Use this:
 PS C:\Windows\system32> $quote = get-random-quote ______________________________________________________________________________________________________________________________________________________________________ PS C:\Windows\system32> $quote quoteText quoteAuthor senderName senderLink --------- ----------- ---------- ----------    —   .    ______________________________________________________________________________________________________________________________________________________________________ PS C:\Windows\system32> $quote.quoteAuthor    

')
Explanations are small:


From my point of view, the most interesting thing here is the conversion to XML. The result is an object with which you can work in standard ways PowerShell, for example
 $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 


Returns a collection of xml elements of the type:

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 : {, , -, ...}
....


Which can be filtered, for example like this:

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

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


All Articles