如何在 bat 文件中设置变量并将其传递给 php 变量?

如何在 bat 文件中设置变量并将其传递给 php 变量?

我正在 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%:

资源(必读):

相关内容