locked
-ne Doesn't seem to work correctly when comparing strings... RRS feed

  • Question

  • Here's the current code I'm using...

    # $acsURLProtocolPath passed in as param. $currentValue = (Get-Itemproperty hkcu:\software\Classes\MyApp\shell\open\command).'(default)' $correctPath = '"{0}\Program Files\MyCo\MyApp\MyApp.exe" "%1"'-f $acsURLProtocolPath If ($currentValue -ne $correctPath ) { # URL Protocol registry information incorrect and needs to be reset... Set-ItemProperty -Path hkcu:\software\Classes\MyApp\shell\open\command -Name '(default)' -Value $correctPath Write-Host "The ACS URL Protocol entry was reset to" $correctPath }

    When I alter the registry entry before running the script containing this code, it is reset correctly during execution.  However, if the registry value is already equal to the correct value, I still see the Write-Host output in my build log.  It seems to always execute regardless of the comparison result.  Should the comparison operator be something like -notlike since I'm working with strings?

    It's not causing a huge problem since the value will always be correct as the main gist of the snippet is working.  I was hoping though that if I needed to troubleshoot around this, if I see the protocol message in the log post-build, I know something is changing the registry value elsewhere in my processes.  The way it seems to be working now is that it is always incorrect and nothing should really be touching it to this point in the process.

    I wonder if it will work if I put the Write-host before the actual reset of the value.

    Sunday, May 5, 2019 2:40 PM

Answers

  • So I added the current value write to the log before it is checked/rewritten and not the Write-Host in the if block seems to be working as intended.  It shows the current value before the if block and now the reset, correction message is no longer shown.

    Not sure what the issue was, but it seems to be corrected now.

    • Marked as answer by NitLions Tuesday, May 7, 2019 11:12 AM
    Sunday, May 5, 2019 9:17 PM

All replies

  • Because the two strings are not exactly the same.  The original string likely is off by one character.

    Just because they look the same to your eye does not guarantee they are the same.

    Just write the value even if it is already set.  I see no reason to worry if it is already set.


    \_(ツ)_/

    Sunday, May 5, 2019 3:16 PM
  • Can you show what values are stored in the $currentValue, $acsURLProtocolPath, and $correctPath variables? And, just in case, what type of variables they are? (e.g. $currentValue.gettype())

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, May 5, 2019 3:32 PM
  • So I added the current value write to the log before it is checked/rewritten and not the Write-Host in the if block seems to be working as intended.  It shows the current value before the if block and now the reset, correction message is no longer shown.

    Not sure what the issue was, but it seems to be corrected now.

    • Marked as answer by NitLions Tuesday, May 7, 2019 11:12 AM
    Sunday, May 5, 2019 9:17 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, May 7, 2019 2:29 AM