[英]Powershell: Sending an email if output of command differs from the array of 3 strings that is expected
this is what i got so far 这就是我到目前为止
if the command places a forth line of output then the regex shouldn't be match, but it does for me. 如果命令放置第四行输出,则不应该匹配正则表达式,但对我来说匹配。
any ideas? 有任何想法吗?
$From = "cody@tech.com"
$To = "cody@tech.com"
$Subject = "hello"
$Body = "body"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Attachment = "c:\checkpointlog.txt"
$RE = [regex]"(?smi)^Checkpointing to checkpoint.*^MD5 \(checkpoint.*^Rotating D:\\Perforce\\Server\\journal to journal.*?$"
$output = p4d -jc
$output | add-content c:\checkpointlog.txt
if ($output -notmatch $RE) {
$param = @{
From = $From
To = $To
Subject = $Subject
Body = $Body
SmtpServer = $SMTPServer
port = $SMTPPort
Usessl = $True
Credential = $cred
Attachments = $Attachment
}
Send-MailMessage @param
Write-Host 'unexpected value, email sent'
exit
}
else {
Write-Host 'continuing script'
}
the output of a Perforce Helix Server command p4 -jc should always be like the following 3 lines: Perforce Helix Server命令p4 -jc的输出应始终类似于以下三行:
Checkpointing to checkpoint.22...
MD5 (checkpoint.22) = F561234wer2B8E5123456767745645616D
Rotating D:\Perforce\Server\journal to journal.21...
I would like to use a regex in an if statement so that if the output doesn't match the 3 line string below then an email is sent with the log file for us to inspect. 我想在if语句中使用一个正则表达式,以便如果输出与下面的3行字符串不匹配,则将发送包含日志文件的电子邮件供我们检查。
Checkpointing to checkpoint.*
MD5 (checkpoint.*
Rotating D:\Perforce\Server\journal to journal.*
I am hoping to use a wild card * to account for the incremental number 我希望使用通配符*来说明增量数
Any ideas would be great! 任何想法都很棒!
Your regex tries to match multiple lines , and therefore needs a single multi-line string as input . 您的正则表达式会尝试匹配多行 ,因此需要一个多行字符串作为input 。
Capturing an external program's output returns an array of strings (lines) . 捕获外部程序的输出将返回一个字符串数组 (行) 。
Using an array of string as the LHS of the -match
operator causes PowerShell to match the regex against each string individually . 使用串的阵列作为的LHS
-match
操作者使PowerShell来个别地匹配针对每个字符串中的正则表达式。
Therefore, join the lines output by p4d
with newlines to form a single multi-line string , so that your regex can match multiple lines: 因此, 将
p4d
输出的p4d
与换行符p4d
在一起以形成单个多行字符串 ,以便您的正则表达式可以匹配多行:
$output = (p4d -jc) -join "`n"
Additionally, if you want to make sure that your regex matches the entire input, not just a substring , restructure you regex as follows: 此外,如果要确保正则表达式与整个输入匹配,而不仅仅是子字符串 ,请按以下方式重新构造正则表达式:
Remove in-line option m
(multi-line) so that ^
and $
truly only match the very start and end of the multi-line string 删除内联选项
m
(多行),以使^
和$
仅真正匹配多行字符串的开头和结尾
Remove in-line option s
, so that .
删除行选项
s
,这样.
doesn't match newlines, and match newlines explicitly with \\n
(instead of ^
). 与换行符不匹配,而是用
\\n
(而不是^
)显式匹配换行符。
$RE = '(?i)^Checkpointing to checkpoint.*\nMD5 \(checkpoint.*\nRotating D:\\Perforce\\Server\\journal to journal.*$'
$RE = [regex]"(?smi)^Checkpointing to checkpoint.*^MD5 \(checkpoint.*^Rotating D:\\Perforce\\Server\\journal to journal.*?$"
$output = p4d -jc
$output | add-content c:\checkpointlog.txt
if ($output -notmatch $RE) {
$param = @{
From = $From
To = $To
Subject = $Subject
Body = $Body
SmtpServer = $SMTPServer
port = $SMTPPort
Usessl = $True
Credential = $cred
Attachments = 'c:\checkpointlog.txt'
}
Send-MailMessage @param
exit
}
I know you wanted to use RegEx but maybe this would help? 我知道您想使用RegEx,但这可能会有帮助吗? It might not be the greatest but it should work.
它可能不是最大的,但应该可以工作。
$file='checkpointlog.txt'
$line1Expected="Checkpointing to checkpoint*"
$line2Expected="MD5 (checkpoint*"
$line3Expected="Rotating D:\Perforce\Server\journal to journal*"
$line1Actual=Get-Content($file) | Select -Index 0
$line2Actual=Get-Content($file) | Select -Index 1
$line3Actual=Get-Content($file) | Select -Index 2
if($line1Actual -like $line1Expected`
-and $line2Actual -like $line2Expected`
-and $line3Actual -like $line3Expected){
}else{
echo 'send mail'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.