Jump to content

Help with RegRead Results


 Share

Recommended Posts

This code loads the result of a regread into a variable and then parses the result:

$RegResult = RegRead($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks")
    Select
        Case $RegResult = ""
            $RegWriteResult = RegWrite($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks", "REG_DWORD", "0")
            TrayTip("Registry Read", "AutoShareWks Reg key not found and was created. The read result was: " & $RegResult & " " & @error & _
                    ".  The write result was: " & $RegWriteResult & " " & @error, 5)
        Case $RegResult = "0" ; 0 indicates sharing is off
            TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
        Case $RegResult = "1" ; 1 indicates sharing is on
            TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)
    EndSelect

The problem is that the value of AutoShareWks is boolean and either 1 or 0.  The documentation for regread states that if failure of the regread, "" is returned.  However it seems to also interpret 0 as "" and trigger a false failure.  Even @error has the same issue.  If the reading value is 0, @error will trigger.

So, how can I get around this false failure trigger?

EDIT:

I figured out a work around like this:

$RegResult = RegRead($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks")
    Select
        Case $RegResult = "0" ; 0 indicates sharing is off
            TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
        Case $RegResult = "1" ; 1 indicates sharing is on
            TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED.", 5)
        Case Else ; Default Actions
            TrayTip("Registry Read", "AutoShareWks value not present in registry.  Auto Drive Sharing is currently ENABLED.", 5)
    EndSelect

However, still.......  Why is the error process not catching it correctly?

Edited by Colyn1337
Link to comment
Share on other sites

This code loads the result of a regread into a variable and then parses the result:

$RegResult = RegRead($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks")
    Select
        Case $RegResult = ""
            $RegWriteResult = RegWrite($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks", "REG_DWORD", "0")
            TrayTip("Registry Read", "AutoShareWks Reg key not found and was created. The read result was: " & $RegResult & " " & @error & _
                    ".  The write result was: " & $RegWriteResult & " " & @error, 5)
        Case $RegResult = "0" ; 0 indicates sharing is off
            TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
        Case $RegResult = "1" ; 1 indicates sharing is on
            TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)
    EndSelect

The problem is that the value of AutoShareWks is boolean and either 1 or 0.  The documentation for regread states that if failure of the regread, "" is returned.  However it seems to also interpret 0 as "" and trigger a false failure.  Even @error has the same issue.  If the reading value is 0, @error will trigger.

So, how can I get around this false failure trigger?

 

You are trying to compare a Number and a String in your Case statements so you have two solutions (maybe more ;-)):

Compare numbers (remove ""):

Case $RegResult = 0 ; 0 indicates sharing is off
    TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
Case $RegResult = 1 ; 1 indicates sharing is on
    TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)

Compare strings (use == instead of =):

Case $RegResult == "0" ; 0 indicates sharing is off
        TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
Case $RegResult == "1" ; 1 indicates sharing is on
        TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)

Take a look to Language Reference - Operators

I hope this helps.

Cheers,

sahsanu

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...