Powershell (new-Object System.Net.Webclient). Downloadfile Strings as Arguments

Asked

Viewed 446 times

0

I have the following code that I use to download the last chromedriver for a specific folder:

(new-object System.Net.WebClient).DownloadFile("https://chromedriver.storage.googleapis.com/$($ChromeDriverLatestVersion)/chromedriver_win32.zip", "C:\path\Debug\chromedriver.zip")

in which $ChromeDriverLatestVersion is the number of the last release. This code works. However, if you put both arguments in strings, the second argument generates an exception. Example:

(new-object System.Net.WebClient).DownloadFile("$Chrome", "$ZipChromePath")

System.Management.Automation.MethodInvocationException: Exception calling "DownloadFile" with "2" argument(s): "The path is not of a legal form." ---> System.ArgumentException: The path is not of a legal form.
   at System.IO.Path.NewNormalizePath(String path, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.GetFullPathInternal(String path)
   at System.Net.WebClient.GetUri(String path)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at CallSite.Target(Closure , CallSite , Object , String , String )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

I know it’s the second argument that makes the exception, because if the

(new-object System.Net.WebClient).DownloadFile("$Chrome", "C:\path\Debug\chromedriver.zip")

the code runs. I just can’t explain why...

1 answer

0

For some reason I couldn’t see, my string had invalid characters. They were removed as follows.

$array=$array -replace "`n",""

and

$array=$array -replace "`r",""

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.