• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 90
  • Last Modified:

PowerShell Error with Regex Object

I have code that I've been testing in one environment, with no problems. I've recently moved it into the production environment, and am teasting it in 'phases'. One block of code that was working fine is now giving me an error. The actual code is:
	if ([regex]::match($file,'_[0-9]{8}_[0-9]{6,}$').Success = $true)

Open in new window

and the error message I am receiving is:
"Success" is a ReadOnly property.
At D:\CABDataImport\common\AutoClientLoad\MoveFilesToStaging.ps1:35 char:51
+     if ([regex]::match($file,'_[0-9]{8}_[0-9]{6,}$'). <<<< Success = $true)
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Open in new window

2 Solutions
You probably meant "... -eq $True", not "... = $True".
It should be
if ([regex]::match($file,'_[0-9]{8}_[0-9]{6,}$').Success -eq $true)

Open in new window

"-eq" instead of "="
I've made that mistake before too.
QlemoBatchelor and DeveloperCommented:
And best way is to leave that out completely. A boolean is a boolean!
if ([regex]::match($file,'_[0-9]{8}_[0-9]{6,}$').Success)

Open in new window

dbbishopAuthor Commented:
oBdA got it first (by seconds). I should have thought of Qlemo's answer. He is correct, a boolean is a boolean.
Live and learn :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now