简体   繁体   English

验证(错误级别)批处理脚本命令并发送带有其输出的邮件

[英]Verify (errorlevel) batch script commands and send mail with their output

I have more batch files(cmd) as follows , that is executing some commands and print the output of their errorlevel code in a file and send an email to me with the output of the file : 我还有更多的批处理文件(cmd),如下所示,它们正在执行一些命令,并将错误级别代码的输出打印在文件中,并通过文件输出向我发送电子邮件:

@echo off

set RESEXE1=0
set RESEXE2=0

SET MyPath=D:\test\client
SET PATH=%MyPath%\;%PATH%
SET PATH=%MyPath%\schema;%PATH%
SET PATH=%MyPath%\xml;%PATH%
SET PATH=%MyPath%\xsl;%PATH%
SET PATH=%MyPath%\DashboardPlugins;%PATH%

:BEGIN

cd /d %MyPath%

set RESEXE1=%ERRORLEVEL%
echo RESEXE1 : %RESEXE1% > D:\Temp\logs\test.txt

START "test" /MIN test.exe -UEOD /ccu:file:test2.config /pcu:file:D:\scripts\test.config /p:Common/Log/Path=D:\Misys\logs\batch\test /p:Common/Log/Verbosity=Warning -S'Report Processing':":0 

set RESEXE2=%ERRORLEVEL%
echo RESEXE2 : %RESEXE2% >> D:\Temp\logs\test.txt

:ENDBATCH

if %RESEXE1% == 0 set RESEXE=0

if %RESEXE1% NEQ 0 set RESEXE=1

if %RESEXE2% == 0 set RESEXE=0

if %RESEXE2% NEQ 0 set RESEXE=1

echo RESEXE : %RESEXE% >> D:\Temp\logs\test.txt

echo %~n0%~x0 >> D:\Temp\logs\test.txt

IF %RESEXE% NEQ 0  Powershell.exe -executionpolicy remotesigned -File D:\Temp\aaa.ps1

The second part is my sendmail script : 第二部分是我的sendmail脚本:

$From = "them@test.com"
$To = "me@test.com"
#$Cc = "he@test.com"
$Subject = $scname
$Body = Get-Content -Path D:\Temp\logs\test.txt
#$Attachments = D:\Temp\test.txt
$SMTPServer = "test.com"
$SMTPPort = "25"
Send-MailMessage -From $From -to $To -Subject "[test][test] $Subject" -Body  ($body | Out-String) -SmtpServer $SMTPServer -port $SMTPPort –DeliveryNotificationOption OnSuccess

My question is , how can i declare a dynamic variable on batch(cmd) scripts, for every output file (ex: test.txt) that is generated with the errorcode and other info and use it in $body variable from sendmail script to output the content in email . 我的问题是,对于每个使用错误代码和其他信息生成的输出文件(例如:test.txt),如何在batch(cmd)脚本上声明动态变量,并在sendbody脚本的$ body变量中使用它,以进行输出电子邮件中的内容。

Batch file : 批处理文件:

@echo off

set RESEXE1=0
set scname=%~n0%~x0
set scname2=%~n0.txt
set pathlog=D:\Temp\logs
set pathfilelog=%pathlog%\%scname2%

:BEGIN

cd h: > %pathfilelog% 2>&1

set RESEXE1=%ERRORLEVEL%
echo RESEXE1 : %RESEXE1% >> %pathfilelog%

:ENDBATCH

 if %RESEXE1% == 0 set RESEXE=0

 if %RESEXE1% NEQ 0 set RESEXE=1

echo RESEXE : %RESEXE% >> %pathfilelog%

 echo %~n0%~x0 >> %pathfilelog%

 IF %RESEXE% NEQ 0  Powershell.exe -executionpolicy remotesigned -File D:\Temp\aaa.ps1 "%scname%" "%scname2%" "%pathfilelog%"

mail script file : 邮件脚本文件:

$scname=$args[0] 
$scname2=$args[1]
$pathfilelog=$args[2]  

$From = "test@test.com"
$To = "test@test.com"
#$Cc = "test@test.com"
$Subject = $scname
$Body = Get-Content -Path $pathfilelog
#$Attachments = D:\Temp\test.txt
$SMTPServer = "test.test.com"
$SMTPPort = "25"
Send-MailMessage -From $From -to $To -Subject "[test][test] $Subject - Failed" -Body  ($body | Out-String) -SmtpServer $SMTPServer -port $SMTPPort –DeliveryNotificationOption OnSuccess

That solved my problem , ty guys . 伙计们,这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM