Leveraging PowerShell to run a script on a remote machine that triggers a batch file

Having trouble running a batch file on a remote server to execute an automated Selenium test

On a remote server, there is a batch file named mybatch.bat that triggers a Selenium test. Below is the code snippet in a Powershell script on the same server:

$BatchFile = "mybatch.bat"

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

Running this PowerShell script locally on the server works fine and initiates the Selenium test successfully. Now attempting to run the test from another machine using PowerShell remoting:

There is a PowerShell script on another server with the following segment of code:

$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 }

Although the script connects to the remote machine and initializes a remote session without errors, it seems to time out before the Selenium test completes. Additionally, no files or logs are generated as expected.

Troubleshooting the code execution issues and echoing batch file outputs back to the local machine seem to be major concerns at this point.

Answer №1

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

There are a couple of issues with the provided code snippet:

  • The variable $command inside the scriptblock and outside the scriptblock refer to different scopes, causing the variable inside the scriptblock to be undefined and resulting in an empty output.

  • Even if $command was defined properly, the scriptblock would still only display its value as it is treated as a string. PowerShell does not execute strings directly unless using Invoke-Expression, which is generally discouraged.

To address these issues, you can modify the code as follows:

$CMD = "D:\mybatch.bat"

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

Invoke-Command -Session $TargetSession -ScriptBlock { & $using:CMD }

Answer №2

To run a batch file on a remote machine using PowerShell Remote Session, start by typing dot followed by a space. Then, enter the exact path of the batch file located on the remote machine.

For example:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Facing difficulties configuring selenium on python3(linux)

Hey there, I'm relatively new to Python and recently came across the Selenium module. Can anyone lend a hand? I've been having trouble getting the selenium module to work with python3. I downloaded the geckodriver for Firefox but still no luck. ...

Tips on how to interact with buttons of type <input> in C#

My attempts to click on the element using .css/by classname were unsuccessful... <div class="col-xs-12"> <input type="submit" name="commit" value="save" class="pp-btn pp-btn-primary w-70 save-item-description" data-disable-with="saving..."> ...

Python and Selenium: Mastering the Art of Drop-Down Menus

I am currently on the hunt for a dropdown element in order to select an option from it. I know that Selenium has a built-in class specifically for handling select drop-downs, but I'm having trouble locating the actual element. Could someone point out ...

Issue connecting to the remote browser detected with a possibility that it has terminated unexpectedly while using Internet Explorer

Whenever I attempt to execute the testcase below using IE Driver, it consistently fails. However, the same code runs successfully when executed with Chrome Driver or Edge Driver. Operating System: Windows 10 Selenium Ver ...

Could not find the Python Selenium ID in the system

I'm currently utilizing Selenium in Python to automatically input the username on a web browser. However, I encountered an issue where it couldn't locate the ID. Strangely enough, it worked perfectly fine on the Bing website but failed on this pa ...

Encountering difficulties extracting audio from the webpage

Attempting to extract audio(under experience the sound) from 'https://www.akrapovic.com/en/car/product/16722/Ferrari/488-GTB-488-Spider/Slip-On-Line-Titanium?brandId=20&modelId=785&yearId=5447'. The code I have written is resulting in an ...

What are the steps for integrating Jenkins CI with Selenium?

Currently, I have reached a point where I am capable of constructing my own test automation framework using TestNG with Selenium. My next objective is to automate runs periodically by incorporating Selenium Grid. I've been conducting some research on ...

Is it possible to operate a selenium webdriver without displaying the browser window?

I attempted to find a solution on this website because there is a question that closely resembles mine. Unfortunately, the suggested solution did not resolve my issue. Below is the current code I am working with... Is it feasible to initiate the webdriver ...

`In Selenium using Python, the browser window opens a URL but abruptly closes``

On opening the Facebook page in the browser, I am encountering an issue where it fails to click on the "Create New Account" button as expected. I have used the following XPATH for locating the Create New Account button: //a[text()='Create New Account& ...

The Python Selenium script encountered difficulty locating an element within a Frameset followed by a Frame

Can you help me locate the highlighted element within this HTML structure? Here is the target element This is the current code implementation I am using. from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver. ...

Cannot see the created item in Rails application when using RSpec, Capybara, Selenium, and JavaScript

Currently, I am in the process of developing a web store. The key functionality is already implemented where all products are displayed on one screen along with the list of ordered items. Whenever a product is selected for ordering, it should instantly app ...

Scraping URLs from src attribute with Python and Selenium: A step-by-step guide

My current project involves downloading multiple images and organizing them into folders using Selenium. The challenge I'm facing is extracting two ID's associated with each image from the URL. No matter what method I use - whether it's by t ...

Configuring flash to work with Selenium using Python's Firefox browser

Every time I open Firefox, the Flash plugin is there. However, when I use Selenium to open Firefox, Flash is not enabled. How can I enable Flash in Selenium? Here is the code snippet I am currently using: firefoxProfile = FirefoxProfile() firefoxProfi ...

Tips for launching a new tab within the current browsing session

Looking for a way to open a new tab in the same session? I have attempted the usual solutions provided by others with no success. I am specifically working with the Chrome browser for this task. My project is coded in Java. ...

After refreshing the page, I am looking to choose the next web element. What is the best way to proceed with this task?

While working with Selenium, I am encountering an issue where after selecting a webelement from a dropdown list using the select class, the page refreshes and the webelement is reset to its default value. How can I ensure that I am able to select the nex ...

Ways to capture user input with Selenium WebDriver in Java

I need to create a Java project that will fulfill the following use cases: Request URL from user: Display an alert window to prompt the user for the URL. If the user clicks "Cancel", the program should close. Open the provided URL in a browser: After ...

Managing duplicate xpaths in Selenium with Java

Seeking assistance with dynamically selecting items from a drop-down menu based on input. The challenge arises when values share the same name. For example, consider this Cucumber statement: When I go to the "Inventory" / "Inventory" application When ut ...

Retrieving only the main content within an HTML element

Currently, I am utilizing Python along with Selenium and a touch of BeautifulSoup for web scraping. Although it seems like they should work harmoniously, I have encountered a specific problem that has stumped me thus far. While I believe the solution is wi ...

What causes Selenium tests to run so slowly?

I am currently developing a web scraper that is designed to download images in a completely legal manner. However, I have encountered a problem during the execution of the script. In certain instances, particularly after a page has finished loading, there ...

What could be causing selenium to struggle in locating specific text on a webpage

In my webpage, I have a "tree menu" that can be opened by clicking on a button. Once the tree menu is open, I need to follow certain steps and click on different titles. For example, I would click on "19,20", then "may", and finally "2". At the top of the ...