当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 PowerShell 中从 URL 下载文件?

作者:精品下载站 日期:2024-12-14 05:08:38 浏览:15 分类:玩电脑

如何在 PowerShell 中从 URL 下载文件?


一个非常常见的要求是您需要从 PowerShell 中的 URL 下载文件。在本 PowerShell 教程中,我们将探索使用 PowerShell 从 URL 下载文件的不同方法,并为每种方法提供示例和完整脚本。

要从 PowerShell 中的 URL 下载文件,您可以使用 Invoke-WebRequest cmdlet,其 -Uri 参数表示文件的 URL,-OutFile目标路径的 参数。例如:

Invoke-WebRequest -Uri "https://powershellfaqs.com/wp-content/uploads/2024/02/TestFile.zip" -OutFile "C:\Bijay\TestFile.zip"

这一行是一种使用 PowerShell 直接从互联网下载文件的简单而有效的方法。

1.使用Invoke-WebRequest

Invoke-WebRequest cmdlet 是在 PowerShell 中下载文件最直接的方法。它类似于 Unix/Linux 系统中的 wget 或 curl 命令,是标准 PowerShell cmdlet 的一部分。

以下是如何使用 Invoke-WebRequest 使用 PowerShell 下载文件的示例:

# Define the URL and the destination path
$url = "https://powershellfaqs.com/wp-content/uploads/2024/02/TestFile.zip"
$destination = "C:\Bijay\TestFile.zip"

# Use Invoke-WebRequest to download the file
Invoke-WebRequest -Uri $url -OutFile $destination

该脚本设置要下载的文件的 URL 以及要保存该文件的本地路径,然后使用 Invoke-WebRequest 将文件从该 URL 下载到指定的目的地。

在我使用 Visual Studio 代码执行 PowerShell 脚本后,您可以在下面的屏幕截图中看到输出。

[玩转系统] 如何在 PowerShell 中从 URL 下载文件?

2. 使用 Start-BitsTransfer

在 PowerShell 中下载文件的另一种方法是使用 Start-BitsTransfer cmdlet。此 cmdlet 是 BITS(后台智能传输服务)的一部分,它更强大且适合网络传输。对于大文件或当您需要对传输过程进行更多控制时,它特别有用。

以下是如何使用 Start-BitsTransfer 在 PowerShell 中下载文件:

# Define the source URL and the destination path
$source = "https://powershellfaqs.com/largefile.iso"
$destination = "C:\Bijay\largefile.iso"

# Use Start-BitsTransfer to download the file
Start-BitsTransfer -Source $source -Destination $destination

此脚本的工作方式与前面的示例类似,但它使用 Start-BitsTransfer 进行下载,它可以处理网络中断并恢复下载。

3.使用新对象System.Net.WebClient

对于那些更喜欢面向对象方法的人,您可以使用 System.Net.WebClient 类在 PowerShell 中下载文件。此方法提供对下载过程的高级控制,并允许您操作标头、使用代理服务器等。

以下是在 PowerShell 中使用 System.Net.WebClient 的示例。

# Create a new WebClient object
$webClient = New-Object System.Net.WebClient

# Define the URL and the destination file
$url = "https://powershellfaqs.com/document.pdf"
$destination = "C:\Bijay\document.pdf"

# Download the file
$webClient.DownloadFile($url, $destination)

此脚本创建一个 WebClient 对象,设置 URL 和目标,然后使用 DownloadFile 方法下载文件。

4. 处理安全连接 (HTTPS)

当您从安全连接 (HTTPS) 下载文件时,您可能会遇到 SSL/TLS 证书问题。要解决此问题,您可以调整 PowerShell 中的 ServicePointManager 设置以绕过证书检查:

# Ignore SSL/TLS certificate errors (use with caution)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

# Now proceed with the download using any of the methods above
# Create a new WebClient object
$webClient = New-Object System.Net.WebClient

# Define the URL and the destination file
$url = "https://powershellfaqs.com/document.pdf"
$destination = "C:\Bijay\document.pdf"

# Download the file
$webClient.DownloadFile($url, $destination)

请记住,绕过 SSL/TLS 检查可能会让您面临安全风险,例如中间人攻击。仅当您确定来源的安全性和完整性时才使用此方法。

结论

从 URL 下载文件是一项常见任务,PowerShell 使之变得简单。您可以在 PowerShell 中使用 Invoke-WebRequestStart-BitsTransferSystem.Net.WebClient 从 PowerShell 中的 URL 下载文件。

在本 PowerShell 教程中,我解释了如何使用各种方法和示例从 PowerShell 中的 URL 下载文件

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯