I'm working on a simple script wherein I wanted to check if a log contains a specific string and give me an output if it contains the string or not. However the logfile gets updated daily and I wanted to check the most recent file. Is there a command I can add to my script to get the output I needed?. Or is there other way I can do this without using Select-String?.
$SEL = Select-String -Path Pathfile.LOG -Pattern "String"
if ($SEL -ne $null)
{
echo Contains String
}
else
{
echo Not Contains String
}}
CodePudding user response:
If the desired log file was most recently written, this might do what you want.
$SEL = Get-ChildItem -Path 'C:\log\dir' -Filter '*.log' |
Sort-Object -Property LastWriteTime |
Select-Object -Last 1 |
Select-String -Pattern 'String'
CodePudding user response:
Thanks for your help!. This is what I got and it worked as expected.
Invoke-Command -Computername 'servername' -ScriptBlock {$SEL = Get-ChildItem 'Filepath' | Sort CreationTime -Descending | Select -First 1 | Select-String -Pattern "STRING" -CaseSensitive
if ($SEL -ne $null)
{
echo STRING found
}
else
{
echo STRING not found
}}