powershell下载文件到服务器

在 PowerShell 中下载文件到服务器通常需要使用 Invoke-WebRequest 命令。以下是一个简单的示例,展示了如何使用该命令来下载文件:

$url = "https://example.com/file.zip"
$output = "C:\path\to\save\file.zip"
Invoke-WebRequest -Uri $url -OutFile $output

在这个示例中,你需要将 $url 替换为你想要下载的文件的 URL,将 $output 替换为你想要将文件保存的位置。

如果你需要授权访问文件,可以使用 -Credential 参数提供用户名和密码:

$url = "https://example.com/file.zip"
$output = "C:\path\to\save\file.zip"
$username = "myuser"
$password = "mypassword" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Invoke-WebRequest -Uri $url -OutFile $output -Credential $credential

在这个示例中,$username$password 变量分别代表用户名和密码,ConvertTo-SecureString 命令将密码转换为安全字符串,而 $credential 变量则包含了凭据信息。要使用凭据,只需要将 $credential 变量传递给 -Credential 参数即可。

  • 5年前
  •