逗号分隔的 wmic 命令
comma separated wmic commands
我制作了这个使用 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
但是输出有点奇怪,它用 "enter" 分隔,知道如何用逗号分隔吗?
您可以尝试以下代码片段:
@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 转换;单个循环会在字符串中留下孤立的尾随 carriage-return 个字符。
wmic
的输出格式已从 /format:table
更改为 /value
以获得如下输出:Caption=Microsoft Windows 7 Enterprise
,因此 for /F
循环可以把=
作为分隔符,得到想要的值。
除此之外,我还更改了以下内容:
在您的代码中,您分别重定向了每个循环迭代的输出。为了避免这种情况,()
被放置在 (outer-most) for /F
循环中,所以整个数据被重定向一次到 a.csv
。因此,重定向运算符已从 append-type >>
更改为 (over-)write-type >
.
Delayed expansion 已启用,这是在 for
循环或 ()
之间的代码块中修改和读取变量 (lineString
) 时所必需的。 32=]
我制作了这个使用 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
但是输出有点奇怪,它用 "enter" 分隔,知道如何用逗号分隔吗?
您可以尝试以下代码片段:
@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 转换;单个循环会在字符串中留下孤立的尾随 carriage-return 个字符。
wmic
的输出格式已从 /format:table
更改为 /value
以获得如下输出:Caption=Microsoft Windows 7 Enterprise
,因此 for /F
循环可以把=
作为分隔符,得到想要的值。
除此之外,我还更改了以下内容:
在您的代码中,您分别重定向了每个循环迭代的输出。为了避免这种情况,()
被放置在 (outer-most) for /F
循环中,所以整个数据被重定向一次到 a.csv
。因此,重定向运算符已从 append-type >>
更改为 (over-)write-type >
.
Delayed expansion 已启用,这是在 for
循环或 ()
之间的代码块中修改和读取变量 (lineString
) 时所必需的。 32=]