Sql server 2008 PowerShell-列出我系统上的所有SQL实例?
是否有Powershell命令列出我系统上的所有SQL实例?(MS SQL 2008) Aaron方法返回更确定的响应。Sql server 2008 PowerShell-列出我系统上的所有SQL实例?,sql-server-2008,powershell,Sql Server 2008,Powershell,是否有Powershell命令列出我系统上的所有SQL实例?(MS SQL 2008) Aaron方法返回更确定的响应。 了解Instance.GetDataSources()导入powershell sql server扩展: Import-Module SqlServer 然后执行以下命令 Set-Location SQLSERVER:\SQL\localhost Get-ChildItem 这只是另一种方法…可以比SQLPS快一点获得快速答案 (get-itemproperty
了解Instance.GetDataSources()导入powershell sql server扩展:
Import-Module SqlServer
然后执行以下命令
Set-Location SQLSERVER:\SQL\localhost
Get-ChildItem
这只是另一种方法…可以比SQLPS快一点获得快速答案
(get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
我发现(至少对我来说)上面没有一个返回我的SQLExpress实例。我有5个命名实例,4个完整的SQL Server,1个SQL Express。上面的答案中包含了4个完整的fat,而SQL Express则没有。所以,我在互联网上做了一些挖掘,遇到了James Kehr,他列出了机器上所有SQL Server实例的信息。我使用这段代码作为编写下面函数的基础
# get all sql instances, defaults to local machine, '.'
Function Get-SqlInstances {
Param($ServerName = '.')
$localInstances = @()
[array]$captions = gwmi win32_service -computerName $ServerName | ?{$_.Name -match "mssql*" -and $_.PathName -match "sqlservr.exe"} | %{$_.Caption}
foreach ($caption in $captions) {
if ($caption -eq "MSSQLSERVER") {
$localInstances += "MSSQLSERVER"
} else {
$temp = $caption | %{$_.split(" ")[-1]} | %{$_.trimStart("(")} | %{$_.trimEnd(")")}
$localInstances += "$ServerName\$temp"
}
}
$localInstances
}
System.Data.Sql命名空间包含支持Sql Server特定功能的类
通过使用System.Data.Sql
命名空间,您可以在windows power shell中使用以下命令获取计算机上的所有MSSQL实例:
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources()
此函数将返回所有已安装的实例,并在对象列表中显示版本详细信息:
function ListSQLInstances {
$listinstances = New-Object System.Collections.ArrayList
$installedInstances = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
foreach ($i in $installedInstances) {
$instancefullname = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
$productversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Version
$majorversion = switch -Regex ($productversion) {
'8' { 'SQL2000' }
'9' { 'SQL2005' }
'10.0' { 'SQL2008' }
'10.5' { 'SQL2008 R2' }
'11' { 'SQL2012' }
'12' { 'SQL2014' }
'13' { 'SQL2016' }
'14' { 'SQL2017' }
'15' { 'SQL2019' }
default { "Unknown" }
}
$instance = [PSCustomObject]@{
Instance = $i
InstanceNameFullName = $instancefullname;
Edition = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Edition;
ProductVersion = $productversion;
MajorVersion = $majorversion;
}
$listinstances.Add($instance)
}
Return $listinstances
}
$instances = ListSQLInstances
foreach ($instance in $instances) {
Write-Host $instance.Instance
}
在我的计算机上,从SQLSERVER获取childitem:\SQL\localhost速度非常慢。我现在不能在其他计算机上测试。这是正常的行为吗?感谢更好的方法是:获取childitem |选择instancenameGreat!这也适用于远程系统:
设置位置SQLSERVER:\SQL\remoteserver;获取ChildItem
。哦,我的上帝!这么多好答案!多谢各位!但现在我有点困惑。哪一个是最好的答案?:-)也可以使用$m=(获取项目SQLServer:\SQL\$mach)。ManagedComputer
# get all sql instances, defaults to local machine, '.'
Function Get-SqlInstances {
Param($ServerName = '.')
$localInstances = @()
[array]$captions = gwmi win32_service -computerName $ServerName | ?{$_.Name -match "mssql*" -and $_.PathName -match "sqlservr.exe"} | %{$_.Caption}
foreach ($caption in $captions) {
if ($caption -eq "MSSQLSERVER") {
$localInstances += "MSSQLSERVER"
} else {
$temp = $caption | %{$_.split(" ")[-1]} | %{$_.trimStart("(")} | %{$_.trimEnd(")")}
$localInstances += "$ServerName\$temp"
}
}
$localInstances
}
function ListSQLInstances {
$listinstances = New-Object System.Collections.ArrayList
$installedInstances = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
foreach ($i in $installedInstances) {
$instancefullname = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
$productversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Version
$majorversion = switch -Regex ($productversion) {
'8' { 'SQL2000' }
'9' { 'SQL2005' }
'10.0' { 'SQL2008' }
'10.5' { 'SQL2008 R2' }
'11' { 'SQL2012' }
'12' { 'SQL2014' }
'13' { 'SQL2016' }
'14' { 'SQL2017' }
'15' { 'SQL2019' }
default { "Unknown" }
}
$instance = [PSCustomObject]@{
Instance = $i
InstanceNameFullName = $instancefullname;
Edition = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Edition;
ProductVersion = $productversion;
MajorVersion = $majorversion;
}
$listinstances.Add($instance)
}
Return $listinstances
}
$instances = ListSQLInstances
foreach ($instance in $instances) {
Write-Host $instance.Instance
}