0

在下面的代码中,错误级别检查永远不会起作用。因此,如果发生故障,则不会发出任何电子邮件。Powershell 命令是正确的,并且在此构造之外工作。我也试过直接检查: If %ERRORLEVEL% NEQ 0

但这也行不通。

有任何想法吗?谢谢!

@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^&            right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "dt=%mm%-%dd%-%yyyy%"

"LogParser.exe" "SELECT * INTO tblIisLog FROM \\iislogs\%dt%\*.log" -i:iisw3c -o:SQL -    oConnString:"Driver=SQL Server;Server=servername; Database=IISDB;Trusted_Connection=yes" -    createTable:ON
SET /a RC=%ERRORLEVEL%

IF %RC% NEQ 0
(

   powershell.exe -executionpolicy unrestricted -command "send-mailmessage -from     'me@abc.com' -to 'me@abc.com' -subject 'test' -body 'testing' -smtpServer 'smtpserv.com'"

)
4

2 回答 2

0

假设 LogParser.exe 确实返回了一个错误级别,这应该可以工作并且启动起来更简洁:

LogParser.exe "SELECT blah, blah, blah..."
if errorlevel 1 powershell.exe -executionpolicy ...
于 2014-07-01T22:47:34.703 回答
0

我已经阅读了一篇文章,logparser该文章声称它确实errorlevel在错误时返回非零值。

我怀疑已经对用户变量进行了分配errorlevelif errorlevel 1 ...由于构造在实际 上运行,因此会起作用,if errorlevelerrorlevel使用的任何方法都%errorlevel%首先检查用户设置的变量。将任何值分配给errorlevel或其密友(date,timerandom)可能会导致这种意外结果(但在调试时可能很有用 - 您可以将这些“变量”之一设置为已知值)

测试该理论(可能是也可能不是治愈方法)的真正简单的 2 秒方法是简单地包含以下行

set "errorlevel="

就在logparser通话之前。比任何技术分析和论证都要快得多。

于 2014-07-01T23:14:20.240 回答