我创建了一个批处理文件,它允许我输入计算机名称,然后它会告诉我谁登录了该设备以及该设备的序列号。我经常在工作中使用这两个特定的信息,所以它非常有用。
我遇到的问题更多是外观问题。它向我提供了信息,但出于某种原因,它向我提供了两次结果。重复返回的两行是:
echo User logged on to %computerName% is: %%i
echo Serial number of %computerName% is: %%j
有人知道原因吗?有人可以提供一些意见吗?这是完整的脚本。
@echo off
:beginning
set /p computerName=Enter the computer name:
for /f "skip=1 delims=" %%i in ('wmic /node:"%computerName%" computersystem get username') do (
echo User logged on to %computerName% is: %%i
)
for /f "skip=1 delims=" %%j in ('wmic /node:"%computerName%" bios get serialnumber') do (
echo Serial number of %computerName% is: %%j
)
echo.
:menu
echo Choose one of the following options:
echo 1. Run the script again
echo 2. Turn to command prompt
echo.
set /p menuOption=Enter your choice:
if "%menuOption%"=="1" goto start
if "%menuOption%"=="2" goto end
:start
echo.
goto main
:end
cmd /k
:main
goto beginning
答案1
1.尝试通过过滤wmic
选项来返回更简单的编码输出字符串,不同于UCS-2 LE 物料清单返回当前表单/命令,例如使用wmic . ... /format:xml
2.请注意,您正在使用系统变量具有关联的值分配%COMPUTERNAME%
,更改变量的名称...
3.考虑使用不同的流程if %integer% EQU %integer%
有条件goto :label
goto another :label
,这将有助于可视化用户的选择所带来的可能的命令/交互的流程。
@echo off
:beginning
set /p "_computerName=Enter the computer name: "
for /f tokens^=4delims^=^>^< %%i in ('
wmic /node:"%_computerName%" computersystem get username /format:xml^|find "/VALUE"
')do echo User logged on to %_computerName% is: %%~i
for /f tokens^=4delims^=^>^< %%i in ('
wmic /node:"%_computerName%" bios get serialnumber /format:xml^|find "/VALUE"
')do echo Serial number of %_computerName% is: %%~i
:menu
echo.
echo Choose one of the following options:
echo 1. Run the script again
echo 2. Turn to command prompt
echo.
set /p "_menuOption=Enter your choice: "
if %_menuOption% equ 2 (
"%ComSpec%" /d /q /k
)else if %_menuOption% equ 1 (
set _computerName=
goto :beginning
)else timeout 5 | echo\Invalid choice!
答案2
我找到了一个解决方案,下面是更正后的行。
我必须补充一下^| findstr /r /v "^$"
for /f "skip=1 delims=" %%i in ('wmic /node:"%computerName%" computersystem get username ^| findstr /r /v "^$"') do ( echo User logged on to %computerName% is: %%i )
for /f "skip=1 delims=" %%j in ('wmic /node:"%computerName%" bios get serialnumber ^| findstr /r /v "^$"') do ( echo Serial number of %computerName% is: %%j )