方法1
在Powershell中使用iwr.它工作,显示进度,但它的速度慢10倍,并且直到文件在内存中时才会刷新
powershell -command "& { iwr https://github.com/mitchellspryn/AirsimHighPolySuv/releases/download/V1.0.0/SUV.zip -OutFile SUV.zip }"
方法2
在Powershell中使用.Net webclient.它工作,但没有显示进展,你不能通过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 }"
方法4
不确定我从哪里获得这段代码,但我已经修改了好几次.希望这会帮助你.
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"
Powershell下载并运行exe文件
步骤1:
我们有两种方法来完成这项任务.第一种是使用Invoke-Webrequest.我们需要的唯一两个参数是.exe文件的URL,以及我们希望该文件在我们的本地机器上的位置.
$url = "http://www.contoso.com/pathtoexe.exe"
$outpath = "$PSScriptRoot/myexe.exe"
Invoke-WebRequest -Uri $url -OutFile $outpath
我在这里使用$PSScriptRoot,因为它可以让我在运行Powershell脚本的地方放下exe,但是可以自由选择一个你选择的路径,比如C:/ temp或者下载或者你想要的任何东西.您可能会注意到,对于较大的文件,Invoke-WebRequest方法需要很长时间.如果是这种情况,我们可以直接致电.Net并希望加快速度.
我们将$url和$outpath的变量设置为相同,但我们将使用以下.Net代码而不是Invoke-WebRequest:
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $outpath)
第2步:
调用可执行文件很容易.
$args = @("Comma","Separated","Arguments")
Start-Process -Filepath "$PSScriptRoot/myexe.exe" -ArgumentList $args
原作者:http://aiuxian.com/article/p-dqjlkvra-bym.html
No Comments