Heber Solis
Heber Solis

Reputation: 493

batch running in the background

I am working with CA workload automation, I tell it to run a batch file or a ps file and it does but the things I tell it to run, it runs them in the background, is there a way to force a batch file to run in the foreground o visually?

EDIT

start /wait /d"C:\Turing\App\" TuringExpo.exe "456555384" "Test"
start /wait /d"C:\Program Files\Microsoft Office\root\Office16\" winword.exe

Upvotes: 0

Views: 1043

Answers (2)

postanote
postanote

Reputation: 16126

As for this ...

'batch running in the background'

is there a way to force a batch file to run in the foreground o visually?

... PowerShell when you call and external file, ie., .exe or .bat, the are defined ways to call these and address your use case. This is well documented all over the web. As noted, those commands are not PowerShell, they are just batch stuff in a PowerShell script or I am going to assume these are what is in your batch file.

All that being said, the process of calling an external file is defined.

No real need to do this as a batch file. Just call this directly in a PowerShell script.

See also details the following---

PowerShell: Running Executables

# Example(s):
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi"  /fullscreen

<#
Things can get tricky when an external command has a lot of parameters or there 
are spaces in the arguments or paths!
With spaces, you have to nest Quotation marks and the result it is not always 
clear!
In this case, it is better to separate everything like so:
#>

$CMD =  'SuperApp.exe'
$arg1 =  'filename1'
$arg2 =  '-someswitch'
$arg3 =  'C:\documents and settings\user\desktop\some other file.txt'
$arg4 =  '-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4

# or something like this:
$AllArgs =  @('filename1',  '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs


<#
** This method should no longer be used with V3
Why: Bypasses PowerShell and runs the command from a cmd shell. Often times used
with a DIR which runs faster in the cmd shell than in PowerShell (NOTE: This was
an issue with PowerShell v2 and its use of .Net 2.0, this is not an issue with 
V3).

Details: Opens a CMD prompt from within PowerShell and then executes 
the command and returns the text of that command. The /c tells CMD that 
it should terminate after the command has completed. There is little to 
no reason to use this with V3.
#>

# Example:
<#
runs DIR from a cmd shell, DIR in PowerShell is an alias to GCI. This will 
return the directory listing as a string but returns much faster than a GCI
#>

cmd /c dir c:\windows

See also

Execution of external commands in PowerShell done right

PowerShell and external commands done right 'https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right'

Top 5 tips for running external commands in Powershell

PowerShell: Running Executables

Solve Problems with External Command Lines in PowerShell

Using Windows PowerShell to run old command-line tools (and their weirdest parameters)

Quoating specifics https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules

So, this ...

start /wait /d"C:\Turing\App\" TuringExpo.exe "456555384" "Test"
start /wait /d"C:\Program Files\Microsoft Office\root\Office16\" winword.exe

... could become something link this, just using PowerShell, no batch file involved:

Start-Process -FilePath 'C:\Turing\App\TuringExpo.exe' -ArgumentList '456555384', 'Test' -Wait
Start-Process -FilePath winword -Wait

Upvotes: 0

Io-oI
Io-oI

Reputation: 2565

This can be more simple...

1) Pay attention to @compo comments, remove path to winword.exe

start /wait /d"C:\Program Files\Microsoft Office\root\Office16\" winword.exe

2) For works with TuringExpo.exe, you need use the correct name/windows, maybe only name.exe don't gona works.

3) The 3 last lines in code vbs is adapted from this answer /by @Siddharth Rout


@echo off 

(
echo/ Set objShell = WScript.CreateObject("WScript.Shell"^)
echo/ WScript.Sleep 999
echo/ objShell.AppActivate "TuringExpo"
echo/ WScript.Sleep 999
echo/ Set objWord = CreateObject("Word.Application"^)
echo/ objWord.Visible = True
echo/ SetForegroundWindow ("objWord"^)
) >"%temp%\Show_Active_TMP.vbs"

start "" /b /d "C:\Turing\App\" "TuringExpo.exe" "456555384" "Test" 
start "" /b /d "C:\Program Files\Microsoft Office\root\Office16\" "winword.exe"

"%__APPDIR__%timeout.exe" 2 & "%windir%\System32\CScript.exe" "%temp%\Show_Active_TMP.vbs" //nologo 
del /q /f "%temp%\Show_Active_TMP.vbs"  & rem./ Next line you may add many commands... or && goto :eof

Upvotes: 0

Related Questions