Run bat file via PowerShell - 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)

Related

How to run a .bat file in a VM from 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!

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

How to run command line argument using PowerShell script

I am able to install the software from the Windows CMD with the following command
setup.exe -inputFile C:\my_installer_input.txt
However, I want to achieve the same above using the PowerShell script.
I tried same from the PowerShell like this
Start-Process -FilePath "C:\Matlab R2018b\setup.exe" -inputFile "C:\my_installer_input.txt" -ArgumentList "/S
and it fails to run with the obvious reason -inputFile parameter is not available for Start-Process in PowerShell.
PowerShell also runs the native commands directly from PowerShell prompt, which means your command
setup.exe -inputFile C:\my_installer_input.txt
should work directly from the PowerShell prompt.
If you are executing on a remote machine, you can run with Invoke-Command as below.
Invoke-Command -Session $session -ScriptBlock { <YOUR CODE HERE> }
or
Invoke-Command -ComputerName <remote-computername> -ScriptBlock { <YOUR CODE HERE> }
If this is on a remote machine, do something like:
Invoke-Command -Computername ‘x’ -Scriptblock {
Set-Location C:\path\to\file
cmd /c setup.exe /arg1 /arg2
}

Run exe in background

I've tried the following:
Start-Process powershell -ArgumentList "C:\Program Files\Prometheus.io\prometheus.exe" -WindowStyle hidden
Invoke-Command -ComputerName . -AsJob -ScriptBlock {
'C:\Program Files\Prometheus.io\prometheus.exe'
}
Start-Job -Name "prometheus" -ScriptBlock {Get-Process prometheus.io}
Start-Job {& .\prometheus.exe}
Sometimes it starts but terminates immediately after starting. If I start it manually it works correctly.
How can I keep my process alive in background?
EDIT :
It doesn't worked because i wasn't in the directory of my process that need a file which pathfile is not set.
Your syntax for Start-Process is wrong, you don't need to reference powershell, just launch your program with the WindowStyle param set
Start-Process "C:\Program Files\Prometheus.io\prometheus.exe" -WindowStyle Hidden
The WorkingDirectory param can also be used to start the program in a specific directory
Start-Process "C:\Program Files\Prometheus.io\prometheus.exe" -WorkingDirectory "C:\Program Files\Prometheus.io" -WindowStyle Hidden

Set Executable's Working Directory When PowerShell Remoting

I'm using PowerShell remoting to execute an exe file on a remote server. The problem is that the exe needs to have its Working Directory set to the directory that the exe is in for it to run properly. If I run the exe locally (on the server) from a command prompt it works fine, and if I use Enter-PSSession (from my workstation) and then use Start-Process -FilePath [PathToExe] -WorkingDirectory [DirectoryPath] that works fine, but if I use Invoke-Command -ComputerName [Blah] -ScriptBlock [MyScriptBlock] or $session = New-PSSession -ComputerName [Blah]; Invoke-Command -Session $session -ScriptBlock [MyScriptBlock] (from my workstation) then the working directory does not get set.
This is what [MyScriptBlock] looks like:
$scriptBlock = {
param($version, $database)
$hubSyncronizerExeDirectoryPath = "C:\inetpubLive\ScheduledJobs\$version\"
$hubSyncronizerExePath = Join-Path $hubSyncronizerExeDirectoryPath 'Test.exe'
Set-Location -Path $hubSyncronizerExeDirectoryPath
Get-Location
Write-Output "$version $database"
Start-Process -FilePath $hubSyncronizerExePath -WorkingDirectory $hubSyncronizerExeDirectoryPath -ArgumentList '/database',$database
}
I've also tried using Invoke-Command instead of Start-Process, but it has the same effect; the Working Directory does not get set.
I've verified this by using the SysInternals Process Explorer, right-clicking on the process and choosing Properties. When I launch it locally or use Enter-PSSession, the Command Line and Current Directory properties are set, but not when using New-PSSession or just Invoke-Command with ComputerName.
I'm using both Set-Location and setting the -WorkingDirectory, which are the 2 typical recommended approaches for setting the working directory, and Get-Location does display the expected (server's local) path (e.g. C:\inetpubLive\ScheduledJobs\1.2.3.4). I'm guessing that this is just a bug with PowerShell (I'm using V4 on workstation and server), or maybe there's something I'm missing?
UPDATE
It turns out that the working directory was a red herring (at least, I think it was). For some reason everything works fine if I called my executable from a command prompt.
So in my Invoke-Command (I replaced Start-Process with Invoke-Command), changing this:
& ""$hubSyncronizerExePath"" /database $database
to this:
& cmd /c ""$hubSyncronizerExePath"" /database $database
fixed the problem.
Thanks for all of the suggestions guys :)
Try looking at New-PSDrive and see if that helps
I guess you'll want something like
New-PSDrive -Name WorkingDir -PSProvider FileSystem -Root "\\RemoteServer\c$\inetpubLive\ScheduledJobs\1.2.3.4"
CD WorkingDir:
I assume you should be able to amend your script to include and put in the $version variable in to the path on the New-PSDrive command...
Not certain this will do what you need it to do, but it's the first thing that sprang to mind...
Alternatively, try amending your script as follows:
$hubSyncronizerExeDirectoryPath = "\\remoteserver\C$\inetpubLive\ScheduledJobs\$version\"