<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[gOxiA=苏繁=SuFan Blog]]></title> 
<link>https://sufan.maytide.net/index.php</link> 
<description><![CDATA[gOxiA,苏繁,sufan,Microsoft MVP]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[gOxiA=苏繁=SuFan Blog]]></copyright>
<item>
<link>https://sufan.maytide.net/read.php/1714.htm</link>
<title><![CDATA[[PS]使用 PowerShell 下载文件]]></title> 
<author>gOxiA &lt;sufan_cn@msn.com&gt;</author>
<category><![CDATA[PowerShell 和脚本]]></category>
<pubDate>Mon, 24 Feb 2014 05:05:47 +0000</pubDate> 
<guid>https://sufan.maytide.net/read.php/1714.htm</guid> 
<description>
<![CDATA[ 
	<p><a href="http://goxia.maytide.net/ftpupfiles/PS-PowerShell-_1055C/powershell_logo.png"><img title="powershell_logo" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="powershell_logo" src="http://goxia.maytide.net/ftpupfiles/PS-PowerShell-_1055C/powershell_logo_thumb.png" width="345" height="70"></a></p> <p><font color="#fd3f0d" size="4"><strong>使用 PowerShell 下载文件</strong></font></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;前段时间部署了一个基于 Windows Server 2012 R2 的 Server Core 环境，在实际使用中会遇到需要从网站上下载文件的问题，因为 Server Core 并未提供 IE，所以下载就不是那么方便了。还好在 PowerShell 下可以调用 System.Net.WebClient 来实现下载。即：</p> <p><div class="code">$client = New-Object “System.Net.WebClient”</div></p> <p><div class="code">$client.DownloadFile(“http://www.maytide.net/info.txt”,”D:&#92;info.txt”)</div></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;虽然上述的脚本实现了从一个 URL 下载文件到本地，但是实际调用起来会感觉很麻烦。因为每次都要输入很长的脚本命令才能完成一个下载。<a href="http://goxia.maytide.net/" target="_blank">gOxiA</a> 在编程方面也没什么天赋，实属一菜鸟，参考了以前的脚本写了一个粗糙的下载脚本，其功能是执行脚本后提示输入 URL 的地址，然后再输入本地存储的文件路径，最后执行下载，并根据结果判断是否下载完毕。</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;脚本中涉及了一些基本的命令代码，其中 Write-Host 用于输出提示文本；Read-Host 用于记录用户输入的数据；调用下载、下载地址、本地存储都使用变量，最后用 Test-Path 对下载的文件进行检查，并使用 if...else 进行判断。下面就是整个脚本的内容：</p> <p><em><font color="#00ff00">#清屏<br/></font></em>Clear<br/><em><font color="#00ff00">#脚本基本信息提示</font></em><br/>Write-Host<br/>Write-Host PowerShell Download Tools Script.<br/>Write-Host<br/><em><font color="#00ff00">#输入下载资源地址，赋予变量 $url</font></em><br/>Write-Host Please enter URL address of download sources! -ForegroundColor Red<br/>$url= Read-Host "Enter the URL..."<br/>Write-Host<br/><em><font color="#00ff00">#输入文件存储路径，赋予变量 $local<br/></font></em>Write-Host Please enter the local store path! -ForegroundColor Green<br/>$local= Read-Host "Enter the store path..."<br/><em><font color="#00ff00">#调用下载，赋予变量 $dl<br/></font></em>$dl= New-Object "System.Net.WebClient"<br/><em><font color="#00ff00">#执行下载</font></em><br/>Write-Host<br/>Write-Host Downloading... Please wait!!!<br/>$dl.DownloadFile("$url","$local")<br/>Write-Host<br/><em><font color="#00ff00">#将路径检查命令赋予变量 $result</font></em><br/>$result=Test-Path $local<br/><em><font color="#00ff00">#对 $result 结果进行判断，如果是 True 表示下载完成，否则失败</font></em><br/>if($result -eq $True)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Write-Host Result status: Completed!!! -ForegroundColor Green<br/>&#125;<br/>else<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Write-Host Result status: Failed!!! -ForegroundColor Red<br/>&#125;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行效果如下图：</p> <p><a href="http://goxia.maytide.net/ftpupfiles/PS-PowerShell-_1055C/image.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://goxia.maytide.net/ftpupfiles/PS-PowerShell-_1055C/image_thumb.png" width="634" height="376"></a></p><br/>Tags - <a href="https://sufan.maytide.net/go.php/tags/microsoft/" rel="tag">microsoft</a> , <a href="https://sufan.maytide.net/go.php/tags/windows/" rel="tag">windows</a> , <a href="https://sufan.maytide.net/go.php/tags/server/" rel="tag">server</a> , <a href="https://sufan.maytide.net/go.php/tags/2012/" rel="tag">2012</a> , <a href="https://sufan.maytide.net/go.php/tags/r2/" rel="tag">r2</a> , <a href="https://sufan.maytide.net/go.php/tags/servercore/" rel="tag">servercore</a> , <a href="https://sufan.maytide.net/go.php/tags/powershell/" rel="tag">powershell</a> , <a href="https://sufan.maytide.net/go.php/tags/download/" rel="tag">download</a>
]]>
</description>
</item><item>
<link>https://sufan.maytide.net/read.php/1714.htm#blogcomment4897</link>
<title><![CDATA[[评论] [PS]使用 PowerShell 下载文件]]></title> 
<author>九叔 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 19 Mar 2014 12:11:24 +0000</pubDate> 
<guid>https://sufan.maytide.net/read.php/1714.htm#blogcomment4897</guid> 
<description>
<![CDATA[ 
	苏总，你着相了。其实脚本可以更简单一些，代码保存为aa.ps1$client = New-Object “System.Net.WebClient”$xiazaidizhi = $args[0]$bendilujing = $args[1]$client.DownloadFile(“$xiazaidizhi”,”$bendilujing”)用户在cmd下执行“PowerShell .\aa.ps1&nbsp;&nbsp; 下载地址&nbsp;&nbsp;本地地址”就好了，$args可以传递输入参数。如果是PowerShell下，就是&nbsp;&nbsp;“.\aa.ps1&nbsp;&nbsp; 下载地址&nbsp;&nbsp;本地地址”另外脚本也可以改下，比如帮用户输“http://”以及本地路径前面那部分，最终打字可以更少一些。
]]>
</description>
</item><item>
<link>https://sufan.maytide.net/read.php/1714.htm#blogcomment4900</link>
<title><![CDATA[[评论] [PS]使用 PowerShell 下载文件]]></title> 
<author>gOxiA &lt;goxia@live.cn&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 22 Mar 2014 03:08:43 +0000</pubDate> 
<guid>https://sufan.maytide.net/read.php/1714.htm#blogcomment4900</guid> 
<description>
<![CDATA[ 
	to:九叔，精辟！学习了
]]>
</description>
</item>
</channel>
</rss>