我的PowerShell脚本有以下结果:
SQLINST15 : MSSQL11.SQLINST15
SQLINST16 : MSSQL12.SQLINST16
SQLINST17 : MSSQL12.SQLINST17
SQLINST18 : MSSQL12.SQLINST18
SQLINST19 : MSSQL13.SQLINST19
SQLINST20 : MSSQL13.SQLINST20
SQLINST13 : MSSQL10_50.SQLINST13
SQLINST1 : MSSQL10_50.SQLINST1
SQLINST2 : MSSQL10_50.SQLINST2
SQLINST3 : MSSQL10_50.SQLINST3
SQLINST4 : MSSQL10_50.SQLINST4
SQLINST5 : MSSQL10_50.SQLINST5
SQLINST6 : MSSQL10_50.SQLINST6
SQLINST21 : MSSQL12.SQLINST21
我正在尝试删除所有包含SQLINST%Number% :
或在冒号(包括)之前的字符串,以便最终我的数组($SQLNamedInstanceArray
)将如下所示:
MSSQL11.SQLINST15
MSSQL12.SQLINST16
MSSQL12.SQLINST17
MSSQL12.SQLINST18
MSSQL13.SQLINST19
MSSQL13.SQLINST20
MSSQL10_50.SQLINST13
MSSQL10_50.SQLINST1
MSSQL10_50.SQLINST2
MSSQL10_50.SQLINST3
MSSQL10_50.SQLINST4
MSSQL10_50.SQLINST5
MSSQL10_50.SQLINST6
MSSQL12.SQLINST2
这是我当前的PowerShell代码:
$RegistrySQLPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL'
Set-Location $RegistrySQLPath
$SQLNamedInstanceArray = (Get-ItemProperty -Path $RegistrySQLPath | Out-String) -split "`r`n" | Select-String MSSQL
$SQLNamedInstanceArray
答案 0 :(得分:1)
看起来像正则表达式匹配:
$SQLNamedInstanceArray = "SQLINST15 : MSSQL11.SQLINST15",
"SQLINST16 : MSSQL12.SQLINST16",
"SQLINST17 : MSSQL12.SQLINST17",
"SQLINST18 : MSSQL12.SQLINST18",
"SQLINST19 : MSSQL13.SQLINST19",
"SQLINST20 : MSSQL13.SQLINST20",
"SQLINST13 : MSSQL10_50.SQLINST13",
"SQLINST1 : MSSQL10_50.SQLINST1",
"SQLINST2 : MSSQL10_50.SQLINST2",
"SQLINST3 : MSSQL10_50.SQLINST3",
"SQLINST4 : MSSQL10_50.SQLINST4",
"SQLINST5 : MSSQL10_50.SQLINST5",
"SQLINST6 : MSSQL10_50.SQLINST6",
"SQLINST21 : MSSQL12.SQLINST21"
$SQLNamedInstanceArray | % { [Regex]::Match($_, '(?<=: ).*').Value }
答案 1 :(得分:1)
首先不要将注册表数据转换为字符串,因为这通常只会使事情变得复杂。从原始注册表数据中提取值要容易得多。
从注册表子项中删除PS*
属性,以便仅使用实例名称获取注册表值。然后,使用固有属性PSObject
扩展其余对象属性的值。
$exclude = 'PSPath', 'PSParentPath', 'PSChildName', 'PSDrive', 'PSProvider'
Get-ItemProperty -Path $RegistrySQLPath |
Select-Object -Property * -ExcludeProperty $exclude |
ForEach-Object { $_.PSObject.Properties.Value }