WebClient DownloadFile problem

Welcome Forums General PowerShell Q&A WebClient DownloadFile problem

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 7 months ago.

  • Author
  • #28162

    Points: 0
    Rank: Member

    I'm new to powershell but I think I'm trying to do something pretty simple. I probably just have a syntax error!

    I've written this command:

    (New-Object System.Net.WebClient).DownloadFile("http://MySite/fileextract.asp?userid=MyUser`&password=MyPassword`&file=MyFile.zip","c:\Download\MyFile.zip")

    Which runs successfully in Powershell.

    The problem I'm having is that when I try to use to Powershell.exe -command syntax in a batch file, I keep getting an error:

    PowerShell.exe -Command {(New-Object System.Net.WebClient).DownloadFile("http://MySite/fileextract.asp?userid=MyUser`&password=MyPassword`&file=MyFile.zip","c:\Download\MyFile.zip")}

    I get an unexpected token error on the coma character. Notice that there is two & characters in the url that I had to escape with `.

    What I'm I doing wrong?


  • #28164

    Points: 0
    Rank: Member

    powershell.exe has the -EncodedCommand parameter specifically to help you deal with weird parsing issues like this. You just need to convert your command to a base64 string (examples for this are in powershell.exe /?). For example:

    $code = @'
    (New-Object System.Net.WebClient).DownloadFile("http://MySite/fileextract.asp?userid=MyUser`&password=MyPassword`&file=MyFile.zip","c:\Download\MyFile.zip")
    $unicodeBytes = [System.Text.Encoding]::Unicode.GetBytes($code)
    $base64 = [System.Convert]::ToBase64String($unicodeBytes)
    Set-Clipboard $base64

    If you run that code, it puts the base64 representation of your command in the clipboard (assuming you're running a version of PowerShell with the Set-Clipboard command, of course.) Then you just need to call powershell.exe like this:

  • #28165

    Points: 0
    Rank: Member

    Great, this works perfectly!

    Thanks alot!

The topic ‘WebClient DownloadFile problem’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort