首页 > 解决方案 > 批处理在后台运行

问题描述

我正在使用 CA 工作负载自动化,我告诉它运行批处理文件或 ps 文件,它确实可以,但是我告诉它运行的东西,它在后台运行它们,有没有办法强制批处理文件运行视觉上在前景中?

编辑

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

标签: powershellbatch-fileworkload

解决方案


至于这个……

'在后台批量运行'

有没有办法强制批处理文件在前台运行?

... PowerShell 当您调用外部文件(即 .exe 或 .bat)时,定义了调用这些文件并解决您的用例的方法。这在整个网络上都有很好的记录。如前所述,这些命令不是 PowerShell,它们只是 PowerShell 脚本中的批处理内容,或者我假设这些是您的批处理文件中的内容。

话虽如此,调用外部文件的过程已定义。

没有必要将其作为批处理文件执行。只需在 PowerShell 脚本中直接调用它。

另请参阅以下详细信息---

PowerShell:运行可执行文件

# 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

也可以看看

在 PowerShell 中正确执行外部命令

正确完成 PowerShell 和外部命令 ' https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right '

在 Powershell 中运行外部命令的 5 大技巧

PowerShell:运行可执行文件

解决 PowerShell 中的外部命令行问题

使用 Windows PowerShell 运行旧的命令行工具(以及它们最奇怪的参数)

引用细节 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules

所以这 ...

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

...可能成为链接这个的东西,只需使用 PowerShell,不涉及批处理文件:

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

推荐阅读