Panicos
Panicos

Reputation: 311

Powershell Regex over array

I am getting the following error when trying to apply the following regex on this Exchange command.

@(Get-ExchangeServer | Format-List AdminDisplayVersion) | % { [regex]::Match($_, "^\sVersion (\d+\.\d+(\.\d+)?)\s").Success }

The command Get-ExchangeServer | Format-List AdminDisplayVersion returns the following:

[PS] C:\Windows\system32>Get-ExchangeServer | Format-List AdminDisplayVersion


AdminDisplayVersion : Version 14.0 (Build 442.3)

AdminDisplayVersion : Version 14.0 (Build 442.3)

And when applying the regex to it I get all false:

[PS] C:\Windows\system32>@(Get-ExchangeServer | Format-List AdminDisplayVersion) | % { [regex]::Match($_, "^\sVersion (\
d+\.\d+(\.\d+)?)\s").Success }
False
False
False
False
False
False

Any pointers as of what am I doing wrong?

Upvotes: 0

Views: 1154

Answers (3)

Shay Levy
Shay Levy

Reputation: 126722

AdminDisplayVersion is a Microsoft.Exchange.Data.ServerVersion object which has all of the version parts as properties. You can get the values directly without having to resort to string manipulation. If the Build part is what you;re looking for:

Get-ExchangeServer | Foreach-Object {$_.AdminDisplayVersion.Build}

Based on the above you can create a query like:

Get-ExchangeServer | Where-Object {$_.AdminDisplayVersion.Build -eq 123}

Upvotes: 1

CB.
CB.

Reputation: 60910

Try changing your pattern in:

"\sVersion (\d+\.\d+(\.\d+)?)\s"

Upvotes: 0

Akim
Akim

Reputation: 8669

You have limited your regex to start with " Version" (^\sVersion). Here is correct one:

% { [regex]::Match($_, "\sVersion (\d+\.\d+(\.\d+)?)\s").Success }

Upvotes: 0

Related Questions