Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

powershell - When running a .bat file with Start-Process

When I run this .bat file directly it does what its supposed to do. When I use start-process, a black window opens and then closes right away.

Start-Process \server01f$filelocationmyfile.bat -Verbose -NoNewWindow

Any ideas why it doesnt run?

Here is what is inside the batch script.

MediSpan.Install.exe /Autostart:DEV_Medispan_Data
question from:https://stackoverflow.com/questions/65867046/when-running-a-bat-file-with-start-process

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Extending my comment:

# Simple batch file
Get-Content -Path 'D:Tempabc.bat'
echo off
cls
echo Hello

# Run the .bat file
D:Tempabc.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:Tempabc.bat'
# Results - same window response as above
<#
Hello
#>


Start-Process -FilePath ' D:Tempabc.bat' -Wait
# Results  - same window response as above
<#
no results are displayed
#>

Start-Process -FilePath ' D:Tempabc.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:Tempabc.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
#> 

If you want to see what the .bat did, then you have to have a pause in the .bat...

echo off
cls
echo Hello
pause

Calling and 3rdP executable from PowerShell, from the TechNet link.

  1. The Call Operator &

Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.

In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another > command that starts with a number, you have to use the command invocation operator &.

The PowerShell V3.0 parser do it now smarter, in this case, you don’t need the & anymore.

Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters

Example(s):

& 'C:Program FilesWindows Media Playerwmplayer.exe' "c:videosmy 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 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 settingsuserdesktopsome other file.txt'
$arg4 = '-yetanotherswitch'
 
& $CMD $arg1 $arg2 $arg3 $arg4

or same like that:

$AllArgs = @('filename1', '-someswitch', 'C:documents and settingsuserdesktopsome other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs

For Start-Process, as per the same TechNet link.

($p = Start-Process -FilePath 'F:SWDATINSCD_AN_19000101MediSpan.Install.exe' -ArgumentList '/Autostart:DEV_Medispan_Data' -wait -NoNewWindow -PassThru)
$p.HasExited
$p.ExitCode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...