Reputation: 333
I have the following result from my PowerShell script:
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
And i'm trying to remove all strings containing SQLINST%Number% :
Or before Colon (included) the so that in the end my array ($SQLNamedInstanceArray
) will be as follows:
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
This is my current PowerShell code:
$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
Upvotes: 1
Views: 778
Reputation: 200523
Don't convert your registry data to a string in the first place, since that usually only complicates matters. It's far easier to extract the value from the original registry data.
Remove the PS*
properties from the registry child items so that you only get the registry values with the instance names. Then use the intrinsic property PSObject
to expand the values of the remaining object properties.
$exclude = 'PSPath', 'PSParentPath', 'PSChildName', 'PSDrive', 'PSProvider'
Get-ItemProperty -Path $RegistrySQLPath |
Select-Object -Property * -ExcludeProperty $exclude |
ForEach-Object { $_.PSObject.Properties.Value }
Upvotes: 1
Reputation: 9143
Looks like regex match:
$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 }
Upvotes: 1