1

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

enter image description here

Trigger (daily) (every 5 minutes)

enter image description here

Actions

enter image description here

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?

1
  • add -ExecutionPolicy Bypass before -File C:\... in your arguments
    – SimonS
    Commented Dec 22, 2021 at 14:54

1 Answer 1

1

That is happening because of Window's Execution policy, Which does not allow an unsigned script to be run by default. You need to change this policy for your script to work.

  1. Open an elevated Powershell prompt.
  2. Type Set-ExecutionPolicy Unrestricted to set the policy to Unrestricted.
  3. Type Get-ExecutionPolicy to verify the current settings for the execution policy.

Please note that changing this setting is a security threat as malicious scripts can also be run by other programs. Instead you can try to write a Batch script spinoff of the same script, and then use that instead of this. Windows exeution policy don't block Batch script.

4
  • 1
    Okay that makes some kind of sense. I am unable to change the execution policy so I think i'll recreate the script in just bash. Thanks! Commented Dec 22, 2021 at 14:16
  • I stand corrected. I can change the policy and I have. All policies are changed to Unrestricted . Even now the script is not running correctly. Even after activating the task and right click -> Execute the file is not being updated. I can see the powershell screen pop up but its empty. Executing the script by right click run in powershell still works. Commented Dec 23, 2021 at 13:38
  • Also, in the Task Scheduler screen I can see an error as a result which is : 0xFFFD0000 Commented Dec 23, 2021 at 13:46
  • 1
    the error should be included in your question
    – Yorik
    Commented Dec 23, 2021 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .