0

I have below a piece of code.

ForEach($line in $lines){
Try
{
    $file = "C:\brivo\" + $line
    $ftpuri = "ftp://administrator:[email protected]/divyesh/" + $line
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftpuri)
    $webclient.DownloadFile($uri,$file)
}
Catch [Exception]
{
    Write-Host $_.Exception | format-list -force
}
}

For the details of $lines when I run

$lines.GetType()
$lines

it is displaying like below.

IsPublic IsSerial Name                                     BaseType                                                           
-------- -------- ----                                     --------                                                           
True     True     Collection`1                             System.Object                                                      
Divyeshwewetwe.json

$file = "C:\brivo$($line)" line generating error every time I run this code with different options. like

$file = ("C:\brivo\" + $line)

and

$file = "C:\brivo\" + $line

it is giving me the below error every time.

System.Net.WebException: An exception occurred during a WebClient request. ---> 
System.ArgumentException: Illegal characters in
path.

When I give a static file name static then it is working fine like below.

$file = "C:\brivo\123.json"
13
  • 1
    What's in $line? Commented Apr 12, 2022 at 7:31
  • it is an array of file names. I have updated the code. Commented Apr 12, 2022 at 7:34
  • That still does not explain what is in $line. Commented Apr 12, 2022 at 7:35
  • it is the file name. and its name is Divyeshwewetwe.json Commented Apr 12, 2022 at 7:45
  • 1
    The problem is most likely that there are characters in the value of $line that are not valid if ftp uris. Add something like write-host “ftp uri = ‘$ftpuri’” and show us the output. Commented Apr 12, 2022 at 7:47

1 Answer 1

1

Continuing from my comment, I think your collection $lines is definitively not a string array, but an array of objects instead. (Collection`1 means it is a Generic collection of one or more of the types that they store or use)

You probably got that from importing a CSV file, something like

"FileName", "SomeOtherStuff"
"Divyeshwewetwe.json", "nothing here" 
"123.json", "" 

Of course, you would have different column headers and the number of columns will also differ from this small example.

The key here is that you want to iterate over the values in just the one column, here called FileName

Try

I changed variable $lines into $data and iterating variable $line into $item as I find that more appropriate when iterating objects from a collection of objects.

$data = Import-Csv -Path 'X:\YourInputFile.csv'
foreach($item in $data){
    try {
        $file = Join-Path -Path 'C:\brivo' -ChildPath $item.FileName  # only the string value column FileName
        $ftpuri = "ftp://administrator:[email protected]/divyesh/{0}" -f $item.FileName
        $webclient = New-Object System.Net.WebClient
        $webclient.DownloadFile($ftpuri,$file)
        # dispose of the current $webclient
        $webclient.Dispose()
    }
    catch {
        Write-Host $_.Exception.Message
    }
}
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.