1

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

2 Answers 2

1

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 }
Sign up to request clarification or add additional context in comments.

4 Comments

i need to put $src in a for each condition (for every line in my array)?
Regex should be applied to every line.
the regex works, but now i'm getting duplicate values. foreach($SQLNamedInstanc in $SQLNamedInstanceArray){ $SQLNamedInstanceArray | % { [Regex]::Match($_, '(?<=: ).*').Value } }
I misunderstood - you have to iterate over every line - that's what % is for. Edited my answer.
1

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 }

2 Comments

Thank! that's what i wanted to achieve from the first place, but didn't know how to accomplish that.. can you please explain about the intrinsic property PSObject? cause i'm trying to understand the last line of you code, without any success so far..
All PowerShell objects have a hidden property PSObject that (among other things) allows you to enumerate the properties of the object. You can see the hidden properties when using Get-Member with the parameter -Force.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.