2
1
I've figured out how to test if a specific instance of software is running using Tasklist.
@echo off Title - FTView Client Application Finder
TASKLIST /FI "WINDOWTITLE EQ Grain*"
I get a response that looks something like this.
Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ DisplayClient.exe 3768 Console 4 62,476 K
Is there a way to use this information to start an application if it is not found?
My intention is to create a scheduled task to run every 15-30 Minutes to run this a batch file that will look for this specific instance of software and launch the software if it is not already running. I can't use the Task Image name because there are multiple instances of the same software running on this machine, so I have to look for a specific instance.
With Damian L.'s help I've made some progress on this using Powershell instead of Command Line. Still have one hitch at line #17 (2nd Else) the script launches the application requested, but doesn't set the $LoggingResult variable correctly. It just outputs the last known $LoggingResult.
#Script Looks for FTView Logging Application
#If Found - Logs that the application is Running to the Log Text File
#If Not Found - Launches the production logging client and Logs appropriate message to the Log Text File
#If Multiple Instances are found - Notifies the operator to close one down.
$Time = Get-Date #variable for the current date string
$Logfile = [string] 'c:\Shared Folder\LoggerLaunchLog.txt' #variable Log File Path
$WindowLookup = [string] 'Grain*' #variable for what window title you are looking for.
$TargetApp = [string]'C:\Users\Public\Documents\RSView Enterprise\SE\Client\GGCLA-WWMill.cli' #Variable for defining the application to launch
#
#
$process = Get-Process | Where-Object -Like MainWindowTitle -Value $windowlookup #Looks for running process with matching window title
#evaluation & action depending on result
If ($process.count -gt 0){
If ($process.count -gt 1) {$LoggingResult = "Multiple Instances of Logging Application Are Running - Please Close 1"}
else {$LoggingResult = "Logging Application is Running"}
}
else {Start-Process $TargetApp{$LoggingResult = "Logging Application not found - Loading Application"}}
Write-Output "$Time $LoggingResult" #Command Line output for Powershell Visual display - optional
Add-content $Logfile -value "$Time $LoggingResult" #Writes the results & actions to the Log Text File
#pause #for testing to visually see output
Final Edit - Thanks again to Damian T. for the assist, it's all working properly now...
#Script Looks for FTView Logging Application
#If Found - Logs that the application is Running to the Log Text File
#If Not Found - Launches the production logging client and Logs appropriate message to the Log Text File
#If Multiple Instances are found - Notifies the operator to close one down.
$Time = Get-Date #variable for the current date string
$Logfile = [string] 'c:\Shared Folder\LoggerLaunchLog.txt' #variable Log File Path
$WindowLookup = [string] 'Grain*' #variable for what window title you are looking for.
$TargetApp = [string]'C:\Users\Public\Documents\RSView Enterprise\SE\Client\GGCLA-WWMill.cli' #Variable for defining the application to launch
$process = Get-Process | Where-Object -Like MainWindowTitle -Value $windowlookup #Looks for running process with matching window title
#evaluation & action depending on result
If ($process.count -gt 0){
If ($process.count -gt 1) {$LoggingResult = "Multiple Instances of Logging Application Are Running - Please Close 1"
Write-Output "$Time $LoggingResult"
pause}
else {$LoggingResult = "Logging Application is Running"}
}
else {Start-Process $TargetApp
$LoggingResult = "Logging Application not found - Loading Application"}
Write-Output "$Time $LoggingResult" #Command Line output for Powershell Visual display - optional
Add-content $Logfile -value "$Time $LoggingResult" #Writes the results & actions to the Log Text File
#pause #for testing to visually see output
3
Hint: findstr - Search for strings in files.
– DavidPostill – 2018-01-16T22:13:46.530