<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Get-Web</title>
	<atom:link href="http://huddledmasses.org/tag/get-web/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Using REST API&#8217;s from PowerShell with the Dream SDK</title>
		<link>http://huddledmasses.org/using-rest-apis-from-powershell-with-the-dream-sdk/</link>
		<comments>http://huddledmasses.org/using-rest-apis-from-powershell-with-the-dream-sdk/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 22:56:52 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Get-Web]]></category>
		<category><![CDATA[Http]]></category>
		<category><![CDATA[HttpRest]]></category>
		<category><![CDATA[HttpWebRequest]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[wGet]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=838</guid>
		<description><![CDATA[Awhile back I wrote a Get-Web script, which I later converted to a compiled Get-Web cmdlet and module (pssnapin) ... adding support for POST forms. At the time, it seemed like GET and POST with some string variables pretty much covered what I&#8217;d ever want to do with web pages, however I found a few [...]]]></description>
			<content:encoded><![CDATA[	<p>Awhile back I wrote a <a href="wget-2-for-powershell">Get-Web script</a>, which I later converted to a compiled <a href="get-web-another-round-of-wget-for-powershell">Get-Web cmdlet</a> and module (pssnapin) ... adding support for <span class="caps">POST</span> forms.  At the time, it seemed like <span class="caps">GET</span> and <span class="caps">POST</span> with some string variables pretty much covered what I&#8217;d ever want to do with web pages, however I found a few places where I needed to <span class="caps">POST</span> a file attachment, and then a couple of full-on <span class="caps">REST</span> APIs, which use not just <span class="caps">GET</span> and <span class="caps">POST</span>, but <span class="caps">PUT</span> and <span class="caps">DELETE</span> as well. </p>

	<p>While I was playing with <a href="http://huddledmasses.org/of-dekiwiki-and-powershell-a-script-module/">scripting the <span class="caps">REST</span> <span class="caps">API</span> for DekiWiki</a> &#8230; I experimented with the <a href="http://wiki.developer.mindtouch.com/Dream">MindTouch Dream SDK</a> &#8230; and as a result, I&#8217;ve refactored the part of that module that actually does the <span class="caps">HTTP</span> work out into a little PowerShell 1.0 compatible script library. Of course, it depends on several dll&#8217;s (mindtouch.core.dll, mindtouch.dream.dll, and SgmlReaderDll.dll and log4net.dll) from the Dream <span class="caps">SDK</span>, which you can <a href="http://sourceforge.net/project/showfiles.php?group_id=173074">download from SourceForge</a> (ok, they&#8217;ve made the Dream <span class="caps">SDK</span> stuff hard to find, so here&#8217;s a temporary link to <a href="http://huddledmasses.org/downloads/HttpRest.7z">my module</a> with the DLLs in it).</p>

	<h2>Example Uses</h2>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Retrieve Google search results:</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Google</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET http:<span style="color: #66cc66;">//</span>www.<span style="color: #003366;">google</span>.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>search @<span style="color: #333;">&#123;</span>q<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> <span style="color: #003366; font-weight: bold;">Xml</span> <span style="color: #009900;">&quot;//h3[@class='r']/a&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> href, InnerText <br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Download a file from a web page</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">WebFile</span></span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span>,<span style="color: #660033; font-weight: bold;">$cred</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #000066;">-auth</span> <span style="color: #660033; font-weight: bold;">$cred</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> File &nbsp;<br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Upload a PowerShell script to PasteBin</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Paste</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PastebinURI</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;http://posh.jaykul.com/p/&quot;</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$file</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$file</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">Exists</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ofs</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> POST <span style="color: #660033; font-weight: bold;">$PastebinURI</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; format<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;posh&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># PowerShell</span><br />
&nbsp; &nbsp; &nbsp; expiry<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;d&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># (d)ay or (m)onth or (f)orever</span><br />
&nbsp; &nbsp; &nbsp; poster<span style="color: #66cc66;">=</span>$<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Security.<span style="color: #003366;">Principal</span>.<span style="color: #003366;">WindowsIdentity</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetCurrent</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Name</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; code2<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;$((gc $file) -replace &quot;</span>http:<span style="color: #66cc66;">//</span><span style="color: #009900;">&quot;,&quot;</span>http``:<span style="color: #66cc66;">//</span><span style="color: #009900;">&quot;)&quot;</span> <span style="color: #666666; font-style: italic;"># To get past the spam filter.</span><br />
&nbsp; &nbsp; &nbsp; paste<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Send&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Type</span> FORM_URLENCODED <span style="color: #000066;">-Wait</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$result</span>.<span style="color: #003366;">AsDocument</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToXml</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;//*[@class='highlight']/*&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">href</span><br />
&nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;File Not Found&quot;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">#########################################################</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Example, to download my PasswordRequired.dll:</span><br />
<span style="color: #660033; font-weight: bold;">$cred</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;test&quot;</span>, $<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">ConvertTo-<span style="font-style: normal;">SecureString</span></span> <span style="color: #009900;">&quot;password&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">WebFile</span></span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>downloads<span style="color: #66cc66;">/</span>PasswordRequired.<span style="color: #003366;">dll</span> <span style="color: #660033; font-weight: bold;">$cred</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Example, to upload a script called TestScript.ps1 to the Pastebin we use on IRC:</span><br />
<span style="color: #660033;">ls</span> TestScript.<span style="color: #003366;">ps1</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Paste</span></span> <span style="color: #009900;">&quot;http://posh.jaykul.com/p/&quot;</span><br />
<br />
&nbsp;</div>

	<p>There are basically two main functions: Invoke-Http and Receive-Http. There are also a few extra functions which are mostly just for use by these two functions, and then Set-HttpDefaultUrl and Set-HttpCredential which you can use to simplify your scripts. I intend to rewrite these as cmdlets in C# and contribute them to the <a href="http://CodePlex.com/PowerShellCX">PowerShell Community Extensions</a> along with some of my other networking cmdlets (Get-<span class="caps">DNS</span>), so if you have any suggestions, I&#8217;d love to hear them!</p>

	<h3>Invoke-Http</h3>

	<p>Invokes <span class="caps">HTTP</span> verbs (like <span class="caps">GET</span>, <span class="caps">POST</span>, <span class="caps">PUT</span>, and <span class="caps">DELETE</span>) against a <span class="caps">URL</span> with optional parameters (eg: name = value), input, etc &#8230; and returns the output as a MindTouch.Dream.Result<MindTouch.Dream.DreamMessage> or (optionally) as a MindTouch.Dream.DreamMessage &#8230;</p>

	<h4>Syntax</h4>

	<p><code>Invoke-Http</code>  <code>[-Verb] &#60;String&#62;</code>  <code>[[-Path] &#60;String&#62;]</code>  <code>[[-With] &#60;Hashtable&#62;]</code>  <code>[[-Content] &#60;Object&#62;]</code>  <code>[[-Type] &#60;String&#62;]</code>  <code>[[-Authenticate] &#60;String&#62;]</code>  <code>[-WaitForResponse]</code></p>

	<h4>Parameters</h4>

	<h5>Verb</h5>

	<p>The <span class="caps">HTTP</span> verb you want to invoke.  Defaults to <strong>GET</strong>, but can be anything your <span class="caps">HTTP</span> server supports.</p>

	<h5>Path</h5>

	<p>The <span class="caps">URL</span> you want to invoke against.  Can be a partial <span class="caps">URL</span>, if you first call <code>Set-HttpDefaultURL</code> first. You can also specify this as an array or a hashtable. This is particularly useful if you&#8217;re specifying a partial <span class="caps">URL</span>, and your <span class="caps">URL</span> is built based on logic in your code. If you pass an array, the values in the array are joined with forward-slashes. If you pass a hashtable, the keys and values are appended joined with forward-slashes, but the values are double encoded (by UrlEncode), ie: <code>$key/$(UrlEncode(UrlEncode($value))</code></p>

	<h5>With</h5>

	<p>The parameters to be passed with the verb call, as a hashtable. (these are particularly used for <span class="caps">POST</span>, etc). These are passed in the query as named parameters, with the values encoded.</p>

	<h5>Content</h5>

	<p>Anything you want to pass as content. In actuality, we really only handle three things: <span class="caps">XML</span> Documents, file paths (the file will be used as content), and everything else is passed as simple text.  If you need other data types, the &#8220;DreamMessage&#8221; that the scripts depend on can handle all sorts of data types, so you&#8217;ll just need to add it around line 50 of the script.</p>

	<h5>Type</h5>

	<p>This is the type of content.  We can automatically deduce this for <span class="caps">XML</span> or File objects, but if you want to force a type, you should use this parameter.  For instance, to fake a web form submission, you need to specify the type as <span class="caps">FORM</span> or FORM_URLENCODED but you can also specify the type as any of:  <span class="caps">ATOM</span>, <span class="caps">BINARY</span>, <span class="caps">BMP</span>, <span class="caps">CSS</span>, DREAM_EXCEPTION, FORM_URLENCODED, <span class="caps">GIF</span>, <span class="caps">HTML</span>, <span class="caps">HTTP</span>, <span class="caps">JPEG</span>, JS, <span class="caps">JSON</span>, MULTIPART_MIXED, <span class="caps">NOTHING</span> (mimetype -/-), <span class="caps">ANY</span> (mimetype */*), <span class="caps">PDF</span>, <span class="caps">PHP</span>, <span class="caps">PNG</span>, <span class="caps">RDF</span>, <span class="caps">RELAXNG</span>, <span class="caps">SVG</span>, ANY_TEXT (mimetype text/*), <span class="caps">TEXT</span> (mimetype text/plain, with the ISO-8859-1 encoding), TEXT_UTF8 (mimetype text/plain, with UTF8 encoding), TEXT_XML, <span class="caps">TIFF</span>, <span class="caps">VCAL</span>, <span class="caps">VERSIT</span>, <span class="caps">XHTML</span>, <span class="caps">XML</span>, <span class="caps">XRDS</span>.</p>

	<h5>Authenticate</h5>

	<p>You can pass either a boolean (in which case the HTTPRest scripts will use a default <code>$global:HttpRestCredential</code> which it will prompt for if you haven&#8217;t set it), or an actual PSCredential or NetworkCredential object.  In any case, this causes the invoke to contain network credentials. Note that sometimes you may need to pre-authenticate <strong>before</strong> making calls to a web-service.</p>

	<h5>WaitForResponse</h5>

	<p>A switch parameter that causes the query to wait until the response completes downloading, <strong>and return a DreamResponse</strong> instead of returning an incomplete <strong>Result<DreamResponse></strong> ...  this is useful when you know the response is a simple <span class="caps">XML</span> file and you need to parse it to determine whether your call has succeeded.  If you need to actually get the data out, or if you just need to know the method was invoked successfully, you shouldn&#8217;t pass this parameter.</p>

	<h3>Receive-Http</h3>

	<p>Receives the response or result from a Dream.Result or DreamMessage. as <span class="caps">XML</span>, XDoc, text, raw bytes, or even to file.  This function is really only useful to receive the output of Invoke-Http when you need the response content.</p>

	<h4>Syntax</h4>

	<p><code>Receive-Http</code>  <code>[[-Output] &#60;string:[Xml|File|Text|Bytes]&#62;]</code>  <code>[[-Path] &#60;string&#62;]</code>  <code>[[-InputObject] &#60;MindTouch.Dream.Result&#60;MindTouch.Dream.DreamMessage&#62;&#62;]</code> </p>

	<p><code>Receive-Http</code>  <code>[[-Output] &#60;string:[Xml|File|Text|Bytes]&#62;]</code>  <code>[[-Path] &#60;string&#62;]</code>  <code>[[-InputObject] &#60;MindTouch.Dream.DreamMessage&#62;]</code></p>

	<h4>Parameters</h4>

	<h5>Output</h5>

	<p>The <span class="caps">TYPE</span> of output you want: 
	<ul>
		<li><strong>Xml</strong> will return an XmlDocument &#8212; or XmlNode(s) if you pass a Path.</li>
		<li><strong>File</strong> will write to file &#8212; currently, you <strong>must</strong> pass a path, I didn&#8217;t make it guess it automatically.</li>
		<li><strong>Text</strong> will output text &#8212; in the case where the <strong>actual</strong> output is <span class="caps">XML</span>, this returns the InnerText.</li>
		<li><strong>Bytes</strong> returns an array of bytes&#8230;</li>
	</ul>
	<ul>
		<li><strong>XDoc</strong> returns a MindTouch.Dream.XDoc (not the .Net native class), which has a boatload of extra web-related methods and features you might find useful such as accepting XPath operators as indexers.</li>
	</ul></p>

	<h5>Path</h5>

	<p>A string containing the path for file output or XPath selector for <span class="caps">XML</span>, XDoc, and Text output</p>

	<h5>InputObject</h5>

	<p>The input <em>must</em> be either a <code>Result&#60;DreamMessage&#62;</code> or a <code>DreamMessage</code> itself.  Values passed on the pipeline are accepted into this parameter.</p>

	<h3>Set-HttpDefaultUrl</h3>

	<p>This takes the passed <span class="caps">URI</span> and sets it as the base (default) <span class="caps">URL</span> for the Invoke-Http function (if you set this, any path passed to Invoke-Http is treated as <em>relative</em> to this path).</p>

	<h3>Set-HttpCredential</h3>

	<p>This takes a <code>PSCredential</code> or <code>NetworkCredential</code> and stores it as a PSCredential for use by the Invoke-Http function. Particularly useful when you want to script a bunch of authenticated methods without being re-prompted. Note that if you don&#8217;t pass a credential, you&#8217;ll be prompted for one the usual way, so calling just <code>Set-HttpCredential</code> is sufficient when working on the command-line.</p>

	<h2>Miscellaneous</h2>

	<p>There are other functions in here, a few should be considered &#8220;internal&#8221; functions (when this is a module, they would not be exported: Get-DreamMessage, Get-DreamPlug, Get-HttpCredential) and a few of which came out of my utilities functions (like Encode-Twice, and Join-Url, Get-FileName, Get-UtcTime, and Get-CredentialBetter)...</p>

	<h2>Code (Updated 28 July 2010)</h2>

	<p><script type="text/javascript" src="http://PoshCode.org/embed/2028"></script></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/using-rest-apis-from-powershell-with-the-dream-sdk/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Get-Web (Another round of wget for PowerShell)</title>
		<link>http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/</link>
		<comments>http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/#comments</comments>
		<pubDate>Fri, 02 May 2008 02:08:08 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Get-Web]]></category>
		<category><![CDATA[Http]]></category>
		<category><![CDATA[HttpWebRequest]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[wGet]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=520</guid>
		<description><![CDATA[Well, after multiple attempts at a wget PowerShell script (the last one works very well for downloading web pages, files, as long as you don&#8217;t need to send post parameters or anything like that) ... I found myself writing a script last week that included a custom HTTP POST function as well as using some [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, after multiple attempts at a <a href="http://huddledmasses.org/wget-2-for-powershell/">wget PowerShell script</a> (the last one works very well for downloading web pages, files, as long as you don&#8217;t need to send post parameters or anything like that) ... I found myself writing a script last week that included a custom <span class="caps">HTTP</span> <span class="caps">POST</span> function as well as using some prior functionality I wrote (<code>ConvertFrom-Html</code>) to convert <span class="caps">HTML</span> files to <span class="caps">XML</span> documents &#8212; which PowerShell can deal with nicely.</p>

	<p>So I&#8217;ve taken my <a href="http://huddledmasses.org/converting-html-to-xml-in-powershell/">Huddled.Html</a> code and worked it in together with a few extra bits &#8230; and I&#8217;m releasing it under the new name &#8220;PoshHttp&#8221; (which has the sole benefit of being short, in the case when you need to type the full command names, like: <code>PoshHttp\Get-Web</code>.</p>

	<p>In lieu of providing proper documentation for this, there&#8217;s a script below which shows off some of the features of the <code>Get-Web</code> cmdlet &#8230; but first, you&#8217;re going to want to <a href="/downloads/PoshHttp.7z">download PoshHttp</a> and I can give you a basic overview.</p>

	<h4>Get-Web </h4>

	<p>Downloads a file from the web <span class="caps">URI</span>, optionally passing <span class="caps">POST</span> or <span class="caps">GET</span> parameters which it accepts as a Hashtable. If the resulting file is an xml or html file, it converts it to <span class="caps">XML</span> and outputs it as an <code>XmlDocument</code> object. If they are not, it saves them to file, and outputs the <code>FileInfo</code> object. </p>

	<ul>
		<li><strong>Plain</strong> will prevent the conversion to xml</li>
		<li><strong>Save</strong> will force saving to file (either xml or binaries)</li>
		<li><strong>Path</strong> lets you specify the file name/path (normally, it selects a path based on what you&#8217;re downloading).</li>
	</ul>
	<ul>
		<li><strong>Force</strong> will prevent it from asking if you want to overwrite files</li>
	</ul>

	<h4>ConvertFrom-Html</h4>

	<p>Converts <span class="caps">HTML</span> to <span class="caps">XML</span>. I&#8217;ll eventually tweak this so it supports passing it a file path and even saving the xml in-place on the original file &#8230; but for now you can use it on a stream of text, like this: <code>(Get-Content file.html | ConvertFrom-Html).save( &#34;file.xml&#34; )</code>.  Note that it outputs an xmldocument object &#8230; you can get the xml as text to the console using <code>Get_OuterXml()</code>.</p>

	<h4> ConvertTo-Hashtable</h4>

	<p>Converts a url-style query string or NameValueCollection into a Hashtable. It&#8217;s mainly to make it easier to use the Get-Web in cases where you&#8217;re getting the <span class="caps">URL</span> or <span class="caps">POST</span> data from elsewhere.</p>

	<p>Here&#8217;s a script with a few example uses of Get-Web: <span id="more-520"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Find the url of the image (download an html file as an xmldocument)</span><br />
<span style="color: #660033; font-weight: bold;">$ImgURL</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>fast<span style="color: #66cc66;">/</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">html</span>.<span style="color: #003366;">body</span>.<span style="color: #003366;">div</span>.<span style="color: #003366;">img</span>.<span style="color: #003366;">src</span><br />
<br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">Name</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;PoshConsole&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#In PoshConsole, show it inline</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ImgUrl</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">WPF</span></span> <span style="color: #000066;">-source</span> <span style="color: #009900;">&quot;&lt;Image Height='174' Source='{Binding Path=AbsoluteUri}' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' /&gt;&quot;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Use Get-Web to download an image (it's saved to file by default), and open it in my default viewer</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## When Get-Web saves a file, it outputs the [FileInfo] which can be invoked with &amp;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://dilbert.com$ImgURL&quot;</span> <span style="color: #000066;">-force</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Determine the language of a snippet of text (5 words minimum for best results)</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Language</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## This time, we're using Get-Web with POST parameters ...</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://www.xrce.xerox.com/cgi-bin/mltt/LanguageGuesser&quot;</span> <span style="color: #000066;">-Post</span> <span style="color: #333;">&#40;</span>@<span style="color: #333;">&#123;</span>Text<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$text</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;//font&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #009900;">&quot;#text&quot;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Translate text to English ... This is obviously reworkable as a general translation tool</span><br />
<span style="color: #666666; font-style: italic;">## But I don't have much use for that, since I only speak Spanish, English, and code ...</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">English</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$text</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$FromLanguage</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$FromLanguage</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$FromLanguage</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Language</span></span> <span style="color: #660033; font-weight: bold;">$text</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$post</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>text<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$text</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$FromLanguage</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Arabic&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ar|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Chinese&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;zh|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Dutch&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;nl|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;French&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;fr|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;German&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;de|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Greek&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;el|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Italian&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;it|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Japanese&quot;</span> &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ja|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Korean&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ko|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Portuguese&quot;</span> &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;pt|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Russian&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ru|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Spanish&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;es|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #009900;">&quot;Sorry, but I can't translate $FromLanguage&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Using Get-Web with POST parameters ... nothing remarkable, but it's just nice with the previous function ...</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://www.google.com/translate_t&quot;</span> <span style="color: #000066;">-Post</span> <span style="color: #660033; font-weight: bold;">$post</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;//div[@id='result_box']&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #009900;">&quot;#text&quot;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">####################################</span><br />
<span style="color: #666666; font-style: italic;">## Figure out the real url behind those shortened forms</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">URL</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$urls</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$snip</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?(?:snurl|snipr|snipurl)\.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$tiny</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?TinyURL.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$isgd</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?is.gd/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$twurl</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?twurl.nl/([^?/ ]*)\b&quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">switch</span> <span style="color: #000066;">-regex</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$urls</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$snip</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Notice that {{ Get-Web ... -plain }} returns plain text, not an xml doc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$snip</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://snipurl.com/resolveurl?id=$($snip.match( $_ ).groups[1].value)&quot;</span> <span style="color: #000066;">-plain</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$tiny</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## GET parameters this time ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$doc</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://tinyurl.com/preview.php&quot;</span> <span style="color: #000066;">-Get</span> @<span style="color: #333;">&#123;</span>num<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$tiny</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">match</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">groups</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">value</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$tiny</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #009900;">&quot;$($doc.SelectSingleNode(&quot;</span><span style="color: #009900;">&quot;//a[@id='redirecturl']&quot;</span><span style="color: #009900;">&quot;).href)&quot;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$isgd</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$doc</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://is.gd/$($isgd.match( $_ ).groups[1].value)-&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$isgd</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #009900;">&quot;$($doc.SelectSingleNode(&quot;</span><span style="color: #009900;">&quot;//div[@id='main']/p/a&quot;</span><span style="color: #009900;">&quot;).href)&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$twurl</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$doc</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://tweetburner.com/links/$($twurl.match( $_ ).groups[1].value)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$twurl</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #009900;">&quot;$($doc.selectsingleNode(&quot;</span><span style="color: #009900;">&quot;//div[@id='main-content']/p/a&quot;</span><span style="color: #009900;">&quot;).href)&quot;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WGet 2 for PowerShell</title>
		<link>http://huddledmasses.org/wget-2-for-powershell/</link>
		<comments>http://huddledmasses.org/wget-2-for-powershell/#comments</comments>
		<pubDate>Thu, 01 May 2008 17:00:26 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Get-Web]]></category>
		<category><![CDATA[HttpWebRequest]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[wGet]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/wget-2-for-powershell/</guid>
		<description><![CDATA[Edit: Added -Passthru Edit: I made a mistake &#8230; I wrote this a few weeks ago, and made an error in the script which causes it to not work properly with most binary files &#8230; I&#8217;ve fixed the script down below now, and while I was at it, I went ahead and added a download [...]]]></description>
			<content:encoded><![CDATA[	<h4> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Edit: Added -Passthru</h4>

	<h4>Edit: I made a mistake &#8230;
I wrote this a few weeks ago, and made an error in the script which causes it to not work properly with most binary files &#8230; I&#8217;ve fixed the script down below now, and while I was at it, I went ahead and added a download progress report  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </h4>

	<p>About a year ago, I wrote a script to <a href="http://huddledmasses.org/wget-for-powershell/">download files from the web</a> using PowerShell, but it was so simple that you had to specify the url to download <em>and</em> the file to save to.  At the time I knew there was a way to find the file name of the download in the header, but I couldn&#8217;t remember how to get to the headers, and it&#8217;s not possible using <a href="http://msdn2.microsoft.com/en-us/library/system.net.webclient.aspx">System.Net.WebClient</a> so I just dropped it.</p>

	<p>Then the other day I saw an old post from <a href="http://scriptolog.blogspot.com/2007/08/query-http-status-codes-and-headers.html">Script Fanatic</a> about querying Http status codes, and it reminded me of my annoyance with my wget script. So I <em>fixed</em> it, and I&#8217;m letting you have it.</p>

	<p>The new version of wget for PowerShell (or Get-WebFile) is on <a href="http://poshcode.org/417">the PowerShell Repository</a> and it&#8217;s been completely rewritten to use <a href="http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.aspx">System.Net.HttpWebRequest</a> and <a href="http://msdn2.microsoft.com/en-us/library/system.net.httpwebresponse.aspx">HttpWebResponse</a> which gives us access to the the name of the file in those cases where the <span class="caps">URL</span> is something like <a href="http://poshcode.org/?dl=417">.../scripts/?dl=417</a> (assuming the server puts the file name in the headers as it should).</p>

	<p>It still doesn&#8217;t support authentication, and even though the headers include the size (and I have to do the streams myself so I could easily read a few bytes at a time and give you progress reports) </p>

	<p><span id="more-511"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Get-WebFile (aka wget for PowerShell)</span><br />
<span style="color: #666666; font-style: italic;">##############################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Downloads a file or page from the web</span><br />
<span style="color: #666666; font-style: italic;">## History:</span><br />
<span style="color: #666666; font-style: italic;">## v3.6 - Add -Passthru switch to output TEXT files </span><br />
<span style="color: #666666; font-style: italic;">## v3.5 - Add -Quiet switch to turn off the progress reports ...</span><br />
<span style="color: #666666; font-style: italic;">## v3.4 - Add progress report for files which don't report size</span><br />
<span style="color: #666666; font-style: italic;">## v3.3 - Add progress report for files which report their size</span><br />
<span style="color: #666666; font-style: italic;">## v3.2 - Use the pure Stream object because StreamWriter is based on TextWriter:</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp;it was messing up binary files, and making mistakes with extended characters in text</span><br />
<span style="color: #666666; font-style: italic;">## v3.1 - Unwrap the filename when it has quotes around it</span><br />
<span style="color: #666666; font-style: italic;">## v3 &nbsp; - rewritten completely using HttpWebRequest + HttpWebResponse to figure out the file name, if possible</span><br />
<span style="color: #666666; font-style: italic;">## v2 &nbsp; - adds a ton of parsing to make the output pretty</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp; &nbsp;added measuring the scripts involved in the command, (uses Tokenizer)</span><br />
<span style="color: #666666; font-style: italic;">##############################################################################################################</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">WebFile</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;The URL to download&quot;</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Passthru</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$quiet</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$req</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Net</span>.<span style="color: #003366;">HttpWebRequest</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Create</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$res</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$req</span>.<span style="color: #003366;">GetResponse</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>;<br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #000066;">-and</span> <span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Location</span></span> <span style="color: #000066;">-PSProvider</span> <span style="color: #009900;">&quot;FileSystem&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #660033; font-weight: bold;">$fileName</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Passthru</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-or</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> <span style="color: #000066;">-PathType</span> <span style="color: #009900;">&quot;Container&quot;</span> <span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #009900;">'(?i)filename=(.*)$'</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Match</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">Headers</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;Content-Disposition&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Groups</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Value</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$fileName</span>.<span style="color: #003366;">trim</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\/&quot;</span><span style="color: #009900;">&quot;'&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">ResponseUri</span>.<span style="color: #003366;">Segments</span><span style="color: #333;">&#91;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$fileName</span>.<span style="color: #003366;">trim</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\/&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Please provide a file name&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$fileName</span>.<span style="color: #003366;">trim</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\/&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Extension</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">+</span> <span style="color: #009900;">&quot;.&quot;</span> <span style="color: #66cc66;">+</span> <span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">ContentType</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;;&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;/&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$fileName</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Location</span></span> <span style="color: #000066;">-PSProvider</span> <span style="color: #009900;">&quot;FileSystem&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #660033; font-weight: bold;">$fileName</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Passthru</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$encoding</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Text</span>.<span style="color: #003366;">Encoding</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetEncoding</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">CharacterSet</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">StatusCode</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">200</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$goal</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">ContentLength</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$reader</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">GetResponseStream</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$writer</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">IO</span>.<span style="color: #003366;">FileStream</span> <span style="color: #660033; font-weight: bold;">$fileName</span>, <span style="color: #009900;">&quot;Create&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$buffer</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> <span style="color: #003366; font-weight: bold;">byte</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span> <span style="color: #cc66cc;">4096</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$total</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$reader</span>.<span style="color: #003366;">Read</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$buffer</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #660033; font-weight: bold;">$buffer</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$writer</span>.<span style="color: #660033;">Write</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$buffer</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Passthru</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">+=</span> <span style="color: #660033; font-weight: bold;">$encoding</span>.<span style="color: #003366;">GetString</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$buffer</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$quiet</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$total</span> <span style="color: #66cc66;">+=</span> <span style="color: #660033; font-weight: bold;">$count</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$goal</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Progress</span></span> <span style="color: #009900;">&quot;Downloading $url&quot;</span> <span style="color: #009900;">&quot;Saving $total of $goal&quot;</span> <span style="color: #000066;">-id</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-percentComplete</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$total</span><span style="color: #66cc66;">/</span><span style="color: #660033; font-weight: bold;">$goal</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">100</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Progress</span></span> <span style="color: #009900;">&quot;Downloading $url&quot;</span> <span style="color: #009900;">&quot;Saving $total bytes...&quot;</span> <span style="color: #000066;">-id</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">while</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$count</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$reader</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$writer</span>.<span style="color: #003366;">Flush</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$writer</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Passthru</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$output</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$res</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$fileName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033;">ls</span> <span style="color: #660033; font-weight: bold;">$fileName</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wget-2-for-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>wget for PowerShell</title>
		<link>http://huddledmasses.org/wget-for-powershell/</link>
		<comments>http://huddledmasses.org/wget-for-powershell/#comments</comments>
		<pubDate>Tue, 24 Apr 2007 14:33:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Get-Web]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[wGet]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/wget-for-powershell/</guid>
		<description><![CDATA[Update: You should look at this other WGet for PowerShell script if what you want is to just download a file and have it work&#8230; I&#8217;m probably the last person on the planet to figure out how easy it is to get a basic wget working in PowerShell &#8230; but just in case I&#8217;m not [...]]]></description>
			<content:encoded><![CDATA[	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Update: You should look at this other <a href="http://huddledmasses.org/wget-2-for-powershell/">WGet for PowerShell</a> script if what you want is to just download a file and have it work&#8230;</p>

	<p>I&#8217;m probably the last person on the planet to figure out how easy it is to get a basic wget  working in PowerShell &#8230; but just in case I&#8217;m not &#8230; here&#8217;s a PowerShell script which will download web files to your hard drive.  </p>

	<p>That&#8217;s <strong>all</strong> it does.  This script doesn&#8217;t support authentication or anything (even though it does use your computer&#8217;s proxy settings), but I think it will be obvious how to extend it, since it&#8217;s basically just .Net Framework stuff.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$url</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$path</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #000066;">-parent</span> <span style="color: #660033; font-weight: bold;">$path</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-or</span> <span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> <span style="color: #000066;">-pathType</span> Container <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #000066;">-parent</span> <span style="color: #660033; font-weight: bold;">$path</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$path</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$pwd</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #000066;">-leaf</span> <span style="color: #660033; font-weight: bold;">$path</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #009900;">&quot;Downloading [$url]<span style="color: #000099; font-weight: bold;">`n</span>Saving at [$path]&quot;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$client</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Net</span>.<span style="color: #003366;">WebClient</span><br />
<span style="color: #660033; font-weight: bold;">$client</span>.<span style="color: #003366;">DownloadFile</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$url</span>, <span style="color: #660033; font-weight: bold;">$path</span> <span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>That&#8217;s all there is to it. Actually, you really only need the last two lines (and the first line), as long as you understand that the default path will be your user directory rather than the current directory. (like:  C:\Users\Joel OR C:\Documents and Settings\Joel\). That means that if you leave off that <code>if(!(Split-Path -parent $path))</code> stuff, you&#8217;ll always have to specify the file starting with a .\ if you want it saved in the folder you&#8217;re in (otherwise it gets dumped in your user directory).  </p>

	<p>Those three lines check if the specified path has a folder in it (and if it&#8217;s a valid folder) and if not, they just save the file to the local current folder. </p>

	<p>Here are some examples of how you&#8217;d use it, assuming you save that into a file called &#8220;wget.ps1&#8221; and put it in your path.  Incidentally, just like in &#8216;nix shells, ~ represents your user directory &#8230;  oh, and the generally more PowerShell-like name for the script would be &#8220;Get-Url.ps1&#8221; or something&#8230;</p>

	<p><span id="more-409"></span></p>

	<ul>
		<li>wget http://huddledmasses.org/downloads/RunOnlyOne.exe RunOnlyOne.exe</li>
		<li>wget http://huddledmasses.org/downloads/RunOnlyOne.exe .\RunOne.exe</li>
	</ul>
	<ul>
		<li>wget http://huddledmasses.org/downloads/RunOnlyOne.exe ~\RunOnly1.exe</li>
	</ul>

	<p>If anyone can tell me how to figure out what the file name should be when the <span class="caps">URL</span> ends with something like: .../download.php?file=361 I&#8217;ll post an amendment here that will let you skip specifying the file name.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wget-for-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

