Simple way to execute batchfile on antoher server with powershell?

Hello everybody,

I have the following problem.

I need to run a batchfile on Server 1 with a powershell command on Server 2.

When I run the batchfile manually (doubleclick) on Server 1, the batchfile is being executed well and does its deployment job.

Unfortunately when running the batchfile from Server 2 via a powershell command, it seems to work but then only shows me “echo off” (see below) and does not execute the rest of the batchfile.

Powershell Script on "Server2":

[pre]$Username = 'USERNAME' $Password = 'PASSWORD' $pass = ConvertTo-SecureString -AsPlainText $Password -Force $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

try
{
Invoke-Command -ComputerName “Server1” -ScriptBlock {cmd /c “L:\Test_Deploy.bat”} -Credential $Cred -ErrorAction Stop
}
catch {Write-Host “error”}[/pre]

 

L:\Test_Deploy.bat on Server1:

[pre]echo off cd /d "C:\Program Files\Microsoft SQL Server\###\DTS\Binn" ISDeploymentWizard.exe /Silent /SourcePath:"\\Server3\h$\Jenkins_workspace\SSIS\1234 Admin\Bin\Development\1234 Admin.ispac" /DestinationServer:"Server2" /DestinationPath:"/SSISDB/0 Tools/1234 B-Admin" EXIT /b 0[/pre]

 

 

Very gratefull for every kind of help.

Looking forward to hearing from you.

Best regards

Manfred

Manfred,

welcome to Powershell.org. Could please go back and fix you post by formatting your code as code using the code tag button named “PRE”? Thanks.

You are running one command line interface (cmd) with another command line interface (Powershell). I think it would be easier to run the necessary commands in your script block without wrapping it in a batch file.

The first suggestion would be to eliminate the BAT file and use Start-Process:

https://powershellexplained.com/2016-10-21-powershell-installing-msi-files/

If you have a requirement to use a BAT file, you need to use a call operator to tell Powershell that cmd is not an Alias or command, but an external executable you are executing:

https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

It has worked running a powershell cmd on an external server. Thank you so much for your help.

I have a new problem with parameters. Looking forwar to hearing from you.

https://powershell.org/forums/topic/powershell-remote-how-to-un-invoke-sqlcmd-for-sql-query-to-initiate-jenkins-de/