Invoke-WebRequest simply downloads the content from any web site. It is then your job to read the content and make sense of it. In the previous tip we explained how you can download PowerShell code, and execute it:

$url = "http://bit.ly/e0Mw9w"
$page = Invoke-WebRequest -Uri $url
$code = $page.Content
$sb = [ScriptBlock]::Create($code)
& $sb

(note: Run above code in a PowerShell console. When run from an editor like PowerShell ISE, the AV engine blocks the call)

Likewise, if the data comes in XML format, you could manually convert it to XML and, for example, retrieve the current exchange rates from a bank:

$url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
$page = Invoke-WebRequest -Uri $url
$data = $page.Content -as [xml]
$data.Envelope.Cube.Cube.Cube

Invoke-RestMethod, in contrast, not only downloads the data but also honors the data type. You get back automatically the correct data. Here are the simplified versions from above, using Invoke-RestMethod instead of Invoke-WebRequest:

Invoke Rick-Ascii (run from PowerShell console):

$url = "http://bit.ly/e0Mw9w"
$code = Invoke-RestMethod -Uri $url
$sb = [ScriptBlock]::Create($code)
& $sb

Retrieve exchange rates:

$url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
$data = Invoke-RestMethod -Uri $url
$data.Envelope.Cube.Cube.Cube

Twitter This Tip! ReTweet this Tip!

Anonymous