Powershell '-match' always returning false, when for text within a string.

Experts,

I've tried multiple iterations of code, dealing with -match, -eq, -like, etc.  - All resulting in failure.  No matter what, my PS script always acts on the "else" statement.  I'll keep it short, but here's the basic code  *I've omitted quite a bit, since the other lines aren't relevant.:

            ForEach ($Log in $AppLogs) {
                  $DBSize = $Log.InsertionStrings[0]
                  $LogSG = $Log.InsertionStrings[1]
                  $strCurrentSG = $db.StorageGroupName
                  $strCurrentDB = $db.Name
                  $strFullDBPath = "$strCurrentSG\$strCurrentDB"
                  If ([String]$LogSG -Match $strFullDBPath) {
                        Write-Host "'$LogSG' matched '$strFullDBPath'"
                        }
                  Else {
                        Write-Host "'$LogSG' did NOT have a match with '$strFullDBPath'"
                  }
            }

Unfortunately, even when the output matches, the script only acts on the "else", instead of the original "if".  Here's a sample output:

'Storage Group 1\SG1 DB1 (ServerName)' did NOT have a match with 'Storage Group 1\SG1 DB1 (ServerName)'
LVL 5
usslindstromAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amit KumarCommented:
Try using -eq function and remove string from If condition as it $LogSG and Path varaible has same value.

ForEach ($Log in $AppLogs) {
                  $DBSize = $Log.InsertionStrings[0]
                  $LogSG = $Log.InsertionStrings[1]
                  $strCurrentSG = $db.StorageGroupName
                  $strCurrentDB = $db.Name
                  $strFullDBPath = "$strCurrentSG\$strCurrentDB"
                  If ($LogSG -eq $strFullDBPath) {
                        Write-Host "'$LogSG' matched '$strFullDBPath'"
                        }
                  Else {
                        Write-Host "'$LogSG' did NOT have a match with '$strFullDBPath'"
                  }
            }

Open in new window

0
Amit KumarCommented:
can you give some value stored in $AppLogs? Ideally it should work with -eq operator.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
usslindstromAuthor Commented:
Thank you good sir.  Grabbed a copy of the code you just pasted - before this last post.

I had originally tried the -eq, without success, but mirroring what you had made it work.  Thanks!
0
Amit KumarCommented:
Actually I have removed string from variable and I gone through your question so found you already tried -eq so thought my script is not appropriate. That is why I asked you to give details about variables. great to know things are good with that code.
0
Amit KumarCommented:
Thanks Qlemo...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

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.