Laavaa
Laavaa

Reputation: 575

Unable to run a powershell script using Windows task scheduler

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

Upvotes: 1

Views: 2806

Answers (1)

PowerShell
PowerShell

Reputation: 2051

<#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

Upvotes: 0

Related Questions