为了获取远程Windows系统中安装的软件包列表,我使用以下命令:
wmic /node:172.22.73.15 product get name /format:csv
输出:
172.22.73.15,Compatibility Pack for the 2007 Office system
172.22.73.15,Microsoft Office Professional Plus 2007
172.22.73.15,Microsoft Office InfoPath MUI (English) 2007
172.22.73.15,Microsoft Office Access MUI (English) 2007
172.22.73.15,Microsoft Office Shared Setup Metadata MUI (English) 2007
172.22.73.15,Microsoft Office Excel MUI (English) 2007
172.22.73.15,Microsoft Office Shared 64-bit Setup Metadata MUI (English) 2007
172.22.73.15,Microsoft Office Access Setup Metadata MUI (English) 2007
172.22.73.15,Microsoft Office PowerPoint MUI (English) 2007
172.22.73.15,Microsoft Office Publisher MUI (English) 2007
172.22.73.15,Microsoft Office Outlook MUI (English) 2007
为了获取同一系统的主机名,我正在使用 以下命令:
wmic /node:172.22.73.15 computersystem get name /format:csv
输出:
172.22.73.15,Mandar-PC
我需要一个批处理脚本,它将在脚本执行时向我显示与IP地址(在输出中)相关联的主机名以及软件包。简而言之,输出应该如下:
172.22.73.15,Mandar-PC,Compatibility Pack for the 2007 Office system
172.22.73.15,Mandar-PC,Microsoft Office Professional Plus 2007
172.22.73.15,Mandar-PC,Microsoft Office InfoPath MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Access MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Shared Setup Metadata MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Excel MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Shared 64-bit Setup Metadata MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Access Setup Metadata MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office PowerPoint MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Publisher MUI (English) 2007
172.22.73.15,Mandar-PC,Microsoft Office Outlook MUI (English) 2007
如何实现这一目标?
更新
我尝试编写批处理脚本如下:
@echo off
setlocal enableextensions enabledelayedexpansion
set "node=172.22.73.15"
ping -n 1 !node! | find "TTL=" > NUL
if not errorlevel 1 (
for /f "delims=" %%x in (
'wmic /node:"!node!" computersystem get name /format:csv ^| find /i "!node!"'
) do (
for /f "tokens=1-2 delims=," %%y in ("%%x") do (
set "_name=%%b"
))
for /f "skip=2 delims=" %%x in ('wmic /node:"!node!" product get name /format:csv ^| find /i "!node!"'
) do (
for /f "tokens=1-2 delims=," %%y in ("%%x") do (
set "_ip=%%a"
set "_soft=%%b"
echo !_ip!,!_name!,!_soft! >> out.txt
)
))
输出:
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
答案 0 :(得分:2)
您的代码的主要问题是您从%%a
和%%b
可替换参数中检索值,但您的for
循环都没有使用此范围的名称。所有人都使用%%x
或%%y
作为起点。所以在这段代码中
for /f "tokens=1-2 delims=," %%y in ("%%x") do (
set "_name=%%b"
)
如果for
参数为%%y
并且您定义了将使用两个令牌(tokens=1-2
),则数据(如果有)将存储在%%y
中对于第一个令牌,%%z
表示第二个令牌。
此外,需要仔细定义delims和令牌编号,因为系统名称和产品名称可以包含用于标记字符串的相同字符。
@echo off
setlocal enableextensions enabledelayedexpansion
set "node=172.22.73.15"
ping -n 1 !node! | find "TTL=" > NUL
if not errorlevel 1 (
for /f "tokens=1,* delims==" %%a in (
'wmic /node:"!node!" computersystem get name /value'
) do if "%%a"=="Name" for %%c in (%%b
) do for /f "tokens=1,* delims==" %%d in (
'wmic /node:"!node!" product get name /value'
) do if "%%d"=="Name" (
echo(!node!,%%c,%%e
)
)
endlocal
此代码用于两组数据检索/值格式。它将以Field=value
形式返回记录,在本例中为Name=......
,并使用等号分隔记录,将第一个标记(Name
字符串)转换为第一个可替换参数每个for
循环,以及该行的其余部分到下一个(按字母顺序)可替换参数(tokens=1,*
的含义,将第一个标记带到第一个参数,其余部分带到第二个参数)
答案 1 :(得分:0)
在接受的答案中收到的有价值的指导方针的帮助下,我修改了代码,它对我来说很好。
修改后的代码:
@echo off
setlocal enableextensions enabledelayedexpansion
set "node=172.22.73.15"
ping -n 1 !node! | find "TTL=" > NUL
if not errorlevel 1 (
for /f "delims=" %%a in (
'wmic /node:"!node!" computersystem get name /format:csv ^| find /i "!node!"'
) do (
for /f "tokens=1-2 delims=," %%b in ("%%a") do (
set "_name=%%c"
))
for /f "skip=2 delims=" %%a in ('wmic /node:"!node!" product get name /format:csv ^| find /i "!node!"'
) do (
for /f "tokens=1-2 delims=," %%b in ("%%a") do (
set "_ip=%%b"
set "_soft=%%c"
echo !_ip!,!_name!,!_soft! >> a.txt
)
))