将列表传递给URL字符串中的变量

时间:2016-10-06 15:24:29

标签: powershell

我正在尝试使用一个循环遍历单词列表的变量来从URL下载受密码保护的文件。

sites.txt:

BOS
HFD
LGA
NYC
PHI
WWD

Powershell脚本:

$sites = Get-Content C:\Users\...\sites.txt

$time = (Get-Date).ToString("yyyyMMdd")

$Username = 'hello'
$Password = 'world'
$url = "http://my.website/" + $sites + "/some.csv"

$Path = "D:\...\...\" + $sites + "/some.csv"
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $url, $path )

我得到了#34;意外的令牌"错误所以,我假设我没有在URL字符串中正确使用$site变量。

1 个答案:

答案 0 :(得分:0)

$sites = Get-Content C:\Users\...\sites.txt
ForEach ($Site in $sites){
$url = "http://my.website/" + $Site + "/some.csv"
$Path = "D:\...\...\" + $Site + "\some.csv"
$Path
$url
}

这会将您的列表传递到单个网址中 编辑包括更改“$ Path'同样。