批处理文件日志输出

Batch file logging output

我有一个批处理文件,它将 运行 并在 运行 时每行给出成功或不成功 echo。此批次中将有 1 条或 0 条成功行。我希望将成功的行记录到提到的 .txt 文件中。

@echo off
Title Kiosk Account Autologin Password Changer
::Query the registry, CHANGE the password, and report back if successful or unsuccessful
::These Kiosk accounts and passwords are pulled from the ICT SharePoint for East and West depots respectively
::Copy the "@%SystemRoot%" line and edit "User" and "Password" for each account as needed
echo Logged time = %time% %date%>> KioskPassword.txt
echo Searching for the active Kiosk account . . .
@%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultUserName" 2>NUL | %SystemRoot%\System32\findstr.exe /IRC:"^  *DefaultUserName  *REG_SZ  *K003566$" 1>NUL && %SystemRoot%\System32\reg.exe Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultPassword" /T "REG_SZ" /D "EMj88qmjTyzy" /F 1>NUL && echo Account K003566 password change successful <======================= Active Kiosk account || echo Account K003566 password change unsuccessful
@%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultUserName" 2>NUL | %SystemRoot%\System32\findstr.exe /IRC:"^  *DefaultUserName  *REG_SZ  *K004167$" 1>NUL && %SystemRoot%\System32\reg.exe Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /V "DefaultPassword" /T "REG_SZ" /D "xxn8YRryvuEK" /F 1>NUL && echo Account K004167 password change successful <======================= Active Kiosk account || echo Account K004167 password change unsuccessful

这部分&& echo Account K003566 password change successful <======================= Active Kiosk account是我想要记录的,如果成功的话。我该怎么做?

我不确定您为什么只记录成功的更改,IMO,您应该记录不成功的更改以及不存在的帐户。

这是我如何做到这一点的想法(实施一些想法来缩短大行,并删除一些重复)

@Rem Query the registry, change the password, and report to file.
@Echo Off
SetLocal EnableExtensions
Title Kiosk Account Autologin Password Changer
Set "Reg=%SystemRoot%\System32\reg.exe"
Set "Key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set "Val=DefaultUserName"
Set "FSt=%SystemRoot%\System32\findstr.exe"
Set "DPs=DefaultPassword"
Rem Call the Change function with your User and Password combinations.
(   Rem Example:
    Rem Call :Change "User" "Password"
    Rem
    Call :Change "K003566" "EMj88qmjTyzy"
    Call :Change "K004167" "xxn8YRryvuEK"
    Rem The above two Kiosk accounts and passwords are pulled from the ICT SharePoint
    Rem  for East and West depots respectively.
) 1>>"%~dp0KioskPassword.txt" 2>NUL
GoTo :EOF

:Change
Echo Searching for the %~1 Kiosk account . . . 1>CON
Echo %DATE% %TIME%
%Reg% Query "%Key%" /V "%Val%" 2>NUL | %FSt% /IRC:"^  *%Val%  *REG_SZ  *%~1$" 1>NUL && (
    %Reg% Add "%Key%" /V "%DPs%" /T "REG_SZ" /D "%~2" /F 1>NUL && (
        Echo  Account %~1 password change successful.
    ) || Echo  Account %~1 password change unsuccessful.
) || Echo  Account %~1 not found.
Exit /B

Searching…行可以看出,任何你想要Echo到控制台,你重定向到CON设备,否则它会被打印到[=14] =],(与批处理文件本身位于同一位置)。

请记住这一定是'Run as administrator'.