对path的Invoke-WebRequest访问被拒绝UnauthorizedAccessException

我以pipe理员身份在Windows Powershell中运行一个Invoke-WebRequest

当我运行以下命令: Invoke-WebRequest http://speedtest.newark.linode.com/100MB-newark.bin -OutFile $env:TEMP ( 这里推荐 ),我得到一个错误,指出Access to Path is Denied见下图)。

在这里输入图像说明

我试过的东西没有用:

  • 在Windows Server 2008和2012以及Windows 8.1上运行该命令。
  • 取消选中Temp文件夹属性下的Read-Only设置权限。
  • 我将$env:TEMP更改$env:TEMP C:\

所有testing的操作系统的错误都是一致的。

看起来您正在获取拒绝访问,因为OutFile参数正尝试在AppData / Local文件夹中创build一个名为TEMP的文件,但是已经有一个名为TEMP的目录,所以存在命名冲突。 我收到了同样的错误运行您的命令,然后我添加了一个文件名,它的工作。 见下文:

 Invoke-WebRequest http://speedtest.newark.linode.com/100MB-newark.bin -OutFile $env:TEMP\100MB-newark.bin 

您的命令在我的系统上运行(Windows-7 SP1 x64)。 作为普通用户和pipe理员运行都可以工作…(尽pipepipe理员看起来有风险)。 我testing了x86和x64版本的Powershell

algorithm哈希path
 --------- ---- --------------------
 SHA256 A99192624C502AF0BF635D1186AC6ECAD613F0E4A48F5BA8D47B6E261C204908 C:\ Temp \ scratch \ 100MB-newark.bin

 SHA1 79105A819B8A0FB67DDCDEADC8E47C7F59DB8677 C:\ Temp \ scratch \ 100MB-newark.bin

 MD5 5F293997D8F256F9C6880272E0773429 C:\ Temp \ scratch \ 100MB-newark.bin

这里是我使用的漂亮的小Get-Webfile函数:添加到您的$ PROFILE或。 来源。 🙂

 Function Get-Webfile ($url) { $dest=(Join-Path $pwd.Path $url.SubString($url.LastIndexOf('/'))) Write-Host "Downloading $url`n" -ForegroundColor DarkGreen; $uri=New-Object "System.Uri" "$url" $request=[System.Net.HttpWebRequest]::Create($uri) $request.set_Timeout(5000) $response=$request.GetResponse() $totalLength=[System.Math]::Floor($response.get_ContentLength()/1024) $length=$response.get_ContentLength() $responseStream=$response.GetResponseStream() $destStream=New-Object -TypeName System.IO.FileStream -ArgumentList $dest, Create $buffer=New-Object byte[] 10KB $count=$responseStream.Read($buffer,0,$buffer.length) $downloadedBytes=$count while ($count -gt 0) { [System.Console]::CursorLeft=0 [System.Console]::Write("Downloaded {0}K of {1}K ({2}%)", [System.Math]::Floor($downloadedBytes/1024), $totalLength, [System.Math]::Round(($downloadedBytes / $length) * 100,0)) $destStream.Write($buffer, 0, $count) $count=$responseStream.Read($buffer,0,$buffer.length) $downloadedBytes+=$count } Write-Host "" Write-Host "`nDownload of `"$dest`" finished." -ForegroundColor DarkGreen; $destStream.Flush() $destStream.Close() $destStream.Dispose() $responseStream.Dispose() } 

**也许一个测量命令可能(更)在这个pipe道的某个地方提供速度。