我已经知道如何使用任务列表来测试特定软件实例是否正在运行。
@echo off 标题 - FTView 客户端应用程序查找器
任务列表 /FI “WINDOWTITLE EQ Grain*”
我收到的回复类似如下。
图像名称 PID 会话名称 会话# 内存使用情况 =========================== ======== ================ ============ ============ DisplayClient.exe 3768 控制台 4 62,476 K
如果找不到应用程序,有没有办法使用此信息来启动它?
我的目的是创建一个计划任务,每 15-30 分钟运行一次,以运行批处理文件,该文件将查找此特定软件实例并启动该软件(如果尚未运行)。我无法使用任务图像名称,因为此计算机上运行着同一软件的多个实例,所以我必须查找特定实例。
在 Damian L. 的帮助下,我使用 Powershell 而不是命令行取得了一些进展。在第 17 行(第二个 Else)仍然有一个问题,脚本启动了请求的应用程序,但没有正确设置 $LoggingResult 变量。它只是输出最后一个已知的 $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
最后编辑 - 再次感谢 Damian T. 的帮助,现在一切正常......
#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
答案1
老实说,对于这种性质的事情,我建议改用 PowerShell。PS v5.1 可以安装在 Windows 7(包括 7)之后的任何系统上。
但是,如果您只能使用基本命令提示符,这里有一个可以使用的示例批处理文件。
批
@echo off
:: Title - FTView Client Application Finder
:: This what is said if it's not running
set TESTSTR=INFO: No tasks are running which match the specified criteria.
set /A NOTRUNNING=0
:: Iterate over all of the lines produced by the command.
for /F "delims=" %%a in ('tasklist /fi "WINDOWTITLE EQ Grain*"') do (
:: If a line matches the test string, then the program isn't running.
if "%%a"=="%TESTSTR%" (
set /A NOTRUNNING=1
)
)
if %NOTRUNNING%==1 (
echo It is NOT running.
) else (
echo It is running.
)
电源外壳
但是,PowerShell 的功能更加广泛。其优点包括:
- 更严格的标准以匹配所需的工艺
- 执行流程的更多选项
- 更好的错误处理
- 更好的流量控制
由于我没有您想要匹配的程序,因此我很难想出一个原型 PowerShell 解决方案。不过,我可以分享我编写它的过程。
> Get-Process
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
19 5 2764 3752 0.00 12188 1 cmd
824 41 109200 123184 14.46 12520 1 powershell
为了简洁起见,仅列出了几个流程。
在 FTView 客户端运行时,获取进程 ID(在我的示例中,我将使用该powershell.exe
进程),然后运行以下命令:
> Get-Process -Id 12520 | Format-List *
Name : powershell
Id : 12520
PriorityClass : Normal
FileVersion : 10.0.14409.1005 (rs1_srvoob.161208-1155)
HandleCount : 950
WorkingSet : 137814016
PagedMemorySize : 123269120
PrivateMemorySize : 123269120
VirtualMemorySize : 696459264
TotalProcessorTime : 00:00:15.3192982
SI : 1
Handles : 950
VM : 696459264
WS : 137814016
PM : 123269120
NPM : 42680
Path : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Company : Microsoft Corporation
CPU : 15.3192982
ProductVersion : 10.0.14409.1005
Description : Windows PowerShell
Product : Microsoft® Windows® Operating System
__NounName : Process
BasePriority : 8
ExitCode :
HasExited : False
ExitTime :
Handle : 3492
SafeHandle : Microsoft.Win32.SafeHandles.SafeProcessHandle
MachineName : .
MainWindowHandle : 919266
MainWindowTitle : Administrator: Windows PowerShell
MainModule : System.Diagnostics.ProcessModule (powershell.exe)
MaxWorkingSet : 1413120
MinWorkingSet : 204800
Modules : {System.Diagnostics.ProcessModule (powershell.exe), System.Diagnostics.ProcessModule
(ntdll.dll), System.Diagnostics.ProcessModule (aswhooka.dll),
System.Diagnostics.ProcessModule (kernel32.dll)...}
NonpagedSystemMemorySize : 42680
NonpagedSystemMemorySize64 : 42680
PagedMemorySize64 : 123269120
PagedSystemMemorySize : 527352
PagedSystemMemorySize64 : 527352
PeakPagedMemorySize : 193662976
PeakPagedMemorySize64 : 193662976
PeakWorkingSet : 206139392
PeakWorkingSet64 : 206139392
PeakVirtualMemorySize : 711643136
PeakVirtualMemorySize64 : 711643136
PriorityBoostEnabled : True
PrivateMemorySize64 : 123269120
PrivilegedProcessorTime : 00:00:07.3320470
ProcessName : powershell
ProcessorAffinity : 255
Responding : True
SessionId : 1
StartInfo : System.Diagnostics.ProcessStartInfo
StartTime : 1/17/2018 20:45:30
SynchronizingObject :
Threads : {9812, 14088, 924, 14064...}
UserProcessorTime : 00:00:08.0184514
VirtualMemorySize64 : 696459264
EnableRaisingEvents : False
StandardInput :
StandardOutput :
StandardError :
WorkingSet64 : 137814016
Site :
Container :
这将向您显示所有可以过滤的属性。我之所以提到这一步,是因为这里的信息与批处理文件中使用的信息略有不同。让我们看看MainWindowTitle
。
MainWindowTitle : Administrator: Windows PowerShell
它包含确切地窗口标题栏中显示的内容。如果我们想根据此内容过滤选择,则可以使用以下命令:
> Get-Process | Where-Object -Filter-Object MainWindowTitle -Like "*PowerShell*"
这就是您确定该进程是否正在运行的方法。为了在条件语句中使用它,您可以执行以下操作:
> $process = Get-Process | Where-Object -Filter-Object MainWindowTitle -Like "*PowerShell*"
> $process.Count
1
> If ($process.Count -gt 0) { Write-Host Yes } Else { Write-Host No }
Yes
然后您可以使用 启动该应用程序Start-Process
。
通过使用PowerShell,您的脚本将更加健壮,并且只有两行代码。
$process = Get-Process | Where-Object -Filter-Object MainWindowTitle -Like "Grain*"
If ($process.Count -eq 0) { Start-Process FTView.exe }
如果您使用Get-Help Start-Process
,您将看到它有一堆可用于启动该过程的选项,例如传递参数、使用凭据、隐藏窗口等。