Powershell BitsTransfer https 基本身份验证语法

Col*_*lin 2 authentication passwords powershell https microsoft-bits

我是 PowerShell 脚本的新手。我正在努力使用 MS 文档并找到几个可以使用的示例。

我正在尝试使用 BitsTransfer 脚本自动从 ntis.gov 每周下载一个大型 txt 文件。我正在使用 .ps1 脚本,因为显然 SSIS 在不编写 .NET 代码的情况下无法做到这一点。

访问此文本文件是通过 https: 使用 NTIS 颁发的用户名和密码。如何将密码指定(硬编码)到身份验证字符串中?我知道这是不好的做法。有一个更好的方法吗?

我的脚本看起来像这样-

    $date= Get-Date -format yyMMdd
    Import-Module BitsTransfer
    $Job = Start-BitsTransfer `
    -DisplayName DMFweeklytrans `
    -ProxyUsage AutoDetect `
    -Source https://dmf.ntis.gov/dmldata/weekly/WA$date `
    -Destination D:\Test.txt `
    -Authentication Basic `
    -Credential "myIssuedUsername" `
    -Asynchronous

While (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) {sleep 5}
Switch($Job.JobState)
    {
        "Transfer Completed" {Complete-BitsTransfer -BitsJobs $Jobs}
        default {$Job | Format-List} 
    }
Run Code Online (Sandbox Code Playgroud)

JPB*_*anc 5

当您必须在非交互模式下提供凭据时,您可以通过以下方式创建 PSCredential 对象。

$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$yourcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)

$Job = Start-BitsTransfer `
    -DisplayName DMFweeklytrans `
    -ProxyUsage AutoDetect `
    -Source https://dmf.ntis.gov/dmldata/weekly/WA$date `
    -Destination D:\Test.txt `
    -Authentication Basic `
    -Credential $yourcreds `
    -Asynchronous
Run Code Online (Sandbox Code Playgroud)