David Gard David Gard - 1 year ago 191
PowerShell Question

Run a batch file in an externam CMD window from PowerShell

I have a PowerShell script that runs on startup and creates a couple of batch files. I need to create the batch files (as opposed to run them directly) as they are required for use by users at a later time.

This PowerShell script should then run each of these batch files in there own CMD window, as these CMD windows will persist until the server is shutdown.

While I can get a new CMD window to appear for each batch file, I can't seem to get them to run my batch files. What am I doing wrong?

# Grab the WebDriver folder and the public IP of this server
$webDriverFolder = "C:\WebDriver"
$WebDriverHubFilePath = "$webDriverFolder\Launch WebDriver Hub.bat"
$WebDriverIENodeFilePath = "$webDriverFolder\Launch WebDriver InternetExplorer Node 1.bat"
$publicIP = (Invoke-WebRequest ifconfig.me/ip).content

# Generate the batch file contents
$webDriverHubStr = "batch file stuff"
$WebDriverIENodeStr = "different batch file stuff"

# Generate up to date WebDriver batch files (with the correct IP and folder path)
$webDriverHubStr | Out-File "$WebDriverHubFilePath"
$WebDriverIENodeStr | Out-File "$WebDriverIENodeFilePath"

# Run the commands to launch WebDriver Hub and IE Node 1
Start-Process cmd.exe "$WebDriverHubFilePath"
Start-Process cmd.exe "$WebDriverIENodeFilePath"

Answer Source

Try this

Start-Process -FilePath cmd.exe -ArgumentList "/c $WebDriverHubFilePath"
Start-Process -FilePath cmd.exe -ArgumentList "/c $WebDriverIENodeFilePath"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download