I have this : $Qname = dspmq | where {$_ -like ‘q’} | Select-Object $_
output of $Qname is :
QMNAME(q.something.1) STATUS(Running)
QMNAME(q.something.2) STATUS(Running)
QMNAME(q.something.3) STATUS(Running)
each line is a separate string ( QMNAME(q.something.1) STATUS(Running) ) is 1 line
I’m trying to extract :
q.something.1
q.something.2
q.something.3
I tried :
$Qname = dspmq | where {$_ -like ‘q’} | Select-Object $_
$Qnames = “”
foreach ($q in $Qname) {
$Qnames += $q -match ‘.?((.?)).’ }
$Qnames = $matches
or similar things but I always get
Name Value
1 q.something.3
0 E(q.something.3 .3) STATUS(Running)}
so I only get 1 match
I need to find a way to extract the data of each first parentheses of each line
I hold my hands up and admit I’m a beginner with this type of thing but how about this:
$Qname = "QMNAME(q.something.1) STATUS(Running)","QMNAME(q.something.2) STATUS(Running)","QMNAME(q.something.3) STATUS(Running)"
$Qnames = @()
foreach($item in $Qname){
$Qnames+=($item-Split'QMNAME(',-1,'simplematch'-Split')',-1,'SimpleMatch')[1]
}
$Qnames
Which returns:
q.something.1
q.something.2
q.something.3
A simple RegEx…
'QMNAME(q.something.1) STATUS(Running)',
'QMNAME(q.something.2) STATUS(Running)',
'QMNAME(q.something.3) STATUS(Running)' |
ForEach {[regex]::Match($PSItem,'(q.*)\d').Value}
# Results
q.something.1
q.something.2
q.something.3
Olaf
4
… or you could use the -split method like this:
'QMNAME(q.something.1) STATUS(Running)',
'QMNAME(q.something.2) STATUS(Running)',
'QMNAME(q.something.3) STATUS(Running)' |
ForEach-Object {
($_ -split "\(|\)")[1]
}
# Results
q.something.1
q.something.2
q.something.3