0

I am having an issue with running a batch file that is located on a remote server.

I have a batch file located on a remote server that i want to run that kicks off an automated Selenium test. For simplicity, let's say the name of my batch file is mybatch.bat

I have the following code in a Powershell script located on the server:

$BatchFile = "mybatch.bat"

Start-Process -FilePath $BatchFile -Wait  -Verb RunAs 

If I run this PowerShell script locally on the server in ISE then it runs fine and it kicks off the selenium test which takes a couple minutes to run.

Now I want to try to execute this test from another machine by using PowerShell remoting. Let's assume that remoting is already configured on the servers.

I have a PowerShell script located on another server which has the following code segment. Assume that all of the session variables have the correct information set:

$CMD = "D:\mybatch.bat"

$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution

$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }

When this script runs, it does connect to the remote machine and create a remote session. It does look like it kicks off the batch file because PowerShell does not give me an error. But it does not wait for the full 3 or 4 minutes for the Selenium test to finish. It seems like it just times out. Also if I am logged onto the other machine, I don't see any Selenium web test running. No Selenium log files or results files are created on remote server as should be expected.

I was wondering what I could be doing wrong with my code.

Also, it seems that the server always returns the echo outputs of the batch file to my local machine. I see these random blinking white screen on ISE which looks like output from the batch file

1
  • add some logging to your script. i suspect you are seeing the "double hop" problem ... and attempting to access the BAT file is getting an access denied error. Commented Apr 18, 2019 at 14:38

2 Answers 2

1
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }

There are 2 issues with the above code:

  • $command inside the scriptblock and $command outside the scriptblock are different variables due to different scopes. The variable inside the scriptblock is thus undefined and the scriptblock will simply echo an emtpy value.

  • Even if $command weren't undefined, the scriptblock would still just echo its value, since it's defined as a string. PowerShell does not execute strings unless you're using something like Invoke-Expression (which you shouldn't).

This should do what you want:

$CMD = "D:\mybatch.bat"

$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution  

Invoke-Command -Session $TargetSession -ScriptBlock { & $using:CMD }
Sign up to request clarification or add additional context in comments.

2 Comments

Ansgar, thank you for your help. The new Invoke-Command does seem to kick off the remote Powershell script because I am seeing output loggging information on my ISE script. But my selenium batch file does not seem to kick off. Should I try using Invoke-Command on the Powershell script on the remote server too? Something like this: Invoke-Command -ScriptBlock { & using:$BatchFile }
I had to change the execution of the batch file in the remote Powershell to this syntax to work: & $BatchFile
0

If you would like execute a bat file from another machine by using PowerShell Remote Session, simply enter dot and then follow by a whitespace, then enter the exact path of bat file located on that remote machine.

Example

Invoke-Command -ComputerName RemoteComputerName -Credential $credential -ScriptBlock {. "D:\Temp\Install_Something.bat"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.