Read from file and process

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Read from file and process

Post by MigrationUser »

05 Aug 2013 12:38
prakash82x

I have a script which gathers some information by Querying WMIC from local server,
Now I want this to query from Remote servers by reading server names from an input file i.e. servers.txt and then generating output as mentioned along with the server name against which the query has been run.

Code: Select all

@echo off
 
set "OutputFile=test.csv"
 
for /F "skip=2 tokens=2-3 delims=," %%A in ('wmic computersystem get manufacturer^,model /FORMAT:csv') do (
  set "Manufacturer=%%A"
  set "Model=%%B"
)
 
for /F "skip=2 tokens=2 delims=," %%A in ('wmic bios get serialnumber /FORMAT:csv') do (
  set "Serial=%%A"
)
 
echo %Manufacturer%, %Model%, %Serial%>>%OutputFile%
----------------------------

#2 06 Aug 2013 19:03
sonda


Where is the question?

----------------------------

#3 06 Aug 2013 23:37
Giacomo
sonda wrote:

Where is the question?
I guess: "How to improve my script to work from a remote server?"

----------------------------

#4 07 Aug 2013 09:25
bluesxman


As is so often the case, the answer is in the manual.

You can do this with the /node switch:

Code: Select all

C:\>wmic /node /?

NODE - Specify which servers the alias will operate against.
USAGE:

/NODE:<machine id list>
NOTE: <machine id list> ::= <@filename | machine id> | <@filename | machine id> <,machine id list>

NOTE: Enclose the switch value in  double quotes, if the value contains special  characters like '-' or '/'.
So you could just point it at your servers.txt file.

Last edited by bluesxman (07 Aug 2013 09:26)

cmd | *sh | ruby | chef

----------------------------

#5 08 Aug 2013 16:25
prakash82x
bluesxman wrote:

As is so often the case, the answer is in the manual.

You can do this with the /node switch:

Code: Select all

    C:\>wmic /node /?

    NODE - Specify which servers the alias will operate against.
    USAGE:

    /NODE:<machine id list>
    NOTE: <machine id list> ::= <@filename | machine id> | <@filename | machine id> <,machine id list>

    NOTE: Enclose the switch value in  double quotes, if the value contains special  characters like '-' or '/'.
So you could just point it at your servers.txt file.
(Sorry if I could not post my question correctly)
Currently there is no wmi /node:servername syntax defined in the script so it is generating output from the local machine only on which it is executed, I want it to loop through the whole script and run against each remote server listed in the input file (need to define one text file containing target servers) and write the result in test.csv along with the node name/target server in a column.

----------------------------

#6 08 Aug 2013 16:28
prakash82x
Giacomo wrote:

sonda wrote:

Where is the question?

I guess: "How to improve my script to work from a remote server?"
Correct I want to run the script against remote servers and the server names should be picked up from a text file ..

----------------------------

#7 09 Aug 2013 10:55
bluesxman


Something like this then?

Code: Select all

set "OutputFile=test.csv"
for /f "usebackq tokens=*" %%S in ("servers.txt") do (
 
  for /F "skip=2 tokens=2-3 delims=," %%A in ('wmic computersystem get manufacturer^,model /FORMAT:csv /node:%%~S') do (
    set "Manufacturer=%%A"
    set "Model=%%B"
  )
 
  for /F "skip=2 tokens=2 delims=," %%A in ('wmic bios get serialnumber /FORMAT:csv /node:%%~S') do (
    set "Serial=%%A"
  )
 
  call echo:%%~S, %%Manufacturer%%, %%Model%%, %%Serial%%>>"%OutputFile%"
)
cmd | *sh | ruby | chef
Post Reply