1
votes

I have a simple powershell script to get the names in a directory and write it into a file . I want this to be executed daily at 8:00AM so I created a task for it from the Windows task scheduler the task scheduler is able to.start the script but the script is unable to.write into a file. So as an alternative I created a batch file and I tried to invoke the ps1 using the task scheduler this is giving an error "fileopenerror" My batch file has \PowerShell.exe \test.ps1

My PowerShell script has Set-executionpolicy -scope currentuser -executionpolicy remotesigned -force ls >> names.txt

1
If you set the execution policy in the script, but the current execution policy won't let scripts run to begin with, don't you think that might be an issue? You can override the execution policy when you fire up Powershell. Type Powershell /? at a command prompt. - EBGreen
If I just execute powershell using the command prompt its working but if its initiated by the task scheduler its not . I have no clue why this is happening. - Laavaa
My guess is that the system account is not configured the way that your account is. Did you try overriding the execution policy? - EBGreen
How can I do that ? Can you give me a link or can you give the script which can do that ? -Thanks Abhishek - Laavaa
Read my first comment. Especially the bit that says "You can override the execution policy when you fire up Powershell. Type Powershell /? at a command prompt" - EBGreen

1 Answers

0
votes

<#So Here's an exmaple i thought to bring up for you it does a daily restart of BITS Service at 3am,i guess you can modify the script with your apache service example#>

<#Here Im Creating a Trigger for the Job and im setting it to start at 3am daily#>

$trigger = New-JobTrigger -Daily -At 3am

<# Below Im Registering the ScheduledJob with a Name of and a trigger of $trigger which is at daily 3am, Also im placing the script to be executed inside of a script block#>

Register-ScheduledJob -Name BitsServiceRestart -Trigger $trigger -ScriptBlock {

<# Suppose Lets say that you want to schedule a restart of BITS Service#>
Restart-Service -ServiceName BITS

}

<# Once the Job is registered you can get the Job details using Get-ScheduledJob You can also view task details using TaskScheduler --> Windows --> PowerShell #>

Get-ScheduledJob BitsServiceRestart | fl *

<# You can recieve the output of Scheduled Job using Recieve Job #>

Receive-Job -Name BitsServiceRestart