Checking text file daily

Hello all

I'm looking to do something with Powershell and at my present rate of learning it'll be a while so wondered if I could impose on someone to assist me please?

I have a text file that should be updated every day with an entry with appears as the date and an OK like this : "11/11/13 - OK" on a new line.

What I'm after is a script to run once a day and check the TXT file for the correct entry for that day. If it's missing I need it to send an email but I think I can manage that bit.

Thanks in advance.

November 12th, 2013 5:53am

M Assumption is you are trying to search a string in a file

This logic may help you

$file = Get-Content C:\Temp\SomeFile.txt
$value = $file | Select-String -Pattern "11/11/13 - OK"
if($value -match "11/11/13 - OK")
{
Write-Host "Success" -ForegroundColor Cyan
}
Else{

Write-Host "Failed" -ForegroundColor Red
}

Free Windows Admin Tool Kit Click here and download it now
November 12th, 2013 6:05am

To be more precise we can check with the date format appending -OK in a variable as a search string!!! Let me know if you need more assistance.

November 12th, 2013 6:06am

Thanks Chen

I can follow that and take it from there.

I need to have it check for the date on which it's running.

So yesterday it would have checked for "11/11/13 - OK" and today it will check for "11/12/13 - OK" (using US date format which I might change to UK later)

I'll see if I can figure this out.

Free Windows Admin Tool Kit Click here and download it now
November 12th, 2013 6:11am

The below logic for making search string may help you

$date = Get-Date -Format MM/dd/yyyy 
$str = " - OK"
$search = $date + $str
$search

You can use en-US culture info (Formatting) for the same.

You can modify the below code as required

$date = Get-Date -Format MM/dd/yy 
$str = " - OK"
$search = $date + $str
$search
$file = Get-Content C:\Temp\SomeFile.txt | Select-String -AllMatches $search
$file
if($file-match "$search")
{
Write-Host "Success" -ForegroundColor Cyan
}
Else{

Write-Host "Failed" -ForegroundColor Red}

Ensure your year format is yy not yyyy
November 12th, 2013 6:29am

Thanks very much.

I'd got as far as :

$date = Get-Date -UFormat "%m/%d/%y"
$line = $date + " - OK"

but yours looks better.

I think that's me sorted no. Thanks again for your time.

Free Windows Admin Tool Kit Click here and download it now
November 12th, 2013 6:32am

Happy to know that your requirement is met

November 12th, 2013 6:33am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics