批处理文件成功和错误日志 [英] Batch File Success and Error logging

查看:133
本文介绍了批处理文件成功和错误日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,一个批处理文件中,记录错误和批处理文件的成功?

我试图将文件从我的电脑200台(伟大工程)副本,但想知道哪些失败,原因失败(因为屏幕上消失的真快)。

 关闭@echo
如果[%1] == []转到用法
@echo映射L:%1 \\ C $
NET USE * /删除/ Y
NET USE L:\\\\%1 \\ C $密码/用户:%1 \\管理员如果ERRORLEVEL 1(
回声失败 NET USE L:\\\\%1 \\ C $密码/用户:%1 \\管理员)其他(
转到mappingError)> command.txt

::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::

  @echo复制链接文件到C:驱动器
复制C:\\ _ \\ CopyFileToHost \\ logoff.cmdL:\\

::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::

  @echo删除L:制图
NET USE L:/删除/ Y关闭@echo
GOTO:EOF:用法
@echo用法:%0主机
出口/ B 1:mappingError
@echo错误映射远程驱动器,退出
出口/ B 1


解决方案

首先,不要用

 如果[%1] == []转到用法

由于方括号没有这里的任何特殊含义。更好的是使用

 如果%〜1==goto语句用法

双引号字符有特殊的意义,因为它使我们可以比较含有1个或多个空格或1个或更多字符&放串;()[] {} ^ =;!' +,`〜

%〜1 表示周围去掉双引号第一个参数。通过在命令提示符窗口中运行,请参阅帮助输出呼叫/?了解更多信息。

二,阅读有关 Microsoft文章使用命令重定向操作符测试在批处理文件一个特定的错误级别了解批次code下文。

 关闭@echo
如果%〜1==goto语句用法
%SYSTEMROOT%\\ SYSTEM32 \\ NET.EXE使用L:/删除/ Y 2 - ; NUL

    回声映射L:到%〜1 \\ C $ ...
    %SYSTEMROOT%\\ SYSTEM32 \\ NET.EXE使用L:\\\\%〜1 \\ C $密码/用户:%〜1 \\管理员/持续性:无
    如果没有ERRORLEVEL 1(
        呼应复制文件X ...
        REM复制; X L:\\
        呼应断开网络驱动器L:...
        %SYSTEMROOT%\\ SYSTEM32 \\ NET.EXE使用L:/删除/ Y
    )
)> command.txt 2 - ;&放大器; 1
GOTO:EOF:用法
回声通话与计算机的参数名称此批处理文件。
回声。
暂停

写入错误消息的标准错误被追加为标准输出 2 - ;&放大器; 1 标准的消息是这在默认情况下写的。所有的一切都被写入标准输出被重定向到文件 command.txt 在当前目录,因此该文本文件包含正确的输出顺序标准和错误消息。

如果你想也写入文本文件中的命令,以更容易确定从哪个命令哪个错误或标准的消息传出,回声额外的命令了。

Is there a way, within a batch file, to log the error and success of the batch file?

I am trying to copy a file from my computer to 200 machines (works great) but want to know which fail and the reason it fails (because the screens go away really quick).

@echo off
if [%1]==[] goto usage
@echo mapping l: to %1\c$
net use * /delete /y
net use l: \\%1\c$ password /user:%1\administrator

if ERRORLEVEL 1 (
echo failed

 net use l: \\%1\c$ password /user:%1\administrator

) else (
goto mappingError

) > command.txt

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo copying link file to C: Drive
copy "c:\_\CopyFileToHost\logoff.cmd" l:\

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo deleting l: mapping
net use l: /delete /y

@echo off
goto :eof

:usage
@echo Usage: %0 hostname
exit /B 1

:mappingError
@echo Error mapping remote drive, exiting
exit /B 1

解决方案

First, don't use

if [%1]==[] goto usage

because the square brackets don't have any special meaning here. Better is using

if "%~1"=="" goto usage

The double quote character has a special meaning because it makes it possible to compare strings containing 1 or more spaces or 1 or more of the characters &()[]{}^=;!'+,`~.

%~1 means first argument with surrounding double quotes removed. See help output by running in a command prompt window call /? for more details.

Second, read the Microsoft articles about Using command redirection operators and Testing for a Specific Error Level in Batch Files to understand the batch code below.

@echo off
if "%~1"=="" goto usage
%SystemRoot%\System32\net.exe use L: /delete /y 2>nul
(
    echo Mapping L: to %~1\c$ ...
    %SystemRoot%\System32\net.exe use L: "\\%~1\c$" password /user:%~1\administrator /persistent:no
    if not errorlevel 1 (
        echo Copying the file X ...
        rem copy X L:\
        echo Disconnecting network drive L: ...
        %SystemRoot%\System32\net.exe use L: /delete /y
    )
) >command.txt 2>&1
goto :EOF

:usage
echo Call this batch file with name of computer as parameter.
echo.
pause

Error messages written to stderr are appended to stdout by 2>&1 to which standard messages are written by default. And everything written to stdout is redirected to file command.txt in current directory and therefore the text file contains standard and error messages in right output order.

If you want the commands also written to the text file to easier determine from which command which error or standard message comes, echo additionally the commands, too.

这篇关于批处理文件成功和错误日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆