Link to home
Start Free TrialLog in
Avatar of cornholme
cornholme

asked on

Task Scheduler Fails to run script

I have a problem with Task Scheduler it will not do what I want it to do! Here’s the story. I’m using Powershell ISE to export a list of all Services on a pc using the following script and it works just fine.
  $DateToday = get-date -format "yyyy_MM_dd HH_mm_ss"

  Get-Service | Export-Csv -path "C:\ServicesList\Services $($dateToday).csv" -NoTypeInformation

I then move the .csv file into an Access table (365) with date and time in the table name using the following script and it works fine.


$Access = New-Object -ComObject Access.Application
$Access.OpenCurrentDatabase("C:\SystemServices\ServicesList.accdb")
$Access.Run("ImportServices")
$Access.Quit()

I then create a Task Scheduler task to automate creating a csv file and the move it into an Access table. And run it at start up. This does not work even if I run it manually. I do not get any error feedback from Task Scheduler. I then checked thro each and every tab in Task Scheduler running it after each change. No luck! I then changed the Execution Policy settings from Undefined to RemoteSigned to Bypass for LocalMachine, CurrentUser and Process. I used the following script with variations to accommodate the different Scopes.

     set-executionpolicy -scope LocalMachine -executionPolicy RemoteSigned -force

I have also changed the Registry setting at
SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell for both 32bit and 64bit.

I’m getting to a point where the problem is probably obvious, but I can’t see it.
Assistance greatly needed

cornholme
Avatar of SnAkEhIpS
SnAkEhIpS
Flag of United States of America image

What exactly are you typing into Task Scheduler?
Hi,  
why not script this   using power shell as you did

Get-Service | where {$_.Status -eq "Running"} | Export-Csv -path "C:\services.csv"  and save as  script.ps1

and schedule the task using fowwlong argument

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe


Thanks


-ExecutionPolicy Bypass C:\Scripts\yourscript.PS1 -RunType $true
Avatar of Vikas Bhat
Vikas Bhat

Can you tell me if the script runs manually? It may sound strange but try to put C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe in quotes like below

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Avatar of cornholme

ASKER

Hi Vikas,
I've tried both manual and scheduled start. Both with and without quotes
I mean to run the script manually using powershell, could be issue with permisison as you are saving on root of c drive.
User generated image
Is your task set to "Run whether user is logged on or not"?
Some items depend on having an interactive session open and won't work with that option checked.  I'm pretty sure that I've seen that required for other Office COM objects, so it wouldn't surprise me if usage of the Access COM object required it as well.
Hi footech "Runwhether user is logged on or not" is checked. I've also tested User Account with my account and System account, but I've not tried Admin account which I shall now do!

cornholme
footech you are the instigator of success! Changing User Account to Administrator under the General tab in Task Scheduler and bingo it works! So it must be a permission issue. but where?  Is the permission on the Powershell script folder or the .ps1 file in that folder. Is it on the database folder or file. Or does Powershell need to run as administrator. Is it inherited?

More digging to do, but Progress is being made thank you footech.

regards cornholme
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi footech
Thanks for the comments. The Services_date.csv file gets created without any problems.  When the PowerShell script is run manually it works every time.  I need to investigate permissions to find out why the Task Scheduler works under Author Administrator, but not under System or User.

Cheers
Cornholme
Not sure about SYSTEM, but your user may not have the "log on as batch job" user right.