我制作了这个使用wmic从远程计算机获取信息的脚本
set /p "user=write UserName="
set /p "password=write Password="
for /f "tokens=*" %%a in (ip.txt) do (
wmic /node:%%a /user:%user% /password:%password% computersystem get Name /format:table
wmic /node:%%a /user:%user% /password:%password% computersystem get Model /format:table
wmic /node:%%a /user:%user% /password:%password% computersystem get totalphysicalmemory /format:table
wmic /node:%%a /user:%user% /password:%password% cpu get Name /format:table
wmic /node:%%a /user:%user% /password:%password% path Win32_VideoController get Name /format:table
wmic /node:%%a /user:%user% /password:%password% os get Caption /format:table
wmic /node:%%a /user:%user% /password:%password% csproduct get identifyingnumber /format:table
wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenheight /format:table
wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenwidth /format:table
)>>a.csv
但输出有点奇怪,它与#34;输入"任何想法如何分开?
答案 0 :(得分:1)
您可以尝试以下代码段:
@echo off
setlocal EnableDelayedExpansion
set /P "user=write UserName="
set /P "password=write Password="
> "a.csv" (
for /F "usebackq tokens=*" %%a in ("ip.txt") do (
set "lineString="
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem get Name /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem get Model /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem get totalphysicalmemory /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% cpu get Name /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% path Win32_VideoController get Name /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% os get Caption /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% csproduct get identifyingnumber /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenheight /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenwidth /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
if defined lineString echo(!lineString:~1!
)
)
endlocal
exit /B
wmic
输出存储在变量,
中的lineString
分隔字符串中。为了捕获命令的输出,for /F
循环是完美的。但是,wmic
命令会生成Unicode输出,因此需要两个嵌套的for /F
循环才能进行正确的Unicode / ANSI转换;单个循环会在字符串中留下孤立的尾随回车字符。
wmic
的输出格式已从/format:table
更改为/value
以获得如下输出:Caption=Microsoft Windows 7 Enterprise
,因此for /F
循环可以采用=
作为分隔符并获得所需的值。
除此之外,我还改变了以下内容:
在您的代码中,您分别重定向每个循环迭代的输出。为了避免这种情况,()
已放置在(最外层)for /F
循环周围,因此整个数据会重定向到a.csv
。因此,重定向运算符已从append-type >>
更改为(over-)write-type >
。
Delayed expansion,这是在lineString
循环或for
之间的代码块中修改和读取变量(()
)时所必需的。