Advise please Guys with a query on a dword value
Home  » CP Development  » Advise please Guys with a query on a dword value

Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 7:03 AM :: Rank: 0
Author
Points: 24529
Level: System Center Expert

I have written some code which is not giving me the answer I am looking for and was hoping for one of you to tell me where I'm going wrong. I want to get a dword value but I will be unsure of the name of the key (as this could be random) what I do need to find is that when a 'ValidCommunity' is created that the dward setting is set to 4.

Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002

Dim strComputer
Dim objRegistry
Dim strKeyPath
Dim strValueName
Dim dwValue

strComputer = "."
'Set objRegistry=GetObject("winmgmts:\\" & _
   ' strComputer & "\root\default:StdRegProv")
Set objRegistry=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")   
 
strKeyPath = "System\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities"
strValueName = "."
dwValue = "reg_dword"

objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue
Wscript.Echo "" & dwValue

'Wscript.Echo "This registry key exist with the", strValueName & " = " & dwValue

Thoughts....

Reply  
Report Abuse
Re: Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 9:29 AM :: Rank: 0
Author
Points: 12514
Level: System Center Expert
Hi Simon,



I think I cannot follow. Can you post a screenshot of this reg key where some communities are shown. Do you then want to iterate through all values and verify that each one has a "4" in it?



cheers,

Stefan
Reply  
Report Abuse
Re: Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 9:34 AM :: Rank: 0
Author
Points: 24529
Level: System Center Expert
Hi Stefan and thanks for your input in the SCC forum; To get a reg key to display is an easy thing to get this one seems not to be the case so I used this code to display ALL inside the SNMP hive, hey guess what is stop before getting to the one I want!



' Constants (taken from WinReg.h)

'

Const HKEY_CLASSES_ROOT = &H80000000

Const HKEY_CURRENT_USER = &H80000001

Const HKEY_LOCAL_MACHINE = &H80000002

Const HKEY_USERS = &H80000003



Const REG_SZ = 1

Const REG_EXPAND_SZ = 2

Const REG_BINARY = 3

Const REG_DWORD = 4

Const REG_MULTI_SZ = 7



' Chose computer name, registry tree and key path

'

strComputer = "." ' Use . for current machine

hDefKey = HKEY_LOCAL_MACHINE

strKeyPath = "System\CurrentControlSet\Services\SNMP"



' Connect to registry provider on target machine with current user

'

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")



' Enum the subkeys of the key path we've chosen

'

oReg.EnumKey hDefKey, strKeyPath, arrSubKeys



For Each strSubkey In arrSubKeys



' Show the subkey

'

wscript.echo strSubkey



' Show its value names and types

'

strSubKeyPath = strKeyPath & "\" & strSubkey

oReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes



For i = LBound(arrValueNames) To UBound(arrValueNames)

strValueName = arrValueNames(i)

Select Case arrTypes(i)



' Show a REG_SZ value

'

Case REG_SZ

oReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue

wscript.echo " " & strValueName & " (REG_SZ) = " & strValue



' Show a REG_EXPAND_SZ value

'

Case REG_EXPAND_SZ

oReg.GetExpandedStringValue hDefKey, strSubKeyPath, strValueName, strValue

wscript.echo " " & strValueName & " (REG_EXPAND_SZ) = " & strValue



' Show a REG_BINARY value

'

Case REG_BINARY

oReg.GetBinaryValue hDefKey, strSubKeyPath, strValueName, arrBytes

strBytes = ""

For Each uByte in arrBytes

strBytes = strBytes & Hex(uByte) & " "

Next

wscript.echo " " & strValueName & " (REG_BINARY) = " & strBytes



' Show a REG_DWORD value

'

Case REG_DWORD

oReg.GetDWORDValue hDefKey, strSubKeyPath, strValueName, uValue

wscript.echo " " & strValueName & " (REG_DWORD) = " & CStr(uValue)



' Show a REG_MULTI_SZ value

'

Case REG_MULTI_SZ

oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues

wscript.echo " " & strValueName & " (REG_MULTI_SZ) ="

For Each strValue in arrValues

wscript.echo " " & strValue

Next



End Select

Next



Next

Reply  
Report Abuse
Re: Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 9:35 AM :: Rank: 0
Author
Points: 24529
Level: System Center Expert
HERE is the Key,



Windows Registry Editor Version 5.00



[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities]

"Public"=dword:00000004
Reply  
Report Abuse
Re: Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 10:54 AM :: Rank: 0
Author
Points: 24529
Level: System Center Expert
This is the correct code.



Const HKEY_CLASSES_ROOT = &H80000000

Const HKEY_CURRENT_USER = &H80000001

Const HKEY_LOCAL_MACHINE = &H80000002

Const HKEY_USERS = &H80000003

Const REG_SZ = 1

Const REG_EXPAND_SZ = 2

Const REG_BINARY = 3

Const REG_DWORD = 4

Const REG_MULTI_SZ = 7





' Chose computer name, registry tree and key path





strComputer = "." ' Use . for current machine

hDefKey = HKEY_LOCAL_MACHINE

strKeyPath = "System\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities"



' Connect to registry provider on target machine with current user



Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")



strSubKeyPath = strKeyPath & "\" & strSubkey

oReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes



For i = LBound(arrValueNames) To UBound(arrValueNames)



strValueName = arrValueNames(i)



Select Case arrTypes(i)



' Show a REG_SZ value



Case REG_SZ



oReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue



wscript.echo " " & strValueName & " (REG_SZ) = " & strValue



' Show a REG_EXPAND_SZ value



Case REG_EXPAND_SZ



oReg.GetExpandedStringValue hDefKey, strSubKeyPath, strValueName, strValue



wscript.echo " " & strValueName & " (REG_EXPAND_SZ) = " & strValue





' Show a REG_BINARY value



Case REG_BINARY



oReg.GetBinaryValue hDefKey, strSubKeyPath, strValueName, arrBytes



strBytes = ""



For Each uByte in arrBytes



strBytes = strBytes & Hex(uByte) & " "



Next



wscript.echo " " & strValueName & " (REG_BINARY) = " & strBytes







' Show a REG_DWORD value



'



Case REG_DWORD



oReg.GetDWORDValue hDefKey, strSubKeyPath, strValueName, uValue



wscript.echo " " & strValueName & " (REG_DWORD) = " & CStr(uValue)





' Show a REG_MULTI_SZ value



'



Case REG_MULTI_SZ



oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues



wscript.echo " " & strValueName & " (REG_MULTI_SZ) ="



For Each strValue in arrValues



wscript.echo " " & strValue



Next



End Select



If uValue=int(4) Then

WScript.echo "True"

Else

WScript.Echo "False"

WScript.quit

End If





Next
Accepted Answer
Reply  
Report Abuse
RE: Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 11:12 AM :: Rank: 0
Author
Points: 29130
Level: System Center Expert

 here is a commented and beautified version of the correct script. Basically, it simply checks all SNMP community strings to determine if any communities are NOT READ ONLY.

I have also attached a downloadable version to this post. This would be just right for use in a DCM baseline in ConfigMgr 2007. If you  download, rename with a .vbs extension.

'Declare Registry Constants
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7


'Chose computer name, registry tree and key path

strComputer = "." ' Use . for current machine

'This is the HIVE we're working in
hDefKey = HKEY_LOCAL_MACHINE

'This is the key we will target. Next, we will enumerates values of this key
strKeyPath = "System\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities"

' Connect to registry provider on target machine with current user
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

'Next, we will loop through the values in this key looking for any CASE where the 
'SNMP community string does not have an access value of 4 (read only)

strSubKeyPath = strKeyPath & "\" & strSubkey
oReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes


For i = LBound(arrValueNames) To UBound(arrValueNames)

    strValueName = arrValueNames(i)

Select Case arrTypes(i)

' Show a REG_SZ value

Case REG_SZ 

    oReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue

    wscript.echo " " & strValueName & " (REG_SZ) = " & strValue

' Show a REG_EXPAND_SZ value


Case REG_EXPAND_SZ

    oReg.GetExpandedStringValue hDefKey, strSubKeyPath, strValueName, strValue

    wscript.echo " " & strValueName & " (REG_EXPAND_SZ) = " & strValue

' Show a REG_BINARY value

Case REG_BINARY

    oReg.GetBinaryValue hDefKey, strSubKeyPath, strValueName, arrBytes

    strBytes = ""

For Each uByte in arrBytes

    strBytes = strBytes & Hex(uByte) & " "

Next

wscript.echo " " & strValueName & " (REG_BINARY) = " & strBytes


' Show a REG_DWORD value

Case REG_DWORD

    oReg.GetDWORDValue hDefKey, strSubKeyPath, strValueName, uValue

    wscript.echo " " & strValueName & " (REG_DWORD) = " & CStr(uValue)    


' Show a REG_MULTI_SZ value

Case REG_MULTI_SZ

    oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues    

    wscript.echo " " & strValueName & " (REG_MULTI_SZ) ="

For Each strValue in arrValues

    wscript.echo " " & strValue 

Next

End Select

'Display the SNMP community strings 
'If value is not 4(read only) return FALSE and Exit

    If uValue=int(4) Then 
    
        WScript.echo "True" 
    
    Else 
    
        WScript.Echo "False"
        WScript.quit 
    
    End If 



Next

Reply  
Report Abuse
Re: Advise please Guys with a query on a dword value
Posted: Sat, Oct 31, 2009 11:31 AM :: Rank: 0
Author
Points: 24529
Level: System Center Expert

What would also be worth knowing is how you just did that! As format the code with such grace.

Reply  
Report Abuse

Home  » CP Development  » Advise please Guys with a query on a dword value
Tag Cloud
Quick Links
Top Contributors
Pete Zerger
Points: 29130
Level: System Center Expert
Simon Skinner
Points: 24529
Level: System Center Expert
Tommy Gunn
Points: 20757
Level: System Center Expert
Stefan Koell
Points: 12514
Level: System Center Expert
MadHatter
Points: 11676
Level: System Center Expert