使用 PowerShell 下载 URL 内容 [英] Download URL content using PowerShell
问题描述
我正在使用脚本,我可以在其中浏览网页内容或网址",但无法复制其中的网页内容 &作为文件下载.这是我到目前为止所做的:
$url = "http://sp-fin/sites/arindam-sites/_layouts/xlviewer.aspx?listguid={05DA1D91-F934-4419-8AEF-B297DB81A31D}&itemid=4&DefaultItemOpen=1"$ie=new-object -com internetexplorer.application$ie.visible=$true$ie.navigate($url)而($ie.busy){开始睡眠1}
如何将$url
的内容复制并保存到本地驱动器作为文件?
更新:
我收到了这些错误:
<块引用>调用DownloadFile"的异常与2"参数:远程服务器返回错误:(401)未经授权."在 :line:6 char:47 + (New-Object system.net.webclient).DownloadFile( <<<< "$url/download-url-content", 'save.html' )>
方法调用中缺少)".在 :line:6 char:68 + (New-Object system.net.webclient).DownloadFile( "$url", 'save.html' <<<<
调用DownloadFile"的异常与2"参数:远程服务器返回错误:(401)未经授权."在 :line:6 char:47 + (New-Object system.net.webclient).DownloadFile( <<<< "$url", 'save.html' )
好的,让我解释一下我正在尝试做的事情:我在我们的共享点站点上有一个 excel 文件 &这是我尝试在本地下载的文件(任何格式),它是脚本的一部分,因此对于脚本的后面部分,我可以将此文件与其他数据进行比较 &得到一个输出.
现在,如果我能以某种方式映射我的文档"从网站 &能够下载文件,这也适用于我.
2014 年 1 月更新:使用随 Windows 8 一起发布的 Powershell v3,您可以执行以下操作:
(Invoke-webrequest -URI "http://www.kernel.org").Content
原始帖子,适用于 Powershell 版本 2
此解决方案与 stej、Jay Bazusi 和 Marco Shaw 的其他答案非常相似.它更通用,通过将新模块安装到模块目录 psurl 中.模块 psurl 添加新命令,以防您必须使用 powershell 执行大量 html 获取(和 POST).
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") |交换器
查看代码共享网站http://psget.net/的主页.
<块引用>这行漂亮的 PowerShell 脚本将下载 GetPsGet.ps1 并发送调用 Invoke-Expression 来安装 PsGet 模块.
然后安装 PsUrl,一个受 curl 启发的 Powershell 模块:
要从中央目录安装一些东西(在我们的例子中是 PsUrl),只需输入:
install-module PsUrl获取模块 -name psurl
输出:
ModuleType Name ExportedCommands---------- ---- ----------------脚本 psurl {Get-Url, Send-WebContent, Write-Url, Get-WebContent}
命令:
get-command -module psurl
输出:
CommandType 名称定义——————————————————函数 Get-Url ...函数 Get-WebContent ...别名 gwc Get-WebContent函数发送-WebContent ...别名 swc Send-WebContent函数写-Url ...
您只需执行此操作一次.
请注意,可能会出现此错误:
问:错误无法加载文件xxx,因为在此系统上禁用了脚本的执行.有关详细信息,请参阅get-help about_signing"."
A:默认情况下,PowerShell 限制所有脚本的执行.这都是关于安全的.要修复"此问题,请以管理员身份运行 PowerShell 并调用
Set-ExecutionPolicy RemoteSigned
从现在开始,在您的新 powershell 会话/脚本中,执行以下操作:
import-module psurlget-url "http://www.google.com"
要下载并保存到文件,请执行以下操作:
get-url "http://www.google.com" |out-file -filepath "myfile.html"
I am working in a script, where I am able to browse the web content or the 'url' but I am not able to copy the web content in it & download as a file. This is what I have made so far:
$url = "http://sp-fin/sites/arindam-sites/_layouts/xlviewer.aspx?listguid={05DA1D91-F934-4419-8AEF-B297DB81A31D}&itemid=4&DefaultItemOpen=1"
$ie=new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate($url)
while($ie.busy) {start-sleep 1}
How can I copy the content of $url
and save it to local drive as a file?
Update:
I got these errors:
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized." At :line:6 char:47 + (New-Object system.net.webclient).DownloadFile( <<<< "$url/download-url-content", 'save.html' )
Missing ')' in method call. At :line:6 char:68 + (New-Object system.net.webclient).DownloadFile( "$url", 'save.html' <<<<
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized." At :line:6 char:47 + (New-Object system.net.webclient).DownloadFile( <<<< "$url", 'save.html' )
Ok, let me explain more, on what I am trying to do: I have a excel file in our share point site & this is the file I am trying to download locally(any format), which is a part of the script, so that for the later part of the script, I can compare this file with other data & get an output.
Now if I can somehow map "my documents" from the site & able to download the file, that will also work for me.
Update Jan 2014: With Powershell v3, released with Windows 8, you can do this:
(Invoke-webrequest -URI "http://www.kernel.org").Content
Original Post, valid for Powershell Version 2
This solution is very similar to the other answers from stej, Jay Bazusi and Marco Shaw. It is a bit more general, by installing a new module into your module directory, psurl. The module psurl adds new commands in case you have to do a lot of html-fetching (and POSTing) with powershell.
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
See the homepage of the code-sharing website http://psget.net/.
This nice line of PowerShell script will dowload GetPsGet.ps1 and send it to Invoke-Expression to install PsGet Module.
Then install PsUrl, a Powershell Module inspired by curl:
To install something (in our case PsUrl) from central directory just type:
install-module PsUrl
get-module -name psurl
Output:
ModuleType Name ExportedCommands
---------- ---- ----------------
Script psurl {Get-Url, Send-WebContent, Write-Url, Get-WebContent}
Command:
get-command -module psurl
Output:
CommandType Name Definition
----------- ---- ----------
Function Get-Url ...
Function Get-WebContent ...
Alias gwc Get-WebContent
Function Send-WebContent ...
Alias swc Send-WebContent
Function Write-Url ...
You need to do this only once.
Note that this error might occur:
Q: Error "File xxx cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details."
A: By default, PowerShell restricts execution of all scripts. This is all about security. To "fix" this run PowerShell as Administrator and call
Set-ExecutionPolicy RemoteSigned
From now on, in your new powershell sessions/scripts, do this:
import-module psurl
get-url "http://www.google.com"
To download and save to a file, do this:
get-url "http://www.google.com" | out-file -filepath "myfile.html"
这篇关于使用 PowerShell 下载 URL 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!