Solved

PowerShell Error with Regex Object

Posted on 2015-02-11
4
79 Views
Last Modified: 2015-02-11
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

0
Comment
Question by:dbbishop
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 350 total points
ID: 40603632
You probably meant "... -eq $True", not "... = $True".
0
 
LVL 39

Expert Comment

by:footech
ID: 40603634
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.
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 150 total points
ID: 40603685
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

0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40603726
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 :-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question