0

wrote this small part of code to check if file exist and contains string pattern

try {
$SEL = Select-String -Path \\$serversPing\c$\Scripts\compare_result.txt -Pattern "no differences encountered" -ErrorAction SilentlyCOntinue
}catch{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
}
Finally {
if ($SEL | Test-Path -ErrorAction SilentlyContinue){
    #write-host $serversPing $SEL.Pattern
    #write-host $serversPing $SEL
    if ($SEL.Pattern -eq "no differences encountered")
    {
       $SoftCheckResult = "ok"
    }
    else
    {
        $SoftCheckResult ="Verify"
    }

}
else{
    $SoftCheckResult =  "NotInScope"
}
}

But, it does not do what it should. First of all it partially recognize that path exist and secondly it does partially recognize pattern in txt file. Can you please help me?

I suspect that PATTER is partially recognizable on multiply server.(whitepaces etc) even so how to skip that?

Strange think is that it does not see that pattern is missing in file, it return NotinScope instead Verify Below file without this pattern

enter image description here

And below you can see normal pattern

enter image description here

4 Answers 4

2

Since you use plural in $serversPing, I suspect this variable comes from an earlier part of your code and contains a COLLECTION of servers.

I would change the order of checks and start with a test to see if the file exists on that server or not:

# As you mentioned a possible whitespace problem the pattern below uses regex `\s+` so multiple whitespace characters are allowed betwen the words.
$pattern = "no\s+differences\s+encountered"
foreach ($server in $serversPing) {
    if (Test-Connection $server -Count 1 -Quiet) {
        $filePath = Join-Path -Path "\\$server" -ChildPath 'c$\Scripts\compare_result.txt'
        if (Test-Path $filePath -PathType Leaf) {
            # -Quiet:       Indicates that the cmdlet returns a Boolean value (True or False), instead of a MatchInfo object. 
            #               The value is True if the pattern is found; otherwise, the value is False.
            if (Select-String -Path $filePath -Pattern $pattern -Quiet) {
                Write-Host "Pattern '$pattern' found in '$filePath'"
                $SoftCheckResult = "ok"
            }
            else {
                Write-Host "Pattern '$pattern' not found in '$filePath'"
                $SoftCheckResult = "Verify"
            }
        }
        else {
            Write-Host "File '$filePath' not found"
            $SoftCheckResult ="NotInScope"
        }
    }
    else {
            Write-Host "Server '$server' is off-line."
            $SoftCheckResult ="OffLine"
    }
}

I added a Test-Connection in the foreach loop to first see if the server is online or not. If you have checked that before and the $serversPing variable contains only servers that are online and reachable, you may skip that.

Sign up to request clarification or add additional context in comments.

Comments

0

Concerning the -Path of the Select-String cmdlet, you should put the value between "" :

$SEL = Select-String -Path "\\$serversPing\c$\Scripts\compare_result.txt" -Pattern "no differences encountered" -ErrorAction SilentlyCOntinue

EDIT

This should do the trick :

try {
    $SEL = Select-String -Path \\$serversPing\c$\Scripts\compare_result.txt -Pattern "no differences encountered" -ErrorAction SilentlyCOntinue
}catch{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
}
Finally {
    if ($SEL){
        $SoftCheckResult = "ok"
    }
    else
    {
        $SoftCheckResult ="Verify"
    }
}

4 Comments

Strange think is that it is not see that pattern is missing in file, it return NotinScope instead Verify
What does compare_result.txt contains ? Currently you're searching for the string "no differences encountered" within your text file, is that what you want to do ? After your Select-String cmdlet $SEL will only contains lines containing this string, so the Test-Path cmdlet is unlikely to work.
SO what should i do instead Test-Path?
I edited my answer to take in account what you've told me. Does it do what you want ?
0
try
{
    $SEL = $null
    $SEL = Select-String -Path \\$serversPing\c$\Scripts\compare_result.txt -Pattern "no differences encountered" -ErrorAction Stop
    if ($SEL)
    {
        $SoftCheckResult = "ok"
    }
    else
    {
        $SoftCheckResult = "Verify"
    }
}
catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    $SoftCheckResult = "NotInScope"
}
return $softCheckResult 

Comments

0

Please try like below :

$SEL = "Fiile path location"
if ($SEL | Test-Path -ErrorAction SilentlyContinue){

if ($SEL Get-Content | Select-String -pattern "no differences encountered")
{

}
....
}

1 Comment

seems strange ... what is this $SELGet ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.