gpt4 book ai didi

windows - 批处理脚本 : Merging the output of WMIC calls

转载 作者:可可西里 更新时间:2023-11-01 11:53:20 24 4
gpt4 key购买 nike

为了获取安装在远程 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,普通话-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

最佳答案

您的代码的主要问题是您正在从 %%a%%b 可替换参数中检索值,但是您的 for code> loops 使用这个范围的名称。所有都使用 %%x%%y 作为起点。所以在这段代码中

for /f "tokens=1-2 delims=," %%y in ("%%x") do (
set "_name=%%b"
)

如果 for 参数是 %%y 并且您正在定义您将使用两个标记 (tokens=1-2),那么数据(如果可用)将存储在第一个标记的 %%y 和第二个标记的 %%z 中。

此外,delims 和 token numbers 需要仔细定义,因为系统名称和产品名称可以包含您用来标记字符串的相同字符。

@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

此代码对两组数据检索使用/value 格式。它将以Field=value的形式返回记录,在本例中为Name=......,等号用于拆分记录,取第一个标记(Name 字符串)到每个 for 循环的第一个可替换参数,该行的其余部分到下一个(字母顺序)可替换参数(tokens=1,*, 将第一个标记作为第一个参数,其余的或行作为第二个参数)

关于windows - 批处理脚本 : Merging the output of WMIC calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23902733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com