Jalena Blog

  • 首页
  • 程序开发
    • Java
    • Spring
    • SpringBoot
    • SpringMvc
    • Python
    • Odoo
    • C#
    • SQL
    • Web
    • Linux
  • 移动设备
    • Android
    • Router
  • 杂七杂八
    • 我的废话
    • 音频
    • 视频
    • 常用软件
  • 关于
  1. 首页
  2. 软件开发
  3. Windows
  4. 正文

PowerShell 下载文件

2022-04-15

最近在堡垒机上面做开发,遇到一个非常恶心的事情。机器使用的是Windows Server 2008,里面就一个IE,想下载一个软件都是各种权限受限!

那么就想绕过这个浏览器下载的方式,通常情况下可以拷贝个Wget、cUrl去下载,但这也需要去找到这个小程序。

其实在Windows上默认是带了PowerShell的,它本来就是个命令行,那么它肯定也有可以下载文件的方式。搜索引擎一搜,果然还有不少!

下载文件实例

自带命令下载文件的几种方式

  1. Invoke-WebRequest
  2. Invoke-RestMethod
  3. Start-BitsTransfer
  4. System.Net.WebClient

Invoke-WebRequest

Description

The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other significant HTML elements.
This cmdlet was introduced in Windows PowerShell 3.0.
# 下载文件
Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.10.4/python-3.10.4-amd64.exe" -OutFile "python-3.10.4-amd64.exe"

# 挂代理
Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.10.4/python-3.10.4-amd64.exe" -OutFile "python-3.10.4-amd64.exe" -Proxy "http://127.0.0.1:7890"

# 更多的的参数
Get-Help Invoke-WebRequest
Invoke-WebRequest

说明

此种方式是先将流读取至内存中,待全部读取完成再写入文件。因此,该方式是不适合做大文件下载的。

Invoke-RestMethod

Invoke-RestMethod与Invoke-WebRequest类似,区别在于RestMethod支持Json和XML类型,会尝试适当的解码器来读取。

# 下载文件
Invoke-RestMethod -Uri "https://www.python.org/ftp/python/3.10.4/python-3.10.4-amd64.exe" -OutFile "python-3.10.4-amd64.exe" -Proxy "http://127.0.0.1:7890"

# 更多参数
Get-Help Invoke-RestMethod
Invoke-RestMethod

Start-BitsTransfe

Start-BitTransfer是windows自带的一个程序,支持断点续传、多文件,因此它是一个非常适用于下载的命令。

# 下载文件
Start-BitsTransfer -Source "https://www.python.org/ftp/python/3.10.4/python-3.10.4-amd64.exe" -Description "python-3.10.4-amd64.exe"

# 下载多个文件
Import-CSV filelist.txt | Start-BitsTransfer
filelist.txt

Source, Destination
https://www.python.org/ftp/python/3.10.4/python-3.10.4-amd64.exe, python-3.10.4-amd64.exe
https://webcdn.m.qq.com/spcmgr/download/QQ9.5.9.28650.exe, QQ9.5.9.28650.exe

Start-BitsTransfe

System.Net.WebClient

(New-Object System.Net.WebClient).DownloadFile("https://webcdn.m.qq.com/spcmgr/download/QQ9.5.9.28650.exe","qq.exe")
标签: powershell
最后更新:2022-04-15

Jalena

原创内容,转载请注明出处! 部分内容来自网络,请遵守法律适用!

点赞
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

【腾讯云】云服务器特惠热卖中
文章目录
  • 下载文件实例
    • Invoke-WebRequest
    • Invoke-RestMethod
    • Start-BitsTransfe
    • System.Net.WebClient
标签聚合
java Centos odoo 其他 生活 wordpress docker MySql
分类
  • Android / 4篇
  • C# / 10篇
  • Codec / 9篇
  • Conversion / 14篇
  • Editing / 16篇
  • Java / 47篇
  • Javascript / 3篇
  • Linux / 30篇
  • Odoo / 25篇
  • Python / 6篇
  • SQL / 30篇
  • Web / 3篇
  • Windows / 4篇
  • 我的日记 / 10篇
  • 杂七杂八 / 93篇
  • 软件开发 / 72篇
  • 非线性编辑 / 1篇

COPYRIGHT © 2026 Jalena Blog. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备17025376号