是否有 Powershell 命令可以列出我的系统上的所有 SQL 实例? (MS SQL 2008)
只是另一种方法...可以比 SQLPS 更快一点以获得快速答案。
(get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
导入 powershell sql server 扩展:
Import-Module SqlServer
然后执行这些命令
Set-Location SQLSERVER:\SQL\localhost
Get-ChildItem
我发现(至少对我来说)以上都没有返回我的 SQL Express 实例。我有 5 个命名实例、4 个全功能 SQL Server、1 个 SQL Express。上面的答案中包含了 4 个全脂,而 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.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$mach = '.'
$m = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $mach
$m.ServerInstances
System.Data.Sql 命名空间包含支持 SQL Server 特定功能的类。
通过使用
System.Data.Sql
命名空间,您可以在 Windows Power shell 中使用以下命令获取计算机上的所有 MSSQL 实例:
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources()
$a = "MyComputerName"
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() |
where { $_.servername -eq $a}
Aaron 方法返回更确定的响应。 阅读这里了解 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
}
如果您希望列表包含实例使用的随附网络端口,您可以尝试此操作...
$MsSqlSvc = Get-CimInstance Win32_Service | where Name -match 'mssql'
$PortList = Get-NetTcpConnection | select -Unique LocalPort,OwningProcess
$SqlPorts = $MsSqlSvc | foreach {
$ThisSvc = $_;
$PortList | where { $_.OwningProcess -in $ThisSvc.ProcessId }
}
$SqlPorts | select OwningProcess,
@{
l = 'SqlInstance';
e = {
$ThisPort = $_;
$MsSqlSvc | where { $_.ProcessId -eq $ThisPort.OwningsProcess } |
select -ExpandProperty Name
}
},
LocalPort