Simple way to execute batchfile on antoher server with powershell?

Welcome Forums General PowerShell Q&A Simple way to execute batchfile on antoher server with powershell?

Viewing 3 reply threads
  • Author
    Posts
    • #206202
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      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”:

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

       

      L:\Test_Deploy.bat on Server1:

      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

       

       

      Very gratefull for every kind of help.

      Looking forward to hearing from you.

      Best regards

      Manfred

      • This topic was modified 3 months ago by Kiran. Reason: Code Formatting
    • #206217
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      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.

    • #206223
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      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

    • #206442
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      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.

      Powershell Remote – How to un Invoke-Sqlcmd for SQL Query to initiate Jenkins De

Viewing 3 reply threads
  • The topic ‘Simple way to execute batchfile on antoher server with powershell?’ is closed to new replies.