How to run a .bat file in a VM from Powershell - powershell

I am trying to parallelize an operation by running .bat files in several remote VMs simultaneously using a Powershell script from a local VM. I can successfully run the .bat file in the local VM by using the following:
Start-Process cmd.exe -ArgumentList $batOptimization_path, $vmNumberParam, $rInstanceParam, $optDataParam, $dataSetParam
The first param in -ArgumentList is the path where the .bat file is located, the rest are command line parameters for the .bat file. To try to run the .bat file on a VM, I am using the following:
$psSession = New-PSSession -ComputerName $script:FsvRConfig.VmList[$i].IpAddress
Invoke-Command -ArgumentList $batOptimization_path, $vmNumberParam, $rInstanceParam, $optDataParam, $dataSetParam `
-Session $psSession `
-ScriptBlock {Start-Job -ScriptBlock { Start-Process cmd.exe -ArgumentList $args[0], $args[1], $args[2], $args[3] -AsJob}} `
-ErrorAction SilentlyContinue
This does not run the .bat files properly. The jobs start and then fail, so obviously this is not the way to launch the jobs. Any advice on getting the .bat files to launch in the remote VMs the way it does in the local VM would be greatly appreciated!

Related

Run bat file via PowerShell

I am trying to run a batch file from remote computer but it also doesn't work locally
Invoke-Command -ComputerName $remoteComputer -ScriptBlock {
Start-Process "C:\Users\Administrator\Desktop\version\Installer\Installer.bat" -NoNewWindow -Wait
} -Credential $credentials -ErrorAction Stop
There is no errors but nothing happens
The installer.bat is located under C:\Users\Administrator\Desktop\version\Installer
The batch file contain setup.exe that also located at the same folder.
I am searching for hours the solution
For local I used this:
Start-Process "cmd.exe" "/c C:\Users\Administrator\Desktop\version\Installer\Installer.bat" -NoNewWindow
Seems that its working locally when i am going to the right folder (cd c:..)
and run
Invoke-Expression -Command 'cmd.exe /C C:\Users\Administrator\Desktop\version\Installer\Installer.bat'
So how can i do it remotely (I mean to go to the right path first)

How to make powershell wait for a batch file to complete the execution of all comand on remote-server

$storesess = New-PSSession -ComputerName marshy -Credential marshy001
Enter-PSSession -Session $storesess
Invoke-Command -ScriptBlock {start-process C:\Users\marshmellow\Documents\Some\xyz.bat }
Exit-PSSession
Above is the script which calls a bat file saved on remote server C:\Users\marshmellow\Documents\Some\xyz.bat
The bat file has two commands one which sets the working directory using "pushd" and another which stops a application process. The second command takes a couple of minutes to complete. I have found that the Start-Process doesn't wait for the second command to complete successfully, it just fires the command and closes the process.
Is there any way to make the Start-Process wait for the command to get completed successfully as I have already tried using -Wait which doesn't work.
If there's a way to even open a cmd session on the remote server and pass few commands in it saved in variables and that output is relayed to my PowerShell script even that is fine. can anyone please help?
Using cmd.exe might work?
Invoke-Command -ScriptBlock {
cmd /k "C:\Users\marshmellow\Documents\Some\xyz.bat"
} -ErrorAction Stop
If not, you could probably Start-Process -Wait on cmd.exe, then supply the batch commands as an -ArgumentList
What do you get if you try this?
Invoke-Command -FilePath $PathToBatchFile
You do not need the Invoke-Command cmdlet. Just use Start-Process with the -Wait parameter and pass the correct parameters to cmd:
$storesess = New-PSSession -ComputerName marshy -Credential marshy001
Enter-PSSession -Session $storesess
Start-Process cmd -ArgumentList "/C C:\Users\marshmellow\Documents\Some\xyz.bat" -Wait
Exit-PSSession

Using PowerShell to execute a remote script that calls a batch file

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

Trigger external remote script

I have a powershell script located on a different server, and I want to trigger this script from the build server(TFS 2018) using a network path.
//MyServer/MyScripts/run.ps1
In the Powershell build step I need to specify script path under TFS source path. Is there a way to trigger the external script?
This is what I've tried without success....
Invoke-Command -ComputerName MyServer -ScriptBlock {Get-Process -Name //MyServer/MyScripts/run.ps1} -ArgumentList 'arg1', 'arg2'
Something like this:
Invoke-Command -Session $session -scriptblock {Pushd $rootFolder
.\$scriptname.ps1}
Or you could Invoke-Command -FilePath
Depends on where you want to run it.

Unable to execute EXE remotely using Powershell

There is an exe file on backup server “Backupsrv” C:\Program files\Veritas\NetBackup\bin\admincmd\bpdbjobs.exe.
When I run this exe on “Backupsrv” using PowerShell it is giving some output on console
& "C:\Program Files\Veritas\netbackup\bin\admincmd\bpdbjobs.exe" -all_columns
Now I tried to run this exe from an application server remotely using Invoke-Command as below.
$result = Invoke-Command -ComputerName "backupsrv" -Credential $cred -ErrorAction Stop -ScriptBlock {
& "C:\Program Files\Veritas\netbackup\bin\admincmd\bpdbjobs.exe" -all_columns
}
No result When executed remotely.
The exe is being executed but not giving any result.