1

When I run this command locally on a remote machine it works:

powershell -Command "(New-Object Net.WebClient).DownloadFile('<WebPath>', 'C:\Users\<user>\Desktop\file.exe')"

When I try the same command remotely using PsExec in a batch file:

(Set downloadFileCommand = powershell -Command "(New-Object Net.WebClient).DownloadFile('%WebPath%', 'C$\Users\<user>\Desktop\file.exe')")

PsExec.exe \\<remote_machine> -u %username% -p %password% -s -d cmd /c %downloadFileCommand%

I am getting "cmd started on remote_machine with process ID #id_number."

However, nothing happened and the download wasn't executed. The suggestions here: Run PowerShell scripts on remote PC

didn't work for me.

Any suggestions?

Edit:

I managed to download the file through the command line (in cmd) using this command:

PsExec.exe \\<remote_machine> -u <username> -p <password> -d powershell -Command (New-Object Net.WebClient).DownloadFile('<url address','C:\file.exe')

But it doesn't work when I try this in a batch file:

(Set DownloadInstaller = "powershell -Command (New-Object Net.WebClient).DownloadFile('<url address','C:\file.exe')")

PsExec.exe \\<remote_machine> -u %username% -p %password% -h -d cmd /c %DownloadInstaller%
user2653179
  • 393
  • 1
  • 6
  • 21

1 Answers1

0

This worked - all in one line. I have a loop variable, a remote computer (%%a). I assumed that when I use PsExec, C:\ ... \file.exe "thinks" of C:\ locally in the remote computer, but that probably wasn't the case.

I had to write it all in one line, since the path in DownloadFile (The location I want to download the file to) is locally in the remote computer (which is a loop variable):

PsExec.exe \\%%a -u %username% -p %password% -h -d cmd /c powershell.exe -Command "&{ (New-Object System.Net.WebClient).DownloadFile('%url%','\\%%a\C$\Users\<username>\Desktop\%file%')}")
user2653179
  • 393
  • 1
  • 6
  • 21