简体   繁体   English

在Azure VM上使用Powershell不稳定文件下载

[英]Choppy File Download using Powershell on Azure VM

I'm noticing some strange behavior on an Azure Virtual Machine when downloading a file using the Invoke-WebRequest command. 使用Invoke-WebRequest命令下载文件时,我注意到Azure虚拟机上有一些奇怪的行为。 Seems like the download stream is super choppy. 好像下载流超级震撼。 Normally, Azure VMs download files super fast, so not sure what's causing this. 通常,Azure VM超级快地下载文件,所以不确定是什么原因造成的。 The file lives in azure blob storage in the same region as the VM. 该文件位于与VM相同区域的Azure Blob存储中。 When I download the file via web browser, it only takes 3 seconds. 当我通过网络浏览器下载文件时,只需要3秒钟。 Using powershell takes about a minute! 使用Powershell大约需要一分钟! 在此处输入图片说明

Here is a screen capture of the network when done using powershell. 这是使用powershell完成后的网络屏幕截图。

To clarify, this is the code I'm using to download a file... 为了澄清,这是我用来下载文件的代码...

$dest = "$($buildDir)\MyStuff.zip"
Invoke-WebRequest "https://mystorage.blob.core.windows.net/apps/$($using:buildNumber)/App/MyStuff.zip" -OutFile $dest

I'm using whatever version of powershell comes on the 2016-Datacenter image in the azure gallery. 我正在使用azure画廊中2016-Datacenter映像上随附的任何版本的Powershell。

Yes, PowerShell cmdlet Invoke-WebRequest download files slow than web browser, because the progress reporting every byte, the overhead is high than others(web browser). 是的,PowerShell cmdlet Invoke-WebRequest下载文件的速度比Web浏览器慢,因为报告每个字节的进度都比其他人(Web浏览器)高。

More information about speed of Invoke-WebRequest , please refer to the answer of @jasongin . 有关Invoke-WebRequest速度的更多信息,请参考@jasongin答案

If you want to download file via PowerShell, maybe we can use WebClient Class , that will fast than WebRequest Class , we can use command like this: 如果您想通过PowerShell下载文件,也许我们可以使用WebClient Class ,它比WebRequest Class快,我们可以使用如下命令:

$download = New-Object net.webclient  
$download.Downloadfile($source_url, $local_url)

Hope this helps. 希望这可以帮助。

You do not show your code on how you are using Invoke-WebRequest or if you are using 32 vs 64bit. 您不会显示有关如何使用Invoke-WebRequest或使用32位还是64位的代码。

From a similar post with an accepted answer of... 来自类似的帖子,答案是...

"The issue is that he's piping COM objects into another cmdlet – in this case, Select-Object. When that happens, we attempt to bind parameters by property name. Enumerating property names of a COM object is brutally slow – so we're spending 86% of our time on two very basic CLR API calls: “问题在于,他正在将COM对象传递到另一个cmdlet中(在本例中为Select-Object。),发生这种情况时,我们尝试按属性名称绑定参数。枚举COM对象的属性名称的速度非常慢,因此我们花了很多钱。我们有86%的时间在两个非常基本的CLR API调用上:

(…) // Get the function description from a COM type typeinfo.GetFuncDesc(index, out pFuncDesc); (…)//从COM类型typeinfo.GetFuncDesc(index,out pFuncDesc);获取函数描述 (…) // Get the function name from a COM function description typeinfo.GetDocumentation(funcdesc.memid, out strName, out strDoc, out id, out strHelp); (…)//从COM函数描述中获取函数名称typeinfo.GetDocumentation(funcdesc.memid,out strName,out strDoc,out id,out strHelp); (…) (......)

We might be able to do something smart here with caching. 我们也许可以通过缓存在这里做一些聪明的事情。

A workaround is to not pipe into Select-Object, but instead use language features: Grab the rows from the table, skipping the first row (column headers) 一种解决方法是不通过管道传递到Select-Object,而是使用语言功能:抓住表中的行,跳过第一行(列标题)

$allRows = @($slotTable.getElementsByTagName("tr"))
$rows = $allRows[1..$allRows.Count]

Why is this PowerShell code (Invoke-WebRequest / getElementsByTagName) so incredibly slow on my machines, but not others? 为什么此PowerShell代码(Invoke-WebRequest / getElementsByTagName)在我的计算机上如此缓慢,而在其他计算机上却如此缓慢?

But in general Invoke-WebRequest in 5.1 and below is slow. 但总体而言,5.1及以下版本中的Invoke-WebRequest速度较慢。 In PoSHv6(Core) it is way faster. 在PoSHv6(Core)中,速度更快。 Try is for yourself and see. 尝试是自己的,看看。

There are several articles on line the specifically talk to how slow Invoke-WebRequest can be. 在线上有几篇文章专门讨论Invoke-WebRequest的速度有多慢。

Invoke-WebRequest speed seems slow compare to Windows version but is not https://github.com/PowerShell/PowerShell/issues/5284 https://github.com/PowerShell/PowerShell/issues/2656 https://www.chasewright.com/invoke-webrequest-vs-system-net-webclient-download-speed 与Windows版本相比,Invoke-WebRequest速度似乎较慢,但不是https://github.com/PowerShell/PowerShell/issues/5284 https://github.com/PowerShell/PowerShell/issues/2656 https://www.chasewright .COM /调用-的WebRequest-VS-系统净Web客户端下载速度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM