2014-12-27 91 views
1

我有数百台服务器,并希望检查是否存在运行在其上的非系统SQL Server数据库。它们可能有多种版本:2005,2008,2012,Express。如何在不登录的情况下获取SQL Server数据库名称?

我不确定数据库名称是否存储在特定的注册表(虽然我找不到它)或文件/文件名(在C:驱动器上),并可以通过这种方式获取信息。如果可能的话,我希望能够通过Powershell脚本完成此操作,但是我可以将其批入批处理中。

回答

2

假设您有权这样做,T-SQL和使用SQL Server管理对象(SMO)可能是您最好的选择。有关运行T-SQL或使用PowerShell中的SMO,including this one的参考资料比比皆是。

示例使用T-SQL

# Download and dot source Invoke-Sqlcmd2 
# source - https://raw.githubusercontent.com/RamblingCookieMonster/PowerShell/master/Invoke-Sqlcmd2.ps1 
. "\\Path\To\Invoke-Sqlcmd2.ps1" 

#Run sp_databases, among other options, against your SQL servers. 
"Server1", "Server2\NamedInstance" | 
    Invoke-Sqlcmd2 -query "Sp_databases" -AppendServerInstance 

示例使用SQL SMO

#Download, install components from SQL feature pack: 
    # SQLSysClrTypes, SharedManagementObjects, optionally PowerShellTools 
    # http://www.microsoft.com/en-us/download/details.aspx?id=42295 

#One of several ways to load one of the SMO binaries 
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") 

#Connect to the server with integrated Windows auth 
$Connection = New-Object "Microsoft.SqlServer.Management.Smo.Server" -ArgumentList Server1   

#List the databases 
$Connection.Databases 

#List the names of the databases 
$Connection.Databases | Select -ExpandProperty Name 

最后,它听起来就像你所关心的发现。在查找单个数据库之前,您可能会考虑查找SQL实例,然后您可以逐案进行调查。 Boe Prox写道Get-SQLInstance这只需要注册表特权,并且会吐出SQL实例名称,SQL版本和其他有用的信息。

1

SQL Server实例上的数据库未存储在注册表中或系统中任何位置的平面文件中。它存储在主数据库中的系统表中。

根据您对服务器的权限,唯一可以获取数据的地方是通过WMI。看到这个答案on StackOverflow。 WMI中的值不存储在系统中,但SQL Server通过提供程序接口与它通话,数据仍来自SQL Server实例。

这些实际上是您的唯一选择,登录到服务器并运行TSQL查询或使用WMI。

+0

我会调查一下!我搜索时找不到这个。谢谢。 – krmarshall87 2014-12-27 20:01:20

相关问题