1

I have a script which pulls variables and compares them against each other. Most of them works perfectly fine, but there seems to be a problem with this particular variable.

 secedit /export /cfg C:\secedit.txt
 $var = import-csv C:\secedit.txt
 $holding = $var[4] -match "(\d+)"
 $a=$matches[1]

Assuming $var[4] is

Maximum Password Length = 100

$a will be 100

But when I do a comparision with

if($a -gt 50){"true"} My result will be false.

After troubleshooting, only if the value is 1, then it will be recognised as true.

However, doing mathematical operations such as $a/5 $a-1 all willl result in correct values with 20 and 99 respectivly.

Can someone explain this?

1 Answer 1

1

If you call $a.GetType() you will see that $a is System.String. If you use the string to do some arithmetic, it will be implicitly get castet to a number. However, if you compare the string using -gt, you have to explicit cast the value:

 secedit /export /cfg C:\secedit.txt
 $var = import-csv C:\secedit.txt
 $holding = $var[4] -match "(\d+)"
 $a=[int]$matches[1]
Sign up to request clarification or add additional context in comments.

1 Comment

Figured this out after trying to add 1 and it became 1001 ! thanks alot! can only accept answer after 6 minutes though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.