我正在 Windows 中编写一个脚本来获取系统信息,我想将每个命令的结果存储在一个 bat 变量中,然后将它们传递给 php 变量。
这是我尝试过的脚本:
for /f "tokens=1 delims=[" %a in ('ver') do SET OS=%a
for /f "skip=1 tokens=1 delims=|" %a in ('wmic os get Name') do SET version=%a
for /f "tokens=2 delims=[" %a in ('ver') do SET kernel= %a
for /f "skip=1 tokens=1 delims= " %a in ('wmic os get osarchitecture') do SET arq=%a
for /f "skip=1 tokens=1 delims= " %a in ('wmic computersystem get numberofprocessors') do SET numero_cpu=%a
for /f "skip=1 tokens=1 delims= " %a in ('wmic cpu get manufacturer') do SET fabricante_cpu=%a
for /f "skip=1 tokens=1,2 delims=" %a in ('wmic cpu get Name') do SET nombre_cpu=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic cpu get CurrentClockSpeed') do SET frequence_cpu=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic bios get name') do SET name_bios=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic bios get version') do SET version_bios=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic bios get serialnumber') do SET serial_bios=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic bios get manufacturer') do SET fabricant_bios=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic NIC get Description') do SET red=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic SOUNDDEV get productname') do SET audio=%a
for /f "skip=1 tokens=1 delims=" %a in ('wmic PATH Win32_videocontroller GET description') do SET video= %a
ECHO %OS%:%version%:%kernel%:%arq%:%numero_cpu%:%fabricante_cpu%:%nombre_cpu%:%frequence_cpu%:%name_bios%:%version_bios%:%serial_bios%:%fabricant_bios%:%red%audio%:%video%:
谢谢你!
答案1
wmic
行为:每个输出行以 结尾,0x0D0D0A
而不是 common 0x0D0A
。这会导致使用循环解析wmic
输出时出现问题for /F
:即使看似空荡荡的行包含前导0x0D
(回车) 因此第一个标记在那里被识别。
证明(注意转义的^,
逗号):
==>for /F "delims=" %g in ('wmic os get caption^, name /value') do @echo 1st="%g"
"st="
"st="
"st="Caption=Microsoft Windows 8.1
"st="Name=Microsoft Windows 8.1|C:\Windows|\Device\Harddisk1\Partition2
"st="
"st="
"st="
==>
可以使用子程序中使用的附加(嵌套)循环来解决:getwmic
(选择子程序方法来限制多重编码)。for
循环参数为
%%g
仅检索name=value
行(参见/value
开关和findstr
);%%G
检索所需的值(第二个标记%%H
)和删除结尾回车在返回的值中。
为该过程提供两行参数:getwmic
(参考%1
那里%2
):
- 一个简单
wmic
查询(用一对双引号括起来),查询某个类的唯一属性wmi
,例如"wmic os get Name"
; - 环境变量姓名查询的属性应该分配给,例如
version
。
下一个脚本仅给出了一些示例;所有set
命令仅echo
用于调试目的;echo
调试完成后立即删除所有命令。请注意
OS
变量已存在;因此考虑另一个名称(OSx
);- 使用
set "variable=value"
带双引号的语法来转义批处理脚本中具有特殊含义的某些字符,例如,,|
等,并确保没有(意外忘记的)尾随空格;&
<
>
|
^
管道通过in进行转义for /f ... /value ^|findstr "="')
。
剧本:
@ECHO OFF >NUL
for /f "tokens=1 delims=[" %%a in ('ver') do echo SET "OSx=%%a"
for /f "tokens=2 delims=[]" %%a in ('ver') do echo SET "kernel=%%a%"
call :getwmic "wmic os get Name" version
for /f "tokens=1 delims=|" %%a in ("%version%") do echo SET "version=%%a%
call :getwmic "wmic os get osarchitecture" arq
call :getwmic "wmic computersystem get numberofprocessors" numero_cpu
call :getwmic "wmic cpu get manufacturer" fabricante_cpu
call :getwmic "wmic cpu get Name" nombre_cpu
call :getwmic "wmic PATH Win32_videocontroller GET description" video
goto :yoursequel
:getwmic
for /F "delims=" %%g in ('%~1 /value ^|findstr "="') do (
for /F "tokens=1* delims==" %%G in ("%%g") do echo set "%~2=%%H"
)
exit /B
:yoursequel
ECHO %OS%:%version%:%kernel%:%arq%:%numero_cpu%:%fabricante_cpu%:%nombre_cpu%:%frequence_cpu%:%name_bios%:%version_bios%:%serial_bios%:%fabricant_bios%:%red%audio%:%video%:
资源(必读):
- (命令参考)Windows CMD 命令行的 AZ 索引
- (其他细节)Windows CMD Shell 命令行语法
- (
%~g
等%~1
专题页面)命令行参数(参数) - (
|
管材、管件&
等<
专用页面)重定向