1

I'm trying to use the SMO WMI API/objects in PowerShell 2.0 on Windows 7 with SQL Server 2008 R2 installed to get a list of SQL Server instances on the local computer using the Managed Comuter object. However, I'm getting exceptions after I instantiate the objects when I try to access any data on them. I'm running PowerShell as an administrator.

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

Results in this error:

The following exception was thrown when trying to enumerate the collection: "An exception occurred in SMO while trying to manage a service.".
At line:1 char:89
+ (New-Object Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer ($env:computername)). <<<< ServerInstances
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator

Is there some service I have to enable to get this to work? The WMI service is running. Is there some other setting I need? Why can't I enumerate SQL Server instances?

  • Do you load the Assembly for SQL WMI Management? [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SQLWMIManagement') ? – Shawn Melton Jun 12 '15 at 3:35
  • I can also note that running that command on my Windows 8.1 machine with two named instances running, shows the ServerInstances is empty. – Shawn Melton Jun 12 '15 at 3:35
0

Are you running this code as administrator? Access to WMI requires WMI privileges. However, there is an alternate method I use. I documented that here: http://www.powershellmagazine.com/2013/04/24/pstip-enumerate-all-sql-server-instances-in-a-network/

[Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers("MyDBServer")
  • I've updated my question to note that I am running as an administrator. – splattered bits Jun 5 '13 at 19:40
  • That's the workaround I tried. Unfortunately, my script also does some other work with SMO.WMI, and I'm getting similar problems. I really need to get the WMI stuff working. – splattered bits Jun 5 '13 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.