2013-03-27 60 views
0

我是PowerShell腳本編寫新手。我正在努力處理MS文檔,並找到一些可用的例子。Powershell BitsTransfer https基本認證語法

我正在嘗試使用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} 
    } 

回答

2

當你必須提供在非交互模式憑據,您可以通過以下方式創建一個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 
+0

謝謝。很有幫助。我相信你的榜樣也會幫助其他人。 – Colin 2013-03-28 14:22:00