{"id":33524,"date":"2013-10-09T17:01:33","date_gmt":"2013-10-09T16:01:33","guid":{"rendered":"http:\/\/www.fuhaijun.com\/?p=33524"},"modified":"2024-11-06T11:18:46","modified_gmt":"2024-11-06T03:18:46","slug":"powershell%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e4%b8%8b%e8%bd%bd%e7%b1%bbwget","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33524","title":{"rendered":"PowerShell\u5b9e\u73b0\u6587\u4ef6\u4e0b\u8f7d(\u7c7bwget)"},"content":{"rendered":"<p>\u5bf9Linux\u719f\u6089\u7684\u8bfb\u8005\u53ef\u80fd\u4f1a\u5bf9Linux\u901a\u8fc7wget\u4e0b\u8f7d\u6587\u4ef6\u6709\u5370\u8c61\uff0c\u8fd9\u4e2a\u5de5\u5177\u529f\u80fd\u5f88\u5f3a\u5927\uff0c\u5728.NET\u73af\u5883\u4e0b\u63d0\u5230\u4e0b\u8f7d\u6587\u4ef6\u5927\u591a\u6570\u4eba\u719f\u6089\u7684\u662f\u901a\u8fc7<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.net.webclient.aspx\">System.Net.WebClient<\/a>\u8fdb\u884c\u4e0b\u8f7d\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u96c6\u80fd\u5b9e\u73b0\u4e0b\u8f7d\u7684\u529f\u80fd\uff0c\u4f46\u662f\u6709\u7f3a\u9677\uff0c\u5982\u679c\u78b0\u4e0a\u7c7b\u4f3c\u4e8e&#8230;\/scripts\/?dl=417\u8fd9\u7c7b\u7684\u4e0b\u8f7d\u94fe\u63a5\u5c06\u65e0\u6cd5\u6b63\u786e\u8bc6\u522b\u6587\u4ef6\u540d\uff0c\u4e0b\u8f7d\u7684\u6587\u4ef6\u901a\u5e38\u4f1a\u88ab\u547d\u540d\u4e3adl=417\u8fd9\u6837\u53e4\u602a\u7684\u540d\u5b57\uff0c\u5176\u5b9e\u5bf9\u5e94\u7684\u6587\u4ef6\u540d\u662f\u5728\u8bbf\u95ee\u8fd9\u4e2a\u94fe\u63a5\u8fd4\u56de\u7ed3\u679c\u7684HTTP\u5934\u4e2d\u5305\u542b\u7684\u3002\u4e8b\u5b9e\u4e0a\u5fae\u8f6f\u4e5f\u63d0\u4f9b\u4e86\u907f\u514d\u8fd9\u4e9b\u7f3a\u9677\u7684\u7a0b\u5e8f\u96c6<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.net.httpwebrequest.aspx\">System.Net.HttpWebRequest<\/a> \u548c <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.net.httpwebresponse.aspx\">HttpWebResponse<\/a>\uff0c\u672c\u6587\u5c06\u4f1a\u4f7f\u7528\u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u96c6\u6765\u5b9e\u73b0PowerShell\u7248wget\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4ee3\u7801\u4e0d\u600e\u4e48\u590d\u6742\uff0c\u57fa\u672c\u4e0a\u5c31\u662f\u521b\u5efaHttpWebRequest\u5bf9\u8c61\uff0c\u8bbe\u5b9aUserAgent\u548cCookieContainer\u4ee5\u514d\u5728\u9047\u5230\u8bbe\u7f6e\u9632\u76d7\u94fe\u7684\u670d\u52a1\u5668\u51fa\u73b0\u65e0\u6cd5\u4e0b\u8f7d\u7684\u60c5\u51b5\u3002\u7136\u540e\u901a\u8fc7HttpWebRequest\u5bf9\u8c61\u7684GetResponse()\u65b9\u6cd5\u4ecehttp\u5934\u4e2d\u83b7\u53d6\u76ee\u6807\u6587\u4ef6\u7684\u5927\u5c0f\u4ee5\u53ca\u6587\u4ef6\u540d\uff0c\u4ee5\u4fbf\u80fd\u5728\u4e0b\u8f7d\u5230\u6587\u4ef6\u65f6\u63d0\u793a\u5f53\u524d\u4e0b\u8f7d\u8fdb\u5ea6\uff0c\u5728\u4e0b\u8f7d\u5b8c\u6587\u4ef6\u540e\uff0c\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u5bf9\u5e94\u7684\u6587\u4ef6\u3002\u4ee3\u7801\u4e0d\u590d\u6742\uff0c\u6709\u4efb\u4f55\u7591\u95ee\u7684\u8bfb\u8005\u53ef\u4ee5\u7559\u8a00\u7ed9\u6211\uff0c\u8fdb\u884c\u4ea4\u6d41\uff0c\u4e0b\u9762\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre style=\"width: 150.69%; font: 9pt verdana, fixedsys, verdana, tahoma; height: 2126px\"><span class=\"line_num\">        =====\u6587\u4ef6\u540d\uff1aGet-WebFile.ps1=====<\/span>\n<font color=\"#0000ff\">function<\/font> Get-WebFile {\n&lt;<font color=\"#008000\"># Author:fuhj(powershell#live.cn ,http:\/\/fuhaijun.com) <\/font>\n   Downloads a file or page from the web\n.Example\n  Get-WebFile http:\/\/mirrors.cnnic.cn\/apache\/couchdb\/binary\/win\/1.4.0\/setup-couchdb-1.4.0_R16B01.exe\n  Downloads the latest version of this file to the current directory\n<font color=\"#008000\">#&gt;<\/font>\n\n[CmdletBinding(DefaultParameterSetName=<font color=\"#ff00ff\">&quot;NoCredentials&quot;<\/font>)]\n   <font color=\"#0000ff\">param<\/font>(\n<font color=\"#008000\">      #  The URL of the file\/page to download<\/font>\n      [Parameter(Mandatory=$true,Position=0)]\n      [System.Uri][Alias(<font color=\"#ff00ff\">&quot;Url&quot;<\/font>)]$Uri <font color=\"#008000\"># = (Read-Host &quot;The URL to download&quot;)<\/font>\n   ,\n<font color=\"#008000\">      #  A Path to save the downloaded content. <\/font>\n      [<font color=\"#0000ff\">string<\/font>]$FileName\n   ,\n<font color=\"#008000\">      #  Leave the file unblocked instead of blocked<\/font>\n      [<font color=\"#0000ff\">Switch<\/font>]$Unblocked\n   ,\n<font color=\"#008000\">      #  Rather than saving the downloaded content to a file, output it.  <\/font>\n<font color=\"#008000\">      #  This is for text documents like web pages and rss feeds, and allows you to avoid temporarily caching the text in a file.<\/font>\n      [<font color=\"#0000ff\">switch<\/font>]$Passthru\n   ,\n<font color=\"#008000\">      #  Supresses the Write-Progress during download<\/font>\n      [<font color=\"#0000ff\">switch<\/font>]$Quiet\n   ,\n<font color=\"#008000\">      #  The name of a variable to store the session (cookies) in<\/font>\n      [<font color=\"#0000ff\">String<\/font>]$SessionVariableName\n   ,\n<font color=\"#008000\">      #  Text to include at the front of the UserAgent string<\/font>\n      [<font color=\"#0000ff\">string<\/font>]$UserAgent = <font color=\"#ff00ff\">&quot;PowerShellWget\/$(1.0)&quot;<\/font>      \n   )\n\n   <font color=\"#008080\">Write-Verbose<\/font> &quot;Downloading <font color=\"#0000ff\">&amp;<\/font><font color=\"#008000\">#39;$Uri'&quot;<\/font>\n   $EAP,$ErrorActionPreference = $ErrorActionPreference, <font color=\"#ff00ff\">&quot;Stop&quot;<\/font>\n   $request = [System.Net.HttpWebRequest]::Create($Uri);\n   $ErrorActionPreference = $EAP   \n   $request.UserAgent = $(\n         <font color=\"#ff00ff\">&quot;{0} (PowerShell {1}; .NET CLR {2}; {3}; http:\/\/fuhaijun.com)&quot;<\/font> <font color=\"#0000ff\">-f<\/font> $UserAgent, \n         $(<font color=\"#0000ff\">if<\/font>($Host.Version){$Host.Version}<font color=\"#0000ff\">else<\/font>{<font color=\"#ff00ff\">&quot;1.0&quot;<\/font>}),\n         [Environment]::Version,\n         [Environment]::OSVersion.ToString().Replace(<font color=\"#ff00ff\">&quot;Microsoft Windows &quot;<\/font>, <font color=\"#ff00ff\">&quot;Win&quot;<\/font>)\n      )\n\n   $Cookies = <font color=\"#008080\">New-Object<\/font> System.Net.CookieContainer\n   <font color=\"#0000ff\">if<\/font>($SessionVariableName) {\n      $Cookies = <font color=\"#008080\">Get-Variable<\/font> $SessionVariableName -Scope 1 \n   }\n   $request.CookieContainer = $Cookies\n   <font color=\"#0000ff\">if<\/font>($SessionVariableName) {\n      <font color=\"#008080\">Set-Variable<\/font> $SessionVariableName -Scope 1 -Value $Cookies\n   }\n\n   <font color=\"#0000ff\">try<\/font> {\n      $res = $request.GetResponse();\n   } <font color=\"#0000ff\">catch<\/font> [System.Net.WebException] { \n      <font color=\"#008080\">Write-Error<\/font> $_.Exception -Category ResourceUnavailable\n      <font color=\"#0000ff\">return<\/font>\n   } <font color=\"#0000ff\">catch<\/font> {\n      <font color=\"#008080\">Write-Error<\/font> $_.Exception -Category NotImplemented\n      <font color=\"#0000ff\">return<\/font>\n   }\n\n   <font color=\"#0000ff\">if<\/font>((<font color=\"#008080\">Test-Path<\/font> variable:res) <font color=\"#0000ff\">-and<\/font> $res.StatusCode <font color=\"#0000ff\">-eq<\/font> 200) {\n      <font color=\"#0000ff\">if<\/font>($fileName <font color=\"#0000ff\">-and<\/font> !(<font color=\"#008080\">Split-Path<\/font> $fileName)) {\n         $fileName = <font color=\"#008080\">Join-Path<\/font> (<font color=\"#008080\">Convert-Path<\/font> (<font color=\"#008080\">Get-Location<\/font> -PSProvider <font color=\"#ff00ff\">&quot;FileSystem&quot;<\/font>)) $fileName\n      }\n      <font color=\"#0000ff\">elseif<\/font>((!$Passthru <font color=\"#0000ff\">-and<\/font> !$fileName) <font color=\"#0000ff\">-or<\/font> ($fileName <font color=\"#0000ff\">-and<\/font> (<font color=\"#008080\">Test-Path<\/font> -PathType <font color=\"#ff00ff\">&quot;Container&quot;<\/font> $fileName)))\n      {\n         [<font color=\"#0000ff\">string<\/font>]$fileName = ([regex]'&amp;<font color=\"#008000\">#40;?i)filename=(.*)$').Match( $res.Headers[&quot;Content-Disposition&quot;] ).Groups[1].Value<\/font>\n         $fileName = $fileName.trim(&quot;&amp;<font color=\"#008000\">#92;\/&quot;&quot;'&quot;)<\/font>\n\n         $ofs = <font color=\"#ff00ff\">&quot;&quot;<\/font>\n         $fileName = [Regex]::Replace($fileName, <font color=\"#ff00ff\">&quot;[$([Regex]::Escape(&quot;<\/font><font color=\"#ff00ff\">&quot;$([System.IO.Path]::GetInvalidPathChars())$([IO.Path]::AltDirectorySeparatorChar)$([IO.Path]::DirectorySeparatorChar)&quot;<\/font><font color=\"#ff00ff\">&quot;))]&quot;<\/font>, <font color=\"#ff00ff\">&quot;_&quot;<\/font>)\n         $ofs = <font color=\"#ff00ff\">&quot; &quot;<\/font>\n\n         <font color=\"#0000ff\">if<\/font>(!$fileName) {\n            $fileName = $res.ResponseUri.Segments[-1]\n            $fileName = $fileName.trim(<font color=\"#ff00ff\">&quot;\\\/&quot;<\/font>)\n            <font color=\"#0000ff\">if<\/font>(!$fileName) { \n               $fileName = <font color=\"#008080\">Read-Host<\/font> <font color=\"#ff00ff\">&quot;Please provide a file name&quot;<\/font>\n            }\n            $fileName = $fileName.trim(<font color=\"#ff00ff\">&quot;\\\/&quot;<\/font>)\n            <font color=\"#0000ff\">if<\/font>(!([IO.FileInfo]$fileName).Extension) {\n               $fileName = $fileName + <font color=\"#ff00ff\">&quot;.&quot;<\/font> + $res.ContentType.Split(<font color=\"#ff00ff\">&quot;;&quot;<\/font>)[0].Split(<font color=\"#ff00ff\">&quot;\/&quot;<\/font>)[1]\n            }\n         }\n         $fileName = <font color=\"#008080\">Join-Path<\/font> (<font color=\"#008080\">Convert-Path<\/font> (<font color=\"#008080\">Get-Location<\/font> -PSProvider <font color=\"#ff00ff\">&quot;FileSystem&quot;<\/font>)) $fileName\n      }\n      <font color=\"#0000ff\">if<\/font>($Passthru) {\n         $encoding = [System.Text.Encoding]::GetEncoding( $res.CharacterSet )\n         [<font color=\"#0000ff\">string<\/font>]$output = <font color=\"#ff00ff\">&quot;&quot;<\/font>\n      }\n\n      [<font color=\"#0000ff\">int<\/font>]$goal = $res.ContentLength\n      $reader = $res.GetResponseStream()\n      <font color=\"#0000ff\">if<\/font>($fileName) {\n         <font color=\"#0000ff\">try<\/font> {\n            $writer = <font color=\"#008080\">new-object<\/font> System.IO.FileStream $fileName, <font color=\"#ff00ff\">&quot;Create&quot;<\/font>\n         } <font color=\"#0000ff\">catch<\/font> {\n            <font color=\"#008080\">Write-Error<\/font> $_.Exception -Category WriteError\n            <font color=\"#0000ff\">return<\/font>\n         }\n      }\n      [byte[]]$buffer = <font color=\"#008080\">new-object<\/font> byte[] 4096\n      [<font color=\"#0000ff\">int<\/font>]$total = [<font color=\"#0000ff\">int<\/font>]$count = 0\n      <font color=\"#0000ff\">do<\/font>\n      {\n         $count = $reader.Read($buffer, 0, $buffer.Length);\n         <font color=\"#0000ff\">if<\/font>($fileName) {\n            $writer.<font color=\"#008080\">Write<\/font>($buffer, 0, $count);\n         } \n         <font color=\"#0000ff\">if<\/font>($Passthru){\n            $output += $encoding.GetString($buffer,0,$count)\n         } <font color=\"#0000ff\">elseif<\/font>(!$quiet) {\n            $total += $count\n            <font color=\"#0000ff\">if<\/font>($goal <font color=\"#0000ff\">-gt<\/font> 0) {\n               <font color=\"#008080\">Write-Progress<\/font> <font color=\"#ff00ff\">&quot;Downloading $Uri&quot;<\/font> <font color=\"#ff00ff\">&quot;Saving $total of $goal&quot;<\/font> -id 0 -percentComplete (($total\/$goal)*100)\n            } <font color=\"#0000ff\">else<\/font> {\n               <font color=\"#008080\">Write-Progress<\/font> <font color=\"#ff00ff\">&quot;Downloading $Uri&quot;<\/font> <font color=\"#ff00ff\">&quot;Saving $total bytes...&quot;<\/font> -id 0\n            }\n         }\n      } <font color=\"#0000ff\">while<\/font> ($count <font color=\"#0000ff\">-gt<\/font> 0)\n\n      $reader.Close()\n      <font color=\"#0000ff\">if<\/font>($fileName) {\n         $writer.Flush()\n         $writer.Close()\n      }\n      <font color=\"#0000ff\">if<\/font>($Passthru){\n         $output\n      }\n   }\n   <font color=\"#0000ff\">if<\/font>(<font color=\"#008080\">Test-Path<\/font> variable:res) { $res.Close(); }\n   <font color=\"#0000ff\">if<\/font>($fileName) {\n      <font color=\"#008080\">ls<\/font> $fileName\n   }\n}<\/pre>\n<p>\u8c03\u7528\u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>Get-WebFile <a href=\"http:\/\/mirrors.cnnic.cn\/apache\/couchdb\/binary\/win\/1.4.0\/setup-couchdb-1.4.0_R16B01.exe\">http:\/\/mirrors.cnnic.cn\/apache\/couchdb\/binary\/win\/1.4.0\/setup-couchdb-1.4.0_R16B01.exe<\/a><\/p>\n<p>\u8fd9\u91cc\u4e0b\u8f7dcouchdb\u7684\u6700\u65b0windows\u5b89\u88c5\u5305\u3002<\/p>\n<p>\u6267\u884c\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2013\/10\/image7.png\" width=\"673\" height=\"442\" \/> <\/p>\n<p>\u80fd\u591f\u770b\u5230\u5728\u4e0b\u8f7d\u6587\u4ef6\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u663e\u793a\u5f53\u524d\u5df2\u4e0b\u8f7d\u6570\u548c\u603b\u7684\u6587\u4ef6\u5927\u5c0f\uff0c\u5e76\u4e14\u6709\u8fdb\u5ea6\u6761\u663e\u793a\u5f53\u524d\u4e0b\u8f7d\u7684\u8fdb\u5ea6\uff0c\u8ddfwget\u770b\u8d77\u6765\u662f\u6709\u4e9b\u795e\u4f3c\u4e86\u3002\u4e0b\u8f7d\u5b8c\u6bd5\u540e\u4f1a\u663e\u793a\u5df2\u7ecf\u4e0b\u8f7d\u6587\u4ef6\u7684\u60c5\u51b5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2013\/10\/image8.png\" width=\"669\" height=\"278\" \/><\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b<br \/>\n  <br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709<\/p>\n<p>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22<\/p>\n<p>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\u4e14\u4fdd\u8bc1\u5185\u5bb9\u5b8c\u6574!\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb!<\/p>\n<p>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/www.fuhaijun.com\/\">http:\/\/www.fuhaijun.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9Linux\u719f\u6089\u7684\u8bfb\u8005\u53ef\u80fd\u4f1a\u5bf9Linux\u901a\u8fc7wget\u4e0b\u8f7d\u6587\u4ef6\u6709\u5370\u8c61\uff0c\u8fd9\u4e2a\u5de5\u5177\u529f\u80fd\u5f88\u5f3a\u5927\uff0c\u5728.NET\u73af\u5883\u4e0b\u63d0\u5230\u4e0b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,1],"tags":[],"class_list":["post-33524","post","type-post","status-publish","format-standard","hentry","category-powershell","category-default"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33524"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33524"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33524\/revisions"}],"predecessor-version":[{"id":33735,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33524\/revisions\/33735"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}