批处理STDOUT / STDERR重定向怪异

时间:2016-07-15 19:09:20

标签: windows batch-file cmd

在尝试在Windows 2012R2系统的配置阶段运行某些脚本时,我遇到了批处理脚本和重定向到日志的奇怪问题。

总体流程是这样的:

执行init.cmd:

init.cmd:

@echo off
echo init - foo
call start.cmd >start.log 2>&1
echo init - bar

start.cmd:

@echo off
echo start - foo
call test1.cmd >test1.log 2>&1
echo start - bar
call test2.cmd >test2.log 2>&1
echo start - baz

test1.cmd

@echo off
echo hello

test2.cmd

@echo off
echo aloha

我得到的start.log看起来像是:

start - foo

然后我得到一个看起来像的test1.log:

hello
start - bar

最后是一个看起来像的test2.log:

aloha
start - baz
init - bar

基本上,一旦我重定向STDOUT / STDERR,它会重定向到所有,直到我将其重定向到其他地方,此时,所有内容都会转到新的位置。我已经尝试了几种方法来进行重定向,调用子程序如调用:test1 >test1.log 2>&1并指导它们,或者包装在>start.log 2>&1 (commands)中,所有这些都做同样的事情。

我把这些示例脚本放在Windows 10机器上,但一切正常。

这里有什么我想念的吗?

编辑:

这是真正的脚本:

init.cmd:

@echo off
md %SYSTEMDRIVE%\logs
curl -sf -o %SYSTEMROOT%\temp\win_installer.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win_installer.cmd
call %SYSTEMROOT%\temp\win_installer.cmd >%SYSTEMDRIVE%\logs\installer.log 2>&1
if errorlevel 1 (
    echo Installer failed
    goto:eof
)
shutdown /r /t 0

win_installer.cmd:

@echo off
setlocal enabledelayedexpansion

echo Mounting Samba Share WINLOGS to L:
net use L: \\10.6.7.20\WINLOGS
if exist "L:" (
    md L:\example.acme.com
)

echo Beginning run stage
call :run
if errorlevel 1 (
    echo win2012r2.cmd failed to run completely
    goto :err
)
echo run stage completed successfully

if not exist "L:\example.acme.com" (
    echo Failed to create L:\example.acme.com
    echo This isn't fatal, so going to continue
    echo Logs will still be kept locally
)

echo Copying setupact.log
copy %SYSTEMROOT%\Panther\setupact.log L:\acme.example.com
echo Copying setuperr.log
copy %SYSTEMROOT%\Panther\setuperr.log L:\acme.example.com
echo Copying %SYSTEMDRIVE%\logs\win2012r2.log
copy %SYSTEMDRIVE%\logs\win2012r2.log L:\acme.example.com

echo Beginning netconf stage
call :netconf
if errorlevel 1 (
    echo Could not configure network
    goto :err
)
echo netconf stage completed successfully
goto :end

:run
echo Downloading install script for win2012r2.cmd
echo curl -sf -o %SYSTEMROOT%\temp\win2012r2.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win2012r2.cmd
curl -sf -o %SYSTEMROOT%\temp\win2012r2.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win2012r2.cmd

if ERRORLEVEL 1 (
    echo Could not download install cmd win2012r2.cmd
    goto err
)

echo Calling %SYSTEMROOT%\temp\win2012r2.cmd
call %SYSTEMROOT%\temp\win2012r2.cmd >%SYSTEMDRIVE%\logs\win2012r2.log 2>&1
if ERRORLEVEL 1 (
    echo Postprovisioning failed to run
    exit /b 1
)
goto :eof

:netconf
echo Downloading install script for win_netcfg.cmd
echo curl -sf -o %SYSTEMROOT%\temp\win_netcfg.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win_netcfg.cmd
curl -sf -o %SYSTEMROOT%\temp\win_netcfg.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win_netcfg.cmd

if ERRORLEVEL 1 (
    echo Could not download netcfg cmd win_netcfg.cmd
    goto err
)

echo Calling %SYSTEMROOT%\temp\win_netcfg.cmd
call %SYSTEMROOT%\temp\win_netcfg.cmd >%SYSTEMDRIVE%\logs\netconf.log 2>&1
if ERRORLEVEL 1 (
    echo Networking configuration failed to run
    exit /b 1
)
goto :eof

:err
echo win_installer.cmd failed to run
exit /b 1

:end
exit /b 0

我最终得到了一个c:\ logs \ installer.log:

Mounting Samba Share WINLOGS to L:
Beginning run stage
Downloading install script for win2012r2.cmd
curl -sf -o C:\Windows\temp\win2012r2.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win2012r2.cmd
Calling C:\Windows\temp\win2012r2.cmd

然后是c:\ logs \ win2012r2.log文件:

<all the stuff that win2012r2.cmd does>
run stage completed successfully <-- from here to end of the file should be installer.log
Copying setupact.log
Copying setuperr.log
Copying C:\logs\win2012r2.log
Beginning netconf stage
Downloading install script for win_netcfg.cmd
curl -sf -o C:\Windows\temp\win_netcfg.cmd http://10.6.7.20/cblr/svc/op/script/system/acme.example.com/?script=win_netcfg.cmd
Calling C:\Windows\temp\win_netcfg.cmd

然后是C:\ logs \ netconf.log:

< all the stuff netconf does >
netconf stage completed successfully <-- this should be installer.log

0 个答案:

没有答案