使用SMO WMI ManagedComputer枚举SQL Server实例的exception

我正在尝试在安装了SQL Server 2008 R2的Windows 7上使用PowerShell 2.0中的SMO WMI API /对象,以使用Managed Comuter对象获取本地计算机上的SQL Server实例列表。 但是,当我尝试访问对象上的任何数据时,我在实例化对象后收到exception。 我以pipe理员身份运行PowerShell。

$computer = New-Object Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer ($env:computername) $computer.ServerInstances 

结果在这个错误:

尝试枚举集合时抛出了以下exception:“尝试pipe理服务时SMO发生exception”。
在线:1个字符:89
+(New-Object Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer($ env:computername))。 <<<< ServerInstances
+ CategoryInfo:NotSpecified:(:) [],ExtendedTypeSystemException
+ FullyQualifiedErrorId:ExceptionInGetEnumerator

有什么服务,我必须使这个工作? WMI服务正在运行。 有我需要的其他设置吗? 为什么我不能枚举SQL Server实例?

你是否以pipe理员身份运行此代码? 访问WMI需要WMI权限。 但是,我使用了另一种方法。 我在这里logging: http : //www.powershellmagazine.com/2013/04/24/pstip-enumerate-all-sql-server-instances-in-a-network/

 [Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers("MyDBServer")