Running batch file on remote machine (VM) using PowerShell

0

Quick question, have searched for awhile but can't find answer anywhere. Some background: I have a local machine and remote machine (Virtual Machine) that I am connecting through RDP.

I have a batch file that I run that starts the RDP session through powershell commands and logs in with credentials. My next step is where I run into problems, I have another batch file on the remote machine that I want to run (also Powershell commands).

When I run this batch file physically through the RDP (by either double clicking or running through command prompt) all is well.

When I trigger the batch file remotely the batch file runs, but not the way I want. It seems like it runs in the background and not actually showing what its doing through the RDP session I have. The reason I don't want this running in silent mode is because I have a script that uninstalls and re installs a program. Since it is not MSI I have to have several "Send Keys" in my code.

So in the end my question is this, can I run a batch file remotely that will act as if I double clicked the batch file on the remote machine??? I believe that there is an option psexec, but I would prefer not to use any more programs than needed.

kal

Posted 2015-01-14T20:18:16.213

Reputation: 1

ok seems like this is a limitation of powershell and not of the batch file im running. The ps1 files allow for a non-interactive session to be created. Anyone have a workaround? – kal – 2015-01-14T22:18:59.433

Did you manage to solve this yet? I have just come across the same issue and trying to find a fix. My batch file triggers a load of python scripts and doesn't work if i trigger it remotely but it did correctly if i RDP manually and double click the batch file. – Will Webb – 2016-05-18T15:32:55.563

Have you checked if the batch\powershell script runs under the same user as the user that is currently loged in through the remote session? – Ricardo Bohner – 2020-01-10T23:13:08.473

Answers

0

Have you tried Enter-PSSession?

$s = New-PSSession -ComputerName Server01 -Credential Domain01\User01 
# Enter the session yourself
Enter-PSSession -Session $s

or

Invoke-Command -Session $s -ScriptBlock {
    [SCRIPT GOES HERE]
}

user317619

Posted 2015-01-14T20:18:16.213

Reputation: 21