E_ACCESSDENIED accessing WMI from scheduled task

0

In Powershell, I'm enumerating SQL databases as follows:

$SqlServers = @("SQL1", "SQL2") # SQL servers
foreach ($SqlServer in $SqlServers) {
  $SqlBasePath = "SQLSERVER:\SQL\$SqlServer\"

  foreach ($SqlInstanceName in (Get-ChildItem -Path $SqlBasePath -Name)) {
    Write-Host "Processing $SqlInstanceName"

    foreach ($SqlDatabase in (Get-ChildItem -Path $($SqlBasePath + $SqlInstanceName + "\Databases"))) {

This works great running from the command-line, as the service account which needs to run this script.

However, when running from scheduled task, I end up with "access denied" errors, causing enumeration to fail.

(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
WARNING: Could not obtain SQL Server Service information. An attempt to connect
         to WMI on 'SQL1' failed with the following error: Access is denied. 

The scheduled task is configured to run as the service account, and "with highest privileges" for what its worth.

What could cause accessing WMI from a scheduled task to fail as opposed running directly with the same account from the command line and how to fix this?

Sebazzz

Posted 2016-11-07T07:57:12.020

Reputation: 161

Try following this: http://www.thewindowsclub.com/access-denied-task-scheduler-0x80070005 It seems to apply to your situation

– Kage – 2016-11-07T21:34:52.180

Unfortunately, that didn't work. Perhaps that option is not available in Windows Server 2008 R2. – Sebazzz – 2016-11-08T07:00:42.030

No answers