0

I have a .bat file that was written long ago by someone else, and there may be a better way to do this, but the .bat file runs a Powershell script, the Powershell script looks for a process running on the machine and on machine start up the Powershell script will kill an existing running process, if running, and then start the .exe file.
However, I am having an issue with the .cmd prompt window staying open and not only that, it stays open above the launched application (WPF app running full screen mode).

The .bat file code is

powershell -file "c:\SomeFolder\scripts\ManageApp2.ps1" -f "c:\SomeFolder\SomeApp.exe" -p "Check_Out" -S

Is there a way to have have the .cmd window close after the .bat file fires? Would it be done in the .bat file? I have see using /min "" but that didn't seem to work (or maybe I did it wrong).

The Powershell file script it runs looks like this:

#####Params#####
 param([string]$f = "empty", [string]$p = "empty", [switch]$k, [switch]$S, [switch]$h)
 if ($f -eq "empty" -or $p -eq "empty" -or $h) {
  Write-Host "ManageApp.ps1  -f <filename> -p <process> -t <start/stop> [-h]"
  Write-Host "    -f  absolute path to app to run"
  Write-Host "    -p  process of app as found in Task Manager"
  Write-Host "   -k  just kill process and do not restart app.  Cannot use with -S"
  Write-Host "   -S  Stay Alive.  will re-countdown if process every leaves memory.  
 Cannot use with -k"
   Write-Host "    -h  optional switch.  shows this help"
  Write-Host " This app will kill the app if found in memory then"
  Write-Host " restart it (default) or not (if -k is set)"
 Exit
 }

 #$app='$f'
 $appProcess=$p
 $sleepCountdown=1  #in seconds


 #kill app
 $processActive = Get-Process $appProcess -ErrorAction SilentlyContinue
 if($processActive)
 {
   Stop-Process -Force -processname $appProcess
 }

 if(!$k)
 {
if ($S) {
    #Start-Job -ScriptBlock {
        while ($true) {
            Start-Sleep -Seconds 1
            $processActive = Get-Process $appProcess -ErrorAction SilentlyContinue
            if (!$processActive) {
                $sC = $sleepCountdown
                while ($sC -gt 0) {
                  echo "Program will start in $sC seconds..."
                  Start-Sleep -Milliseconds 1000 #sleep for 1sec to allow flash to let go of file first and wait for windows startup to finish:
                  $sC--
                }
                
                #run app
                Start-Process -FilePath $f
            }
        }
    #} | Wait-Job
} else {
    while ($sleepCountdown -gt 0) {
      echo "Program will start in $sleepCountdown seconds..."
      Start-Sleep -Seconds 1 #sleep for 1sec to allow app to let go of file first and wait for windows startup to finish:
      $sleepCountdown--
    }

    #run app
    #Invoke-Expression -Command $app
    Start-Process -FilePath $f
  }
}

Any suggestions on hwo to run this and then have the prompt window close after the launch of the app or it runs?

ClosDesign
  • 3,894
  • 9
  • 39
  • 62
  • Maybe you can also check this [answer on how to run a CMD or batch file in silent mode](https://stackoverflow.com/questions/411247/running-a-cmd-or-bat-in-silent-mode) – lorvindc Aug 24 '23 at 15:26
  • Do you have control over how `cmd` / the batch file is invoked? If so, hide the window of that invocation via [`System.Diagnostics.ProcessStartInfo`](https://learn.microsoft.com/en-US/dotnet/api/System.Diagnostics.ProcessStartInfo). – mklement0 Aug 24 '23 at 16:00
  • Whats the purpose of the bat file? Can you not just have a shortcut instead, like launches the powershell directly like this: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Executionpolicy bypass -WindowStyle Hidden followed by the rest of your args – KG-DROID Aug 26 '23 at 09:58

0 Answers0