2017-10-19 101 views
0

我在PowerShell中那種跑出來的選項現在......下載Windows中大型文件的命令提示符/ PowerShell的

嘗試1

使用IWR。它的工作原理,顯示了進步,但它的10X速度較慢,不沖水,直到當文件在內存中:(。

powershell -command "& { iwr https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -OutFile SUV.zip }" 

嘗試2

使用的.Net Web客戶端在PowerShell中,它的工作原理,但目前尚無你不能按Ctrl + C :(終止進程。最後一個問題是一個很大的挫折。

powershell -command "& { (New-Object System.Net.WebClient).DownloadFile('https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip', 'SUV.zip') }" 

嘗試3

在Powershell中使用BITS傳輸。它的工作原理,顯示進步和近乎完美...直到你發現它mysteriously doesn't work on GitHub(錯誤與403禁止)!

powershell -command "& { Start-BitsTransfer -Source https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -Destination SUV.zip }" 
+0

爲什麼單獨運行'powershell.exe'('powershell.exe -command ...')?只需從PowerShell命令行直接運行所需的命令即可。 –

+0

我從另一個構建腳本中使用它,並將整個腳本轉換爲PS並不是我們現在的目標。 – ShitalShah

回答

0

不知道我從哪裏得到這段代碼,但我已經修改了幾次。希望這會幫助你。

function downloadFile($url, $targetFile) 
{ 
    "Downloading $url" 
    $uri = New-Object "System.Uri" "$url" 
    $request = [System.Net.HttpWebRequest]::Create($uri) 
    $request.set_Timeout(15000) #15 second timeout 
    $response = $request.GetResponse() 
    $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) 
    $responseStream = $response.GetResponseStream() 
    $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, 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", [System.Math]::Floor($downloadedBytes/1024), $totalLength) 
     $targetStream.Write($buffer, 0, $count) 
     $count = $responseStream.Read($buffer,0,$buffer.length) 
     $downloadedBytes = $downloadedBytes + $count 
    } 
    "Finished Download" 
    $targetStream.Flush() 
    $targetStream.Close() 
    $targetStream.Dispose() 
    $responseStream.Dispose() 
} 

downloadFile "http://URL_to_your_file" "C:\Path\to\destination.file" 
+0

用戶可以通過Ctrl + C取消這個功能嗎? – ShitalShah

0

下面是一個未經測試(由我)的代碼塊,聲稱在PowerShell 2.0中的網站,我發現@MDMarra禮貌下載文件。在目前的工作中,無法看出它是否真的按照錫上的說法行事。

$webClient = New-Object System.Net.WebClient $webURL = "http://www.google.com/index.html" $filePath = "c:\whatever\index.html" $webclient.DownloadFile($webURL,$filePath)

+0

這正是我的問題中描述的方法#2。它不回答我的問題。 – ShitalShah

相關問題