0

So I run this command

$driverRAIDv = $data|where-object{$_.Name -eq "$serverName" -and ($_.Description1 -match "hpsa")} | select -ExpandProperty version

And it returns this value:

HP HPSA Driver (v 5.0.0-28OEM)

I want to take this value/variable and parse it so that I only have 5.0.0-28OEM

2
  • will the text "HP HPSA Driver (v " always be in the string? Commented Jun 4, 2014 at 14:57
  • @Jimbo yes, I believe so Commented Jun 4, 2014 at 15:03

4 Answers 4

2

Try this, matches anything between ( and ) brackets:

EDIT: ..and removes the v followed by a space:

$driverRAIDv = $data|where-object{$_.Name -eq "$serverName" -and ($_.Description1 -match "hpsa")} | select -ExpandProperty version
$regex = "(?<=\().*(?=\))"
[regex]::matches($driverRAIDv,$regex).Value -replace "v "

which returns:

5.0.0-28OEM

or you could use following regex which will match anything between (v and )

$regex="(?<=\(v\s).*(?=\))"
Sign up to request clarification or add additional context in comments.

1 Comment

This works wonders! Except there's no easy way to get rid of the "v" im guessing.. Thanks for the tip!
1

Give this a try. Basically, we're creating a "calculated property" that contains an expression to parse out the portion of the value that you want.

$driverRAIDv = $data|where-object{$_.Name -eq "$serverName" -and ($_.Description1 -match "hpsa")} | select -Property @{ Label = 'Version'; Expression = { [void]($_ -match ('v\s(.*?)\)')); $matches[1]; }; };

I tested this out using the following code:

@{ Version = 'HP HPSA Driver (v 5.0.0-28OEM)'} | select -Property @{ Label = 'Version'; Expression = { [void]($_ -match ('v\s(.*?)\)')); $matches[1]; }; };

If you check out the help for the Select-Object command, you will see that you can create calculated properties, which basically modify the value of a property, through a PowerShell expression. To do this, create a Hashtable that contains two items: Label and Expression. The Label can simply be set to the same property value that you're modifying. The Expression is a PowerShell ScriptBlock that performs some type of operation, and returns a result. In the example I gave above, we are running a regular expression against the Version property, and returning it as the result.

enter image description here

1 Comment

I tried to use this example. I've had no luck. It might be because I don't fully understand whats happening.. If you have time could you explain it a little more?
0

You have many ways to do it depending on your needs and variance of what you will get.

One way could be to use replace operator. Say, you got

$driverRAIDv="HP HPSA Driver (v 5.0.0-28OEM)"
$driverRAIDv -replace 'HP HPSA Driver \(v ','' -replace '\)',''

would get you 5.0.0-28OEM

In the same line:

$driverRAIDv = ($data|where-object{$_.Name -eq "$serverName" -and ($_.Description1 -match "hpsa")} | select -ExpandProperty version) -replace 'HP HPSA Driver \(v ','' -replace '\)',''

2 Comments

I tried the line you provided and it errors out with Regular expression pattern is not valid: HP HPSA Driver (v . Otherwise though it looks simple to use.
@krousemw , hmm, my comment on the update did not make it I guess, but I had updated the line. All you have to do is to escape paranthesis as they have special meaning in regex
0

A non-regex option would be to split on (, ) and (space), and select the last non-empty string.

$driverRAIDv = $data|
    where {$_.Name -eq $serverName -and $_.Description1 -match "hpsa"} |
    foreach {$_.version.split('[() ]')| where {$_}| select -last 1}

Comments

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.