
コンピューター名を入力する機能を提供するバッチ ファイルを作成しました。これにより、デバイスにログインしているユーザーとそのデバイスのシリアル番号がわかります。仕事でこの 2 つの特定の情報を頻繁に取得するため、非常に役立ちます。
私が遭遇した問題は、見た目に関するものです。情報は表示されますが、何らかの理由で結果が 2 回表示されます。重複して返される 2 行は次のとおりです。
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 )