背景
多台服务器上运行多个同名程序(路径不同,配置文件不同),即某个程序有多个同名实例,运行在不同的服务器上。但由于程序为第三方提供,渐进式开发,所以每个程序的组件或者执行文件的版本,并不一样。现在需要快速挑选出,哪个服务器上的哪个实例,是最新版。
仅搜索.exe的版本信息
经过实践,写出如下命令行,在cmd窗口运行便可。
echo off & (for /f "tokens=1 delims=^ " %i in ('wmic process get executablepath ^| findstr "ABC.exe"') do (set text="%i" & set text1=%text:\=\\% & wmic datafile where Name=%text1% get name,version,installdate,creationDate,lastModified,lastAccessed,status)) & echo on注:
1,将ABC.exe换成实际的文件名便可。
搜索程序集(.exe+.dll)的版本信息
上面的做法,仅是查询.exe的文件信息(版本、创建日期、修改日期、最后访问日期等)。由于多台机器同名程序.exe的版本号差别并不大(说明开发人员并未严格升级版本号),经实际分析,其程序集的变动会更频繁些,故分析程序集的信息,能判断出各个程序实例的实际版本。
@echo off
call 0-proc-chcp-to-65001.bat
setlocal EnableDelayedExpansion
set /a process_count=0
rem search process, get the directory of proess, query all the file under the directory.
for /f "tokens=1 delims=^ " %%i in ('wmic process get executablepath ^| findstr "ABC.exe"') do (
set eap_path=%%i
set eap_dir=!eap_path:DIAEAP.exe=!
set /a process_count+=1
for /f "tokens=1 delims=^ " %%i in ('dir /b /s !eap_dir! ^| findstr /R "\.dll$ \.exe$" ^| findstr /I "pat1 pat2"') do (
set text="%%i"
set text1=!text:\=\\!
rem echo !text1!
wmic datafile where Name=!text1! get csname,name,version,installdate,creationDate,lastModified,lastAccessed,status
)
)
if !process_count!==0 (echo process not found)0-proc-chcp-to-65001.bat的内容如下。主要是修改code page。
@echo off
setlocal EnableDelayedExpansion
rem change code page to 65001
for /f "tokens=2 delims=:" %%i in ('chcp') do (
set codepage=%%i
set codepage=!codepage: =!
if "!codepage!" neq "65001" (
chcp 65001
echo code page change from !codepage! to 65001
) else (
rem echo "!codepage!"
)
)程序基本逻辑说明:
1,启用延时变量,将code page 设置为65001
2,查询当前正在运行的进程ABC.exe。然后得到其执行路径
3,搜索执行路径以及其子目录下的所有.exe以及.dll。第3方程序集不是关心点,只搜索感兴趣的,pat1 pat2是示例。文件名中的关键词。实际搜索时换成你自己关心的文件名关键词。
注:
1,上面是搜索单台服务器的多个程序实例及其关联的程序集信息。可以另写脚本程序,在多台服务器上执行此脚本。并将屏幕回显信息重定向到本地文件。这样便可汇聚多台服务器上运行的所有程序实例的程序集信息。
2,针对汇聚的结果,做些简单的替换工作(去掉表头,去掉空行),得到的便是cvs格式的文件(分隔符是空格)。用excel打开后分列便可。这样便实现了在一个excel文件中,将所有该程序实例的程序集版本信息(计算机名、程序集路径、版本号、创建日期、修改日期、最后访问日期等)集中收集起来。方便下一步的分析。
参考:
使用 WMI - PowerShell | Microsoft Docs
一行命令获取文件版本信息_xinke453的博客-CSDN博客