【问题标题】:批处理文件记录输出
【发布时间】:2022-01-23 01:52:42
【问题描述】:

我有一个批处理文件,它将运行并在运行时为每行提供成功或不成功的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

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

【问题讨论】:

    标签: batch-file logging


    【解决方案1】:

    我不确定您为什么只记录成功的更改,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 设备,否则它将被打印到KioskPassword.txt,(与批处理文件本身)。

    请记住,这必须是“以管理员身份运行”。

    【讨论】:

    • 当前设置的方式,当不成功时,批处理会转到下一行查询。完整的文件有 250 行,每个用户名一个,我不想在每次运行时都填写 249 行失败的日志。另外,如果我可以缩短它并删除重复,那就太好了,因为现在每一行都是副本,只有用户和密码更改。我认为 CSV 将是最有效的,但我还不知道如何实现这些。
    • 什么!!!您的问题是关于记录输出的,我已经在上面的代码中向您展示了如何做到这一点,以及使用 1&gt;CON 排除行。你现在还指望我重写你的整个任务吗?我已经将您重复的reg query/reg add 条件行减少为简单地提供名称和密码作为函数/标签的参数。如果您不介意将它们复制到带括号的call 部分,那么您需要将该部分更改为for /f 循环,该循环从文件中读取名称和密码信息,并将它们直接传递给:Change功能。
    • 不,我不希望您重写所有内容。感谢您提供的帮助,我学到了很多东西。我只是在告诉我的批处理文件的当前状态以及我需要做些什么来改进它。这对我来说是全新的,我正在努力学习,一路接受批评和反馈。
    • 我想告诉你的是,我已经在我的回答中向你展示了,不仅仅是如何记录输出,(你的问题),还有如何模块化它并尽量减少您需要的输入行数。您接下来打算做什么与您提出的问题以及我提供的答案无关。我的回答中是否有一些未涵盖您的日志记录问题的内容?
    • 我想我理解你编写的脚本。它将我当前字符串的各个部分分配给一个变量,并使用这些变量来增加模块化并使行更短。使用con 和管道| 有什么区别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多