I have a simple Powershell script (ps1 file) that clears the contents of a existing file, and then appends date and status of a service to the file:
Clear-Content \\MyDomain\Homes\Usr\MyUser\"Mijn Documenten"\Desktop\ScriptOutput\test.txt
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -encoding ascii \\MyDomain\Homes\Usr\MyUser\"Mijn Documenten"\Desktop\ScriptOutput\test.txt -NoNewline
" : " | Out-File -encoding ascii \\MyDomain\Homes\Usr\MyUser\"Mijn Documenten"\Desktop\ScriptOutput\test.txt -NoNewline -append
(Get-Service MyService).status | Out-File -encoding ascii \\MyDomain\Homes\Usr\MyUser\"Mijn Documenten"\Desktop\ScriptOutput\test.txt -NoNewline -append
When executing this script (right click -> run with Powershell) the folder ScriptOutput on my desktop is populated with the file test.txt with the following contents:
2021-12-22 11:41:54 : Running
When I try to schedule this task for every 5 minutes using the Task Scheduler, I can see the Powershell window pop up every 5 minutes for a split second but the file (test.txt) is not populated with the most recent data. I am expecting a different timestamp for every time the script is executed.
Properties of the task are:
General
Trigger (daily) (every 5 minutes)
Actions
Where Program / scripts :
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
And Parameters :
-File C:\Scripts\FNMS Scripts\Beacon Server Service Logging Script\beaconscript_v1.0.ps1
Why wont the script execute and update test.txt with the most recent timestamp?
-ExecutionPolicy Bypass
before-File C:\...
in your arguments