10

I am trying to execute a .bat or .cmd file on a remote pc with this:

PSEXEC \\192.0.0.230 "\Users\James\Desktop\BatchFiles\StopStart.cmd"

This will Disable a NIC and Enable another NIC, problem is, it ignores the timeout /t 10, because instead of opening the file on the remote computer, it runs the commands written in the batch file instead. So if it Disables the first NIC it will lose connection meaning it can't enable the other NIC. But if it can just run the file and let the lines run on the remote PC instead, this is not a problem, because then even if I lose connection due to the NIC being disabled, it should just keep running the .bat file on the remote PC instead of running the commands written in it from my own PC.

Is this possible? Thanks.

SeeeeS
  • 103
  • 1
  • 1
  • 6
  • 2
    A simple solution would be to have a single command disable the first nic and enable the second nic, i.e. & – Natecat Aug 25 '22 at 21:27
  • This question is similar and has similar answer, but at least one answer that isn't here: https://stackoverflow.com/questions/32482483/execute-a-batch-file-on-a-remote-pc-using-a-batch-file-on-local-pc – computercarguy Aug 26 '22 at 16:43
  • @Natecat If that were posted in the answer box, I'd upvote it. – wizzwizz4 Aug 26 '22 at 17:59
  • @Natecat I understand, but there needs to be time in between the two NIC's, approximately 20 seconds. After first NIC gets disabled then I'm not sure if it will still have connection to continue the command. – SeeeeS Aug 26 '22 at 22:10
  • Have you tried using the `-c` option to PsExec? – Neil Aug 27 '22 at 00:56

4 Answers4

8

You might be able to use schtasks. I haven't tried this, but something like this might work:

schtasks /create /s SRV01 /tn MyApp /tr "cmd /c c:\path\to\my.bat"
schtasks /run /s SRV01 /tn MyApp

You can have a look at Microsoft reference for schtasks commands for more details.

mtmk
  • 601
  • 4
  • 7
  • 2
    I got the solution by putting a batch file on the remote computer that has the timeout /t 20 and Enabling/Disabling the NIC's. And using schtasks on my own PC to run a task on the remote computer, and that task runs the batch file. Thanks! – SeeeeS Aug 25 '22 at 12:36
  • Another sneaky trick that sometimes helps, is to run a Ping command to introduce a (somewhat unpredictable) delay. – MikeB Aug 26 '22 at 12:26
3

Maybe you can use a second short script (that you launch with psexec instead) to have the remote machine copy that batch file locally, then in the last line use call to start that second local batch file copy (for example copied to %temp%)?

It'll lose connection to the network location, but as "call" was on the last line, that doesn't matter anymore.

MiG
  • 1,065
  • 1
  • 6
  • 18
  • re: 20 second pause, [UnxUtils](https://sourceforge.net/projects/unxutils/) includes the unix `sleep` command, which allows you to pause for a specified amount of time. If you [add](https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them) them to the `path` system variable, they can easily be called upon. – MiG Aug 27 '22 at 11:55
2

If you have access to Powershell on both machines, you may be able to use Invoke-Command or Invoke-CommandAs to run the command

This admittedly might be more powerful than a regular .bat execution, but based on past experience, would be how in automation environments we've had our Jenkins instances run commands remotely on nodes from the Jenkins host server machine.

In Powershell, there's an Invoke-Command option, which since I last looked at it, appears to have added additional parameters for running effectively as another user on that remote machine (I'm more familiar with the Invoke-CommandAs additional installation component, so I'm not 100% of this particular solution.). That said, based on the documentation there, the following is an example I'm copying here in case that link dies, and for quick reference.

Invoke-Command -FilePath <Path_To_Script*> -ComputerName <ComputerName**>

If, however, you're hoping to use the Invoke-CommandAs approach, this works from at least Powershell version 3.0, as I understand.

That would then look like this:

Invoke-CommandAs -ComputerName '<ComputerName**>' -Credential $Credential -ScriptBlock { Get-Process }

Or:

Invoke-CommandAs -ComputerName '<ComputerName**>' -AsUser $Credential -ScriptBlock { <Path_To_Script*> }

* i.e. "c:\scripts\test.ps1" in their example, or in your case, "\Users\Bridge\Desktop\BatchFiles\StopStart.cmd" - just be sure to remove the angled brackets in the code examples above.

** NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. If using an IP Address, you must also provided a Credential parameter, along the lines of, for example -Credential Domain01\User01

Markus Meyer
  • 1,370
  • 5
  • 11
  • 17
0

Please open Remote Desktop Connection and log into the system from there.

You'll then be able to run whatever program you use.

I was nearly able to run GUI programs in WSL 2.0 on Windows 10 Pro via using the above.

If this doesn't work, please advise as such.

Canadian Luke
  • 24,199
  • 39
  • 117
  • 171
  • 3
    Hi, that is the current method I am doing but if it would be possible to run a program from remote without RDP that would be nice. – SeeeeS Aug 25 '22 at 09:10
  • 1
    RDP is a bad idea just to run a simple non-GUI command. There's no reason to run the whole desktop which is far slower than CLI, especially over slow connection. Besides RDP isn't a tool for automation – phuclv Aug 27 '22 at 09:24
  • 1
    Well, not if the system is in a server rack on your premises. – The Royal Australian Aug 28 '22 at 21:50