How to create script to delete registry key for a subnet of computers

I need help creating a .vbs script that will delete a registry key based on a particular subnet. I would like the script to check and see if the key exist and if it does then delete the key. Any help is appreciated thanks.
tupac1979Asked:
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.

ladarlingCommented:
This will get you very close. Its from our corporate login script, so its been in use for a few hundred thousand logins.

'Utility Constants
Const HKCU = &H80000001
Const HKLM = &H80000002
Const REG_SZ=1
Const REG_EXPAND_SZ=2
Const REG_BINARY=3
Const REG_DWORD=4
Const REG_MULTI_SZ=7
 
'Objects
Set oReg=GetObject("winmgmts:!root/default:StdRegProv") 'WMI [Registry]
 
 
'Here is where your actual work gets done....
 
If GetIPOctet(2) = 1 Then 'Or whatever IP Octet/Value represents your subnet
	DeleteValue(HKLM, "\Whatever\TheTarget\", "SomeValue")
End If
 
 
'End Execution
 
 
'/////////WORKER FUNCTIONS////////////////
 
Function GetIPAddress()
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ")
	Dim LocalIP
	
	For Each IPConfig in IPConfigSet
     If Not IsNull(IPConfig.IPAddress) Then 
        For i=LBound(IPConfig.IPAddress)To UBound(IPConfig.IPAddress)
                LocalIP = IPConfig.IPAddress(i)
        Next
    End If
Next
GetIPAddress = Split(LocalIP,".")
End Function
 
Function GetIPOctet (nOctet)
    Dim IPAddress
    IPAddress=GetIPAddress
    GetIPOctet=IPAddress(nOctet-1)	
End Function
 
Function DeleteValue(Key, SubKey, ValueName)
	DeleteValue = oReg.DeleteValue(Key,SubKey,ValueName)
End Function

Open in new window

0
ladarlingCommented:
For some reason it looks like it did not paste the entire script.... hopefully this one posts.

'Utility Constants
Const HKCU = &H80000001
Const HKLM = &H80000002
Const REG_SZ=1
Const REG_EXPAND_SZ=2
Const REG_BINARY=3
Const REG_DWORD=4
Const REG_MULTI_SZ=7
'Objects
Set oReg=GetObject("winmgmts:!root/default:StdRegProv") 'WMI [Registry]

'Here is where your actual work gets done....
If GetIPOctet(2) = 1 Then 'Or whatever IP Octet/Value represents your subnet
 DeleteValue(HKLM, "\Whatever\TheTarget\", "SomeValue")
End If

'End Execution

'/////////WORKER FUNCTIONS////////////////
Function GetIPAddress()
 strComputer = "."
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ")
 Dim LocalIP
 
 For Each IPConfig in IPConfigSet
     If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress)To UBound(IPConfig.IPAddress)
                LocalIP = IPConfig.IPAddress(i)
        Next
    End If
Next
GetIPAddress = Split(LocalIP,".")
End Function

Function GetIPOctet (nOctet)
    Dim IPAddress
    IPAddress=GetIPAddress
    GetIPOctet=IPAddress(nOctet-1)
End Function

Function DeleteValue(Key, SubKey, ValueName)
 DeleteValue = oReg.DeleteValue(Key,SubKey,ValueName)
End Function
0
tupac1979Author Commented:
ok Thanks that is a big start I read your comments and I just need a little more direction to put the subnet and ip address range btw it is

192.168.0.x
255.255.255.254

and the key is HKLM\software\policies\microsoft\windowsupdate is the one I want to delete
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ladarlingCommented:
Sorry about that. For some reason I was thinking 'site' instead of subnet (hence the part about which IP octet). To get the actual subnet in VBScript, you would use:
The code below will get you the subnet mask from the network adapter configuration. Note that once you have it, you can call GetIPOctet() against it just like an IP address to inspect the individual values.


winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//" & computername & ""
Set IPSet = GetObject(winmgmt1).InstancesOf("Win32_NetworkAdapterConfiguration")
x=0
For Each IP In IPSet 'Check all of the network adapter configurations	                
SubNetMask = IP.IPSubnet(x)	
wscript.echo "Mask:",SubNetMask	
If SubNetMask <> "" Then Exit For	'If a subnet is found, stop looking
x = x+1
Next

Open in new window

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
ladarlingCommented:
Oh, that was adapted (by me) from Rob Olson's subnet code located here:
http://www.myitforum.com/articles/1/view.asp?id=567
 
0
ladarlingCommented:
lol. Sorry, scratch this statement:
Note that once you have it, you can call GetIPOctet() against it just like an IP address to inspect the individual values
I forgot we changed the flow on that function.
You would just use the Split function to bust out the octets into an array based on the "." delimiter (see original code for example)
0
AmazingTechCommented:
Make sure your users has access to delete from HKLM.

I guess you could run it from an administrator's workstation and modify the strcomputer.
Const HKEY_LOCAL_MACHINE = &H80000002
Const KEY_QUERY_VALUE = &H0001
 
 
SearchForIPSubNet="192.168.0."
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
BoolSubnet = False 
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then 
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            IF instr(1, IPConfig.IPAddress(i), SearchForIPSubNet) = 1 then BoolSubnet = True
        Next
    End If
Next
 
 
If BoolSubnet Then
    Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    lngRootKey = HKEY_LOCAL_MACHINE
    strKeyPath = "software\policies\microsoft\windowsupdate"
 
    objReg.CheckAccess lngRootKey, strKeyPath, KEY_QUERY_VALUE, bHasAccessRight
    If bHasAccessRight = True Then objReg.DeleteKey lngRootKey, strKeyPath
End If

Open in new window

0
jmferozCommented:
ladarling;

I tried your code below.  My purpose is just to check to which subnet a computer belong.  I am just testing it for now, so I'm not doing anything except display a message if subnet is BH department
When I run this script I get the following error message:

Line: 24
Char:  5
Error: Subscript out of range '[number:1]'

Please help me, I'm new to VBS

Here is your code:
Set oReg=GetObject("winmgmts:!root/default:StdRegProv") 'WMI [Registry]
If GetIPOctet(2) = 1 Then 'Or whatever IP Octet/Value represents your subnet
      WScript.Echo "bh subnet"
End If
Function GetIPAddress()
      strComputer = "."
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ")
      Dim LocalIP
      
      For Each IPConfig in IPConfigSet
     If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress)To UBound(IPConfig.IPAddress)
                LocalIP = IPConfig.IPAddress(i)
        Next
    End If
Next
GetIPAddress = Split(LocalIP,".")
End Function
 
Function GetIPOctet (nOctet)
    Dim IPAddress
    IPAddress=GetIPAddress
    GetIPOctet=IPAddress(nOctet-1)      
End Function
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
VB Script

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.