GURLOVE CHOPRA
GURLOVE CHOPRA

Reputation: 85

Output file of PowerShell script not created after scheduling using Task Scheduler

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.

Upvotes: 0

Views: 2138

Answers (1)

GURLOVE CHOPRA
GURLOVE CHOPRA

Reputation: 85

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 
}

Upvotes: 1

Related Questions