Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

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.
0
tupac1979
Asked:
tupac1979
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now