简体   繁体   中英

Powershell BitsTransfer https basic authentication syntax

I'm new to PowerShell scripting. I'm struggling with the MS documentation and finding few examples to work with.

I'm trying to automate the weekly download of a large txt file from ntis.gov with a BitsTransfer script. I'm using .ps1 script because apparently SSIS can't do this without writing .NET code.

Access to this text file is via https: with an NTIS issued username and password. How can I specify (hard code) the password into the authentication string? I know this is bad practice. Is there a better way to do this?

My script looks like this-

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

When you have to provide credentials in non-interactive mode, you can create a PSCredential object in the following way.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM