0

I'm trying to launch a batch file in a cmd window on a remote machine using powershell.

This is my ps1 script.

function Run-BatchFile
{
     param($computer = "mycomputer")
     $batfilename = "mybatch.bat"
     Invoke-Command -ComputerName $computer -ScriptBlock {param($batfilename) "cmd.exe /c C:\Batchfiles\$batfilename" } -ArgumentList $batfilename -AsJob
}

Run-BatchFile

When I run it from myhost machine I get this output..

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
5      Job5            RemoteJob       Running       True            mycomputer           param($batfilename) "c...

But no command prompt and batch file are launched on the remote machine.

Any clues as to what I am doing wrong or how to debug this as it looks like it works ok.

Thanks,

John.

1
  • Did you get a solution to your question? If yes, please share. Commented Dec 27, 2022 at 13:19

2 Answers 2

1

You can use PowerShell to launch batch files as well.

function Run-BatchFile
{
 param($computer = "mycomputer")
 $batfilename = "mybatch.bat"
 Invoke-Command -ComputerName $computer -ScriptBlock {param($batfilename) "powershell.exe -NoLogo -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File C:\Batchfiles\$batfilename" } -ArgumentList $batfilename -AsJob
}

Run-BatchFile

Keep in mind I haven't tested this exact code but the general idea should work.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using the ampersand operator to launch your script:

& "cmd.exe /c C:\Batchfiles\$batfilename"

Basically, because you've got the command in quotes Powershell will treat it as a string, not a command to execute. The ampersand forces Powershell to treat it as a command. See here for more information.

1 Comment

This doesn't made a difference unfortunately. I've also tried the full path to cmd.exe.. putting the ""cmd.exe /c C:\Batchfiles\$batfilename" into a $command variable and running that with and without the &.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.