简体   繁体   中英

Start a background process in Powershell

I have a PowerShell script that needs to kick off a batch file and essentially monitor its progress. My assumption was that I could use Start-Process without -wait and this would allow me to do some processing immediately after kicking off the batch file. I'm currently testing a simple script as below:

$BatchFilePath = D:\Scripts\testDB.bat
$stamp = (Get-Date).ToString("yyyyMMdd.hh.mm.ss")

Start-Process -FilePath $BatchFilePath -PassThru -WindowStyle Hidden -RedirectStandardOutput D:\Scripts\DBBackupOutput_${stamp}.log

do
{
   $JobOutputFile = Get-Content D:\Scripts\DBBackupOutput_${stamp}.log
   $LineCount = $JobOutputFile.count
   Write-Host 'Line count: ' $LineCount
   Start-Sleep -s 5
} until ($LineCount -gt 1)

Once the batch file has been started I was trying to monitor the output in the log file but it isn't working as expected. The script seems to pause once the batch file is executed but I was expecting to see a line count every 5 seconds.

Is my understanding here correct? Or should I be using a Job instead? Or is something like this even possible?

Thanks in advance

Your code to start the process is correct. But you need to correct your do-until condition. To print the line count continuously first you need to assign your process to an object, and in 'do-until', you need to check if the process is completed by using.HasExited

Also add 'pause' at the end. So that the powershell window will not close immediately after the termination of batch process.

CODE

$process=Start-Process -WindowStyle Hidden -FilePath C:\batfile\mybat.bat -PassThru -RedirectStandardOutput C:\batfile\mybat.log

do
{
   $JobOutputFile = Get-Content C:\batfile\mybat.log
   $LineCount = $JobOutputFile.count
   Write-Host 'Line count: ' $LineCount   
   Start-Sleep -s 5
} until ($process.HasExited)
pause

I get these errors when I try to run it:

Start-Process : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'FilePath'.
Specified method is not supported.
At C:\Users\admin\foo\test.ps1:4 char:25
+ Start-Process -FilePath $BatchFilePath -PassThru -WindowStyle Hidden  ...
+                         ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.StartProcessCommand

Get-Content : Cannot find path 'C:\Users\admin\foo\DBBackupOutput_20200606.09.21.06.log' because it does not exist.
At C:\Users\admin\foo\test.ps1:8 char:21
+    $JobOutputFile = Get-Content DBBackupOutput_${stamp}.log
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\adm...06.09.21.06.log:String) [Get-Content], ItemNotFoundEx
   ception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Line count:  0

Then the last error keeps repeating. The problem is on line one you're assigning $batchfilepath to the output of the testdb.bat command, instead of the string 'testdb.bat'.

$BatchFilePath = 'D:\Scripts\testDB.bat'

With that change, it works ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM