Remotely get serial number and model from list in batch script

二次信任 提交于 2021-02-11 14:25:13

问题


I am trying to create a simple script that will use computer name from pclist.txt and get serial number and model and export to csv file. I just can not seem to figure out why it does not print the serial number and model in the csv file. Here is my simple script.

@echo off
for /f %%i in (pclist.txt) do (
     for /f "tokens=2 delims==" %%M in ('wmic /node:%%i csproduct get name /value') do set "Model=%%M"
     for /f "tokens=2 delims==" %%I in ('wmic /node:%%i bios get serialnumber /value') do set "SN=%%I"
     echo %%i, %SN%, %Model% >> list.csv
     )
pause

回答1:


I may have misunderstood what your intention was, but I would have expected the following to be the general way of achieving your intended task.

@%__AppDir__%wbem\WMIC.exe /Node:@pclist.txt /FailFast:1000 /Output:list.csv CSProduct Get Name /Format:CSV
@%__AppDir__%wbem\WMIC.exe /Node:@pclist.txt /FailFast:1000 /Append:list.csv BIOS Get SerialNumber /Format:CSV
@Pause

If you wanted to try to just output each item along side the node name separated by commas, then perhaps this will suffice:

@Echo Off
SetLocal EnableExtensions
(
    For /F UseBackQDelims^=^ EOL^= %%G In (
        "pclist.txt"
    ) Do (
        For /F Tokens^=6Delims^=^" %%H In (
            '%__AppDir__%wbem\WMIC.exe /Node:%%G /FailFast:1000 CSProduct Get Name /Format:MOF'
        ) Do (
            For /F Tokens^=6Delims^=^" %%I In (
                '%__AppDir__%wbem\WMIC.exe /Node:%%G /FailFast:1000 BIOS Get SerialNumber /Format:MOF'
            ) Do Echo %%G,%%H,%%I
        )
    )
)>"list.csv"
Pause



回答2:


Your code would run fine - when wmic wouldn't have that ugly line ending of CRCRLF. The first CR becomes part of your variable. CR means "set the cursor back to the beginning of the line", so everything echoed after (in the same line) overwrites the previous content.

You can solve that with another for loop to strip the remaining CR. And you don't need the variables SN and Model; when you nest the for loops, you can use the for variables directly instead:

@echo off
del list.csv 2>nul
for /f %%i in (pclist.txt) do (
  for /f "tokens=2 delims==" %%M in ('wmic /node:"%%i" csproduct get name /value') do for %%m in (%%M) do (
    for /f "tokens=2 delims==" %%S in ('wmic /node:"%%i" bios get serialnumber /value') do for %%s in (%%S) do (
      >>list.csv echo %%i, %%s, %%m
    )
  )
)
type list.csv
pause

(Compo's code works, because the desired string is cut from the middle of a line, so the line ending doesn't matter)



来源:https://stackoverflow.com/questions/61961307/remotely-get-serial-number-and-model-from-list-in-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!