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.
%systemroot%\system32. Take this into account. Also, they may be running undersystem accountor undersome user credentials. Check permissions. More also, are the scheduled task set torun whether the user is log or not?user accountwith enough rights on the system, and set theinitial path(below arguments in the action tab)