0
votes

I am trying to invoke a remote bat file from my local machine that should start and run the bat in a remote machine terminal. My script is able to connect and show me the output. However, it is invoking the remote bat file but waiting on my screen with bat file output. and my idea is the bat file should invoke and running in the remote machine rather than showing the output on my terminal local terminal. What should be the way here?

loop{

Invoke-Command -ComputerName $computer -credential $cred -ErrorAction Stop -ScriptBlock { Invoke-Expression -Command:"cmd.exe /c 'C:\apacheserver.bat'" } }

3

3 Answers

0
votes

From what I understand you want the .bat file to not show you the result. If so, you should do it with the Out-Null or by redirecting STDOUT and STDERR to NULL.

IE: Invoke-Expression -Command:"cmd.exe /c 'C:\apacheserver.bat'" | Out-Null

0
votes

If I'm understanding correctly, you want to suppress the output from the .bat file inside your local console? If so, redirecting to some form of $null is the way to go. You also shouldn't need Invoke-Expression as Invoke-Command can run the file directly:

Invoke-Command -ComputerName $computer -Credential $cred -ErrorAction Stop -Scriptblock {
    cmd.exe /c 'C:\apacheserver.bat' | Out-Null
}

You could also use > $null instead of Out-Null if you prefer.

If you want to redirect the output of the Invoke-Command call to the remote console instead, that kind of defeats the purpose of the cmdlet. You could try redirecting the console output to a file on the remote computer if you want a local record:

$remoteFile = '\\server\C$\Path\To\Output.txt'
 Invoke-Command -ComputerName $computer -Credential $cred -ErrorAction Stop -Scriptblock {
        cmd.exe /c 'C:\apacheserver.bat' > $remoteFile
    }
0
votes

If I understand you, you want to run the script in the background and not wait for it:

Invoke-Command $computer -Credential $cred { start-process C:\apacheserver.bat }