Execute Powershell commands remotely

4

1

I have a linux server with winexe package installed. It's the same as PSExec, and everything works okay when running simple commands, but when I run it with this command below I get a parsing error!

winexe -U user%password //host "powershell -Command (New-Object System.Net.WebClient).DownloadFile("http://host/filename.iso","c:\file.iso")"

Can anyone figure what's wrong with this? Is it's cause a conflict in the quotes around the url and file name, with the quotes around the whole command?

Ammar Lakis

Posted 2013-03-18T21:47:44.197

Reputation: 43

1Can you post the full error message? – cpast – 2013-03-18T22:07:40.730

Answers

3

Try using the format: powershell.exe -Command "& {<command>}"

This worked for me, using your example:

powershell.exe -Command "& {(New-Object System.Net.WebClient).DownloadFile('http://google.com/robots.txt','c:\robots.txt')}"

Also, I used single-quotes in DownloadFile

Reference: http://technet.microsoft.com/en-us/library/hh847736.aspx

Josh

Posted 2013-03-18T21:47:44.197

Reputation: 4 746

1

Nested use of double quotes won't work. Try something like this:

winexe -U user%password //host "powershell -Command (New-Object System.Net.WebClient).DownloadFile('http://host/filename.iso','c:\file.iso')"

skno

Posted 2013-03-18T21:47:44.197

Reputation: 11

0

The easiest way is to run the command directly using single quote. For example listing the processes can be done as follows:

winexe -U "Domain\PC-Name" //IP Address 'powershell.exe Get-Process'

VAM

Posted 2013-03-18T21:47:44.197

Reputation: 1