4

I have this code:

powershell -command "& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }"

it's for download file.
When I execute it in cmd on remote server - everything is ok.
But when I want to execute this code from my computer on remote server using paexec, I have some troubles with escape characters.
Command in my CMD:

psexec.exe \\remoteServer.0.1 -u username -p password -dbg -lo D:\PsExec.log cmd /c "powershell -command "& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }""

I try to use ^ symbol, but the same error;
Code using ^ symbol for double-quotes:

psexec.exe \\remoteServer.0.1 -u username -p password -dbg -lo D:\PsExec.log cmd /c "powershell -command ^"& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }^""

Also, I tried to use \ (like in PHP) for escape, but have the same result.
Help with this or give advice how I can remotely download a file using the command line.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
via via
  • 41
  • 1
  • 1
  • 5

3 Answers3

5

Unfortunately CMD uses different escape characters depending on what is escaped and where. There is no single one escape character that would be used everywhere.

In most cases the next character is escaped by prepending it with a caret (^), e.g. in for /f loops:

for /f "tokens=1" %%a in ('type file.txt ^| find "something"') do ...

But sometimes characters are escaped by doubling them, e.g. percent characters (%) in batch scripts:

@echo off
echo %%DATE%%=%DATE%

Sometimes you may even need need to put in other escape characters (like backslashes) because you need to escape something not for CMD, but for the command the string is being passed to:

mountvol | findstr /r \\\\

Your particular scenario shouldn't require additional quotes or escaping, though. Just run the PowerShell commandline directly, without cmd /c:

paexec.exe \\remoteServer.0.1 -u username -p password powershell -command "&{...}"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Is it possible to use powershell the complete way? If so, you could try the following:

New-PsDrive -Name X -Root \\127.0.0.1\c$ -PsProvider FileSystem -Credential (Get-Credential)
Copy-Item -Path X:\RequestedFile.txt -Destination C:\Temp
Remove-PsDrive -Name X

If your destination is a http address you could perform following actions:

Invoke-WebRequest -Uri http://some.uri -Method Get -OutFile C:\temp\myfile -Credential (Get-Credential)

Hope that helps

Moerwald
  • 10,448
  • 9
  • 43
  • 83
0

Instead Of this:

psexec.exe \\remoteServer.0.1 -u username -p password -dbg -lo D:\PsExec.log cmd /c "powershell -command "& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }""

DO this:

psexec.exe \\remoteServer.0.1 -u 'username' -p 'password' powershell.exe -Command "& {(New-Object System.Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file')}"

This should do your work.

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45