扩展我的评论:
# Simple batch file
Get-Content -Path 'D:\Temp\abc.bat'
echo off
cls
echo Hello
# Run the .bat file
D:\Temp\abc.bat
# Results - no window is seen unless you are really paying attention since it will flash very fast
<#
Hello
#>
# Using the call operator vs Start-Process
& 'D:\Temp\abc.bat'
# Results - same window response as above
<#
Hello
#>
Start-Process -FilePath ' D:\Temp\abc.bat' -Wait
# Results - same window response as above
<#
no results are displayed
#>
Start-Process -FilePath ' D:\Temp\abc.bat' -PassThru
# Results - same window response as above - But this shows it ran
<#
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
19 3 1536 2524 0.00 10160 11 cmd
#>
$A = Start-Process -FilePath ' D:\Temp\abc.bat'-PassThru;$A.ExitCode
$A
# Results - same window response as above - But this shows it ran
<#
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
0 0 0 0.02 19128
#>
如果你想看看 .bat 做了什么,那么你必须在 .bat 中暂停一下...
echo off
cls
echo Hello
pause
来自 PowerShell 的调用和 3rdP 可执行文件,来自 TechNet 链接。
- 电话接线员 &
为什么:用于将字符串视为 SINGLE 命令。对交易有用
带空格。
在 PowerShell V2.0 中,如果您正在运行 7z.exe (7-Zip.exe) 或其他以数字开头的 > 命令,则必须使用命令调用运算符 &。
PowerShell V3.0 解析器现在更智能了,在这种情况下,您不再需要 &。
详细信息:运行命令、脚本或脚本块。调用运算符,也称为“调用运算符”,可让您运行存储在变量中并由字符串表示的命令。因为调用操作符不解析命令,所以无法解释命令参数
示例:
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen
当外部命令有很多参数或者参数或路径中有空格时,事情会变得很棘手!
用空格,你必须嵌套引号,结果并不总是清楚的!
在这种情况下,最好像这样分开所有内容:
$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
或类似:
$AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs
对于 Start-Process,根据相同的 TechNet 链接。
($p = Start-Process -FilePath 'F:\SWDATINSCD_AN_19000101\MediSpan.Install.exe' -ArgumentList '/Autostart:DEV_Medispan_Data' -wait -NoNewWindow -PassThru)
$p.HasExited
$p.ExitCode