<?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; HttpWebRequest</title>
	<atom:link href="http://huddledmasses.org/tag/httpwebrequest/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Tue, 31 Aug 2010 04:13:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<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[HttpWebRequest]]></category>
		<category><![CDATA[PowerShell]]></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/images/smilies/../../../wp-content/plugins/smilingmasses/new.gif' 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/images/smilies/../../../wp-content/plugins/smilingmasses/wink.gif' 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>
	</channel>
</rss>
