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?
User contributions licensed under CC BY-SA 3.0