在 Powershell 中,我枚举 SQL 数据库如下:
$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"))) {
作为需要运行此脚本的服务帐户,从命令行运行效果很好。
但是,当从计划任务运行时,我最终遇到“拒绝访问”错误,导致枚举失败。
(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.
计划任务配置为作为服务帐户运行,并且“具有最高权限”。
与从命令行直接使用同一帐户运行相比,什么可能导致从计划任务访问 WMI 失败,以及如何解决此问题?