0
votes

I have developed a power shell script that will check if it matches atleast one keyword in a file and if not matches I will send an email to the users. Now I have to check the list of files in particular folder with .yaml extension and do the same task that I have developed. If none of the file matches the keywords I would terminate the script

I can get the list of files that I required from the path using Get-ChildItem "D:\Users\Phase1\BridgeConnect*" -recurse -exclude product.yaml I want to check each of the listed files with the keywords matching atleast one and if any file doesn't contain the words I should terminate it

$smtpServer = "smtp.xxxxx.com"
$smtpFrom = "hxxxx"
$smtpTo = "xxxxx"
$messageSubject = "Please configure your API with atleast $words"
$messageBody = "Your product is not published"
$SEL = Select-String -Path .\aventionsearch_1.0.0.yaml -Pattern $words
if ($SEL -ne $null)
{
echo matched
}
else
{
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
Write-Error 'The key words are not matched in this file or may not exist' -ErrorAction Stop
}
2

2 Answers

0
votes
$smtpServer = "smtp.xxxxx.com"
$smtpFrom = "hxxxx"
$smtpTo = "xxxxx"
$messageSubject = "Please configure your API with atleast $words"
$messageBody = "Your product is not published"

$files = Get-ChildItem "D:\Users\Phase1\BridgeConnect*" -recurse -exclude product.yaml | select -ExpandProperty name

foreach ($file in $files) {
    $SEL = Select-String -Path .\aventionsearch_1.0.0.yaml -Pattern $words
    if ($SEL -ne $null)
    {
    echo matched
    }
    else
    {
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
    Write-Error 'The key words are not matched in this file or may not exist' -ErrorAction Stop
    }
}
0
votes

You can use the following:

$smtpServer = "smtp.xxxxx.com"
$smtpFrom = "hxxxx"
$smtpTo = "xxxxx"
$messageSubject = "Please configure your API with atleast $words"
$messageBody = "Your product is not published"
$words = 'keywords1|keywords2|keywords3'
$files = Get-ChildItem "D:\Users\Phase1\BridgeConnect" -Recurse -Exclude product.yaml

foreach ($file in $files) {
    $SEL = Select-String -Path $file.FullName -Pattern $words
    if ($SEL)
    {
        "matched"
    }
    else
    {
        $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
        Write-Error 'The key words are not matched in this file or may not exist' -ErrorAction Stop
    }
}

Explanation:

Select-String -Path parameter can accept an array of paths. If you are okay with not having a result for a specific file, then you can avoid the loop all together.

Select-String -Pattern parameter defaults to using regex for matching. So if you want a list of key words, you will want to separate those words with the | character. You can see this in the $words variable.

Since the -Recurse parameter is used with Get-ChildItem, asterisks are not needed to represent whole leaf file and container names.