我已经弄清楚了如何使用Tasklist测试特定的软件实例是否正在运行。
@echo关闭标题-FTView客户端应用程序查找器
任务列表/ FI“ WINDOWTITLE EQ谷物*”
我得到的响应看起来像这样。
图像名称PID会话名称会话#内存使用========================================= =============================== DisplayClient.exe 3768控制台4 62,476 K
如果找不到该信息,是否可以使用此信息来启动应用程序?
我的意图是创建一个计划的任务,每15-30分钟运行一次,以运行此批处理文件,该文件将查找该软件的特定实例,如果尚未运行该软件,则启动该软件。我无法使用任务映像名称,因为此计算机上正在运行同一软件的多个实例,因此我必须查找特定实例。
在Damian L.的帮助下,我使用Powershell而不是Command Line在此方面取得了一些进展。在第17行(第2条其他)处仍然存在故障,脚本将启动请求的应用程序,但未正确设置$ 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
老实说,我建议使用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 Client运行的情况下,获取进程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
,您将看到它有很多选项可以用来启动该过程,例如传递参数,使用凭据,隐藏窗口等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句