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
cornholmeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SnAkEhIpSCommented:
What exactly are you typing into Task Scheduler?
0
Asif NaeemSr. System Administrator ( Wintel & UNIX (AIX) Commented:
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
0
Vikas BhatExperienced IT Infrastructure Services/operations ManagerCommented:
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"
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

cornholmeAuthor Commented:
Hi Vikas,
I've tried both manual and scheduled start. Both with and without quotes
0
Vikas BhatExperienced IT Infrastructure Services/operations ManagerCommented:
I mean to run the script manually using powershell, could be issue with permisison as you are saving on root of c drive.
Windows-PowerShell.jpg
0
footechCommented:
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.
0
cornholmeAuthor Commented:
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
0
cornholmeAuthor Commented:
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
0
footechCommented:
PowerShell doesn't need to run under an administrator account unless the script itself (actions that it performs) requires it.  I can't say whether any of the Access items need it.

You can examine NTFS permissions on the folders in question, and if there's any doubt you can make simple additions to the PS script to add logging which is written out to a file in the folder in question.
Having logging and error handling becomes more important in scripts which are to be run as scheduled tasks, as you won't be able to see the errors and intervene.

Does your "C:\ServicesList\Services $($dateToday).csv" file get created?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cornholmeAuthor Commented:
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
0
footechCommented:
Not sure about SYSTEM, but your user may not have the "log on as batch job" user right.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.