1

I am trying to use a variable that loops through a list of words to be used in downloading password-protected files from a URL.

sites.txt:

BOS
HFD
LGA
NYC
PHI
WWD

Powershell Script:

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

I am getting "unexpected token" errors so, I'm assuming that I am not using the $site variable correctly in the URL string.

1
  • I wonder if it's simply that a combination of slashes and backslashes are used when the $Path variable is set? Commented Oct 6, 2016 at 15:30

1 Answer 1

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

This will pass your list into individual URL's Edited to include change to '$Path' as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.