
컴퓨터 이름을 입력할 수 있는 배치 파일을 만들었는데, 이 파일을 통해 해당 장치에 로그인한 사람은 물론 해당 장치의 일련 번호도 알 수 있습니다. 나는 내 업무에 대해 이 두 가지 특정 정보를 꽤 많이 가져오므로 매우 도움이 됩니다.
내가 겪은 문제는 미용적인 문제에 가깝습니다. 그것은 나에게 정보를 제공하지만 어떤 이유로 결과를 두 번 제공합니다. 중복된 두 줄은 다음과 같습니다.
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 르 봄다음과 같이 현재 양식/명령으로 반환됩니다.wmic . ... /format:xml
2.당신이시스템 변수연관된 값 할당이 있는%COMPUTERNAME%
,변수 이름을 바꾸다...
삼.다른 흐름을 사용해 보세요.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 )