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.OpenCurrentDatabas e("C:\Syst emServices \ServicesL ist.accdb" )
$Access.Run("ImportService s")
$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\PowerSh ell\1\Shel lIds\Micro soft.Power Shell 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
$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.OpenCurrentDatabas
$Access.Run("ImportService
$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\PowerSh
I’m getting to a point where the problem is probably obvious, but I can’t see it.
Assistance greatly needed
cornholme
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\Window sPowerShel l\v1.0\pow ershell.ex e
Thanks
-ExecutionPolicy Bypass C:\Scripts\yourscript.PS1 -RunType $true
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\Window
Thanks
-ExecutionPolicy Bypass C:\Scripts\yourscript.PS1 -RunType $true
Can you tell me if the script runs manually? It may sound strange but try to put C:\Windows\System32\Window sPowerShel l\v1.0\pow ershell.ex e in quotes like below
"C:\Windows\System32\Windo wsPowerShe ll\v1.0\po wershell.e xe"
"C:\Windows\System32\Windo
ASKER
Hi Vikas,
I've tried both manual and scheduled start. Both with and without quotes
I've tried both manual and scheduled start. Both with and without quotes
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.
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.
ASKER
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
cornholme
ASKER
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
More digging to do, but Progress is being made thank you footech.
regards cornholme
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.