I'm trying to run this command that works perfectly fine, but from the run.exe
(new-object System.Net.WebClient).DownloadFile("host", "filename.ext");
Tried:
powershell.exe -noexit -Command "(new-object System.Net.WebClient).DownloadFile(\"host\", \"filename.ext\");"
But I get this error:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:1 char:1
+ (new-object System.Net.WebClient).DownloadFile("host ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
host and filename.ext are just placeholders.
Consider using Invoke-WebRequest
instead:
powershell.exe -c "Invoke-WebRequest -Uri http://server.domain.tld/path/to/file.ext -OutFile C:\path\to\save\download\file.ext"
If you need to use the old method, which is what you attempted above, you have escaped your quotes incorrectly. The download call should look like this:
powershell.exe -noexit -Command "(new-object System.Net.WebClient).DownloadFile(`"host`", `"filename.ext`");"
Your problem with the command above is that you are trying to escape your double-quotes with a \\
instead of a `
. The escape character in PowerShell is `
. Of course, you could always avoid the need to escape your string by using a single quote for the DownloadFile
parameter instead:
powershell.exe -noexit -Command "(new-object System.Net.WebClient).DownloadFile('host', 'filename.ext');"
Note that in PowerShell, double-quoted strings can have variables and subexpressions run directly in it, and can have escaped characters. Single-quoted strings escape all special characters and thus cannot have variables or subexpressions inserted directly into them. Both single-quoted and double-quoted strings do support Format Strings , however.
This should work:
Powershell.exe -NoExit -Command "& {$wc = New-Object System.Net.WebClient; $wc.DownloadFile(`"\\host\"`, `"targetfullpathname.ext"`)}"
A script block is needed to execute multiple commands.
Don't use backticks unless there are special miserable character(s).
Another method is already described by @Bender The Greatest.
This works for me with singlequotes that cmd doesn't interpret like doublequotes.
powershell (new-object Net.WebClient).DownloadFile('https://live.sysinternals.com/procmon.exe', 'procmon.exe')
Note that the first parameter is the full address of the file, not just the host.
powershell (new-object Net.WebClient).DownloadFile
OverloadDefinitions
-------------------
void DownloadFile(string address, string fileName)
void DownloadFile(uri address, string fileName)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.