获取计算机规格,并自动保存结果

问题描述

我在每个命令后都添加YOUR-COMMAND | Out-File -FilePath c:\PATH\TO\FOLDER\OUTPUT.txt,但仍然没有使用pc规范创建文件。将脚本保存在批处理文件中后,会弹出cmd并显示规格,但是我需要将其自动保存在桌面上。

@echo off
echo Checking your system info,Please wait...
ipconfig | find /i "IPv4" 
systeminfo | findstr /c:"Host Name" 
systeminfo | findstr /c:"OS Name" 
systeminfo | findstr /c:"System type" 
systeminfo | findstr /c:"Domain" 
systeminfo | findstr /c:"OS Version" 
systeminfo | findstr /c:"System Model"
systeminfo | findstr /c:"Total Physical Memory" 
systeminfo | findstr /c:"System Manufacturer" 

echo.
echo Service Tag:
wmic bios get serialnumber

echo.
echo Hard Drive Space:
wmic diskdrive get Name,Manufacturer,Model,InterfaceType,MediaType,SerialNumber,size

echo.
echo cpu:
wmic cpu list brief

echo.
echo Memory:
wmic MemoryChip get BankLabel,Capacity,MemoryType,TypeDetail,Speed

echo.
echo BaseBoard:
wmic baseboard get product,version,serialnumber

pause

解决方法

按照Compo的建议,一种更有效的方法是在Systeminfo的单个实例上执行多个findstr命令。以下内容使用宏来制定和格式化成功的findstr命令的输出,并通过子字符串修改将要找到的每个字符串提供给宏。运行系统信息命令的For /F循环被括在括号中,以使能够重定向到完整命令输出的.txt文件,而不是执行多次写操作。

关于宏用法的相同方法可以应用于wmic命令。

@Echo Off & Setlocal DISABLEdelayedexpansion
@Echo Off & Setlocal DISABLEdelayedexpansion
CD "%~dp0"
 Set "FSTR=( Echo/"%%~A" | %__AppDir__%findstr.exe /LIC:"$Str" > Nul 2> Nul ) && (Set "$$Str=%%~A" & <Nul Set /P "=$Str=" & <Nul Set/P "=!$$Str:*:=!" &Echo/)"
 Set "Wmic=For %%n in (1 2)Do if %%n==2 ( >>"%~n0_Outfile.txt" Echo/!Field!:&For /F "UsebackQ Delims=" %%G in (`" wmic Attrib /Format:Value 2^> Nul "`)Do If Not "%%~G" == "" For /F "Delims=" %%i in ("%%~G")Do ( 1>> "%~n0_outfile.txt" Echo/%%~i))Else Set Field="
Setlocal EnableDelayedExpansion
(For /F "Delims=" %%A in ('%__AppDir__%systeminfo.exe /FO List')Do (
 %FSTR:$Str=Host Name%
 %FSTR:$Str=OS Name%
 %FSTR:$Str=System type%
 %FSTR:$Str=System Model%
 %FSTR:$Str=Total Physical Memory%
 %FSTR:$Str=System Manufacturer%
))>"%~n0_outfile.txt"
 %WMIC:Attrib=bios get serialnumber%Bios
 %WMIC:Attrib=diskdrive get Name,Manufacturer,Model,InterfaceType,MediaType,SerialNumber,size%Hard Drive
 %WMIC:Attrib=cpu list brief%Cpu
 %WMIC:Attrib=MemoryChip get BankLabel,Capacity,MemoryType,TypeDetail,Speed%Memory
 %WMIC:Attrib=baseboard get product,version,serialnumber%BaseBoard
Type "%~n0_outfile.txt"
Endlocal & Endlocal
,

这是一个有趣的问题。 我不了解许多pc specs命令,但是我知道如何将输出保存到文件中。您需要在代码的每一行后附加>> OUTPUT.txt

我已经为你做到了。这是代码!

@echo off
echo Checking your system info,Please wait... >> OUTPUT.txt
ipconfig | find /i "IPv4" >> OUTPUT.txt
systeminfo | findstr /c:"Host Name" >> OUTPUT.txt
systeminfo | findstr /c:"OS Name" >> OUTPUT.txt
systeminfo | findstr /c:"System type" >> OUTPUT.txt
systeminfo | findstr /c:"Domain" >> OUTPUT.txt
systeminfo | findstr /c:"OS Version" >> OUTPUT.txt
systeminfo | findstr /c:"System Model" >> OUTPUT.txt
systeminfo | findstr /c:"Total Physical Memory" >> OUTPUT.txt
systeminfo | findstr /c:"System Manufacturer" >> OUTPUT.txt

echo. >> OUTPUT.txt
echo Service Tag: >> OUTPUT.txt
wmic bios get serialnumber >> OUTPUT.txt

echo. >> OUTPUT.txt
echo Hard Drive Space: >> OUTPUT.txt
wmic diskdrive get Name,size >> OUTPUT.txt

echo. >> OUTPUT.txt
echo CPU: >> OUTPUT.txt
wmic cpu list brief >> OUTPUT.txt

echo. >> OUTPUT.txt
echo Memory: >> OUTPUT.txt
wmic MemoryChip get BankLabel,Speed >> OUTPUT.txt

echo. >> OUTPUT.txt
echo BaseBoard: >> OUTPUT.txt
wmic baseboard get product,serialnumber >> OUTPUT.txt

pause