Powershell BitsTransfer https基本身份验证语法
我是PowerShell脚本的新手.我在MS文档方面苦苦挣扎,并发现了一些可以使用的示例.
I'm new to PowerShell scripting. I'm struggling with the MS documentation and finding few examples to work with.
我正在尝试使用BitsTransfer脚本每周自动从ntis.gov下载大型txt文件.我使用的是.ps1脚本,因为显然SSIS如果不编写.NET代码就无法做到这一点.
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.
通过https:使用NTIS颁发的用户名和密码访问此文本文件.如何在身份验证字符串中指定(硬编码)密码?我知道这是不好的做法.有更好的方法吗?
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}
}
当您必须以非交互方式提供凭据时,可以通过以下方式创建PSCredential对象.
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