0

I have made a PowerShell script which will watch some logs and saves output in error.txt (created within script), which is working fine while running it individually. But when I scheduled it for automatic execution using Task Scheduler, the output file error.txt is not created. Rest all is working fine.

Following is my script:

$Modified = Get-Item "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log" |
            Foreach {$_.LastWriteTime.ToLongTimeString()}

$DateTime = Get-Date -format "ddd MMM dd HH:mm:ss yyyy"
$DateTime1 = Get-Date -format "ddd MMM dd"
$Time4 = Get-Date -format "HH"
echo $Time4

$test = ($Time4) - 1
echo $test

$test2 = $DateTime1 +" " + $test
echo $test2

$a = Get-Content "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log" |
     Where-Object {$_ -match $test2} |
     Where-Object {$_ -match "error"}

if ($a) {
  Get-Content "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log" |
    Where-Object {$_ -match $test2} |
    Where-Object {$_ -match "error"} |
    Out-File -Append  error.txt
} else {
  echo -$DateTime---no-errors-in-last-hour- |
    Out-File -Append error.txt 
}

When I scheduled it to run every 5 minutes, rather then creating error.txt and showing echo part, it only shows echo part.

Following are the parameters I pass to scheduler to run PowerShell:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Following are the arguements to run PowerShell script:

-File "C:\Users\gurlove.chopra\Desktop\SAMPLE_WATCHER8.PS1"

Can anyone help me regarding why my file is not getting created?

Also I tried creating a .BAT file which calls this PowerShell script and scheduled it in same manner, but I faced the same results, that is individually it is working fine but using Task Scheduler I got the same result.

4
  • 1
    scheduled task are not running from where you supposed they do. They usually run from %systemroot%\system32. Take this into account. Also, they may be running under system account or under some user credentials. Check permissions. More also, are the scheduled task set to run whether the user is log or not? Commented Jun 6, 2017 at 11:28
  • What did you define as the working directory of the scheduled task? Does the user running the task have write access to that directory? Commented Jun 6, 2017 at 11:35
  • I suggest to set the task to run under user account with enough rights on the system, and set the initial path (below arguments in the action tab) Commented Jun 6, 2017 at 11:36
  • yes you are right @ elzooilogico , i checked the %SystemRoot%\system32\WindowsPowerShell\v1.0 , and i found there my error.txt, and now i have given the path in the powershell script, it is working fine. Thanks for your help, i need lot to learn Commented Jun 6, 2017 at 12:44

1 Answer 1

1

Thanks both of you elzooilogico and Ansgar Wiechers for your concern, it is source of great help.

I made a slight change in my powershell script , which is rather then writing only the name of file , i gave the full path and bingo it is working.

My file looks like this now:

$Modified= Get-Item "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log"| Foreach {$_.LastWriteTime.ToLongTimeString()}

$DateTime= Get-Date -format "ddd MMM dd HH:mm:ss yyyy"

$DateTime1 = Get-Date -format "ddd MMM dd"

$DateTime2 = Get-Date -format "HH:mm:ss"

$Time4 = Get-Date -format "HH"
echo $Time4
$Time5 = Get-Date -format "mm"

$Time6 = Get-Date -format "ss"

$test= ($Time4)- 1
echo $test

$test2= $DateTime1 +" " + $test
echo $test2

$a= Get-Content "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log"|Where-Object {$_ -match $test2}|Where-Object {$_ -match "error"}


if ($a){
Get-Content "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\error.log"|Where-Object {$_ -match $test2}|Where-Object {$_ -match "error"} | Out-File -Append C:\Users\gurlove.chopra\Desktop\error.txt
}
else{
echo -$DateTime---no-errors-in-last-hour-|Out-File -Append C:\Users\gurlove.chopra\Desktop\error.txt 
}
Sign up to request clarification or add additional context in comments.

Comments

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.