
我創建了一個批次文件,使我能夠輸入計算機名稱,它會告訴我誰登入了該設備以及該設備的序號。我為我的工作收集了很多這兩個特定的信息,所以它非常有幫助。
我遇到的問題更多的是表面上的問題。它給了我信息,但由於某種原因它給了我兩次結果。傳回的兩行重複內容是:
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 BOM以當前的形式/命令返回您,例如使用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 )