Reputation: 1755
In order to fetch the list of software packages installed in the remote Windows system, I am using the following command:
wmic /node:172.22.73.15 product get name /format:csv
Output:
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
In order to get the hostname of the same system, I am using following command:
wmic /node:172.22.73.15 computersystem get name /format:csv
Output:
172.22.73.15,Mandar-PC
I need a batch script that will show me the hostname associated with the IP address (in the output) along with the software packages, when the script is executed. In brief, output should look like:
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
How can this be achieved?
Update:
I tried writing a batch script as follows:
@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
)
))
Output:
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
%a,%b,%b
Upvotes: 1
Views: 1221
Reputation: 1755
With the help of valuable guidelines received in the accepted answer, I have modified the code and it is working fine for me.
Modified Code:
@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
)
))
Upvotes: 0
Reputation: 70943
The main problem with your code is that you are retrieving values from %%a
and %%b
replaceable parameters, but none of your for
loops uses this range of names. All are using %%x
or %%y
as starting point. So in this code
for /f "tokens=1-2 delims=," %%y in ("%%x") do (
set "_name=%%b"
)
if the for
parameter is %%y
and you are defining you will use two tokens (tokens=1-2
), then the data, if available, will be stored in %%y
for the first token and %%z
for the second token.
Also, delims and token numbers need to be carefully defined as the system name and product name can contain the same character you are using to tokenize the string.
@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
This code uses for both sets of data retrieval a /value format. It will return records in the form Field=value
, in this case Name=......
and the equal sign is used to split the records, taking the first token (the Name
string) to the first replaceable parameter of each for
loop, and the rest of the line to the next (alphabetically) replaceable parameter (the meaning of the tokens=1,*
, take first token to first parameter and the rest or the line to the second)
Upvotes: 2