cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 8

PowerShell monitor: Text entry within a file on remote host

Hello!

I am trying to perfect this script to monitor if a text entry exists within a file on a remote host. If the Text exists, I want the monitor to turn red, and if the text cannot be found, I would like the monitor to remain green in up status.

Please Review, all suggestions welcome.

Get-Content -Path \\IP\D$\Deploy\Solarwinds\TestFolder\FileTest.txt
if Get-Content -Path \\IP\D$\Deploy\Solarwinds\TestFolder\FileTest.txt -like "May the Force be with you"
{
Write-Host "Message: Exists"
Write-Host "Statistic: 1"
Exit 1
}
else
{
Write-Host "Message: Does Not Exist"
Write-Host "Statistic: 0"
Exit 0
}

Tags (1)
0 Kudos
1 Reply
Level 8

The answer to this is:

Script argument: \\${IP}\C$\Program Files (x86)\Folder1\Folder2\Temp\Filename\file.log

Body:

 

$FileName = "C:\Program Files (x86)\Folder1\Folder2\Temp\Filename\file.log"
$SearchString = "Your search string here" 
$Sel = select-string -pattern $SearchString -path $FileName
If ($Sel -eq $null)
{
Write-Host "Text not found"
Write-Host "Statistic: 0"
Exit 0
}
Else
{
Write-Host "Text found"
Write-Host "Statistic: 1"
Exit 1
}

 

I change advanced settings to use x64 as well and this worked for me... keep in mind if the text is found, this will "fail", which is correct. You can test for a success by swapping your text out for random text that is not within your file you are looking at, and it should exit with Exit code 0 and return "up/green" value.

0 Kudos