问题描述
我正在尝试创建一个简单的脚本,该脚本将使用pclist.txt中的计算机名称,并获取序列号和型号并导出到csv文件.我只是似乎无法弄清楚为什么它不能在csv文件中打印序列号和型号.这是我的简单脚本.
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
推荐答案
您的代码可以正常运行-当 wmic
不会具有 CRCRLF
的丑陋行尾时.第一个 CR
成为变量的一部分. CR
的意思是将光标重新设置到行的开头",因此在同一行之后(在同一行中)回显的所有内容都将覆盖先前的内容.
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.
您可以使用另一个 for
循环来解决此问题,以剥离其余的 CR
.而且您不需要变量 SN
和 Model
;当您嵌套 for
循环时,可以直接使用 for
变量:
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的代码有效,因为所需的字符串是从一行的中间切出的,所以行的末尾无所谓)
(Compo's code works, because the desired string is cut from the middle of a line, so the line ending doesn't matter)
这篇关于从批处理脚本列表中远程获取序列号和型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!