Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HKLM and RegWrite

654 views
Skip to first unread message

David F

unread,
Jan 26, 2004, 2:13:42 PM1/26/04
to
I'm wanting to create an HKLM key that needs to be added to 500
workstations in a windows 2000 domain. i was wanting to do this with
a custom .adm file or via login script.

can an HKLM key be created as a regular user?

thx

Harvey Colwell

unread,
Jan 27, 2004, 12:30:16 PM1/27/04
to

There may be secruity (permissions) issues. Everyone here has local
administrator rights to their PCs.

Here is a script I use within our Logon Script. It checks the date of a
value in a HKLM key to determine if the script has already ran this week or
not. It returns an error code "0" or "1" that I check from within the Logon
Script with the good old "IF ERRORLEVEL" command.

The subroutine "TestReg" does all the work. It checks if the key (Line 16)
and/or value (Line 17) exists. If not, they are created and populated with
data appropriate for the type of testing I'm doing (a date).

You should be able to to take the TestReg sub and easily modify it to write
whatever you want.

Just for completeness: The "WeekOf" function returns that week's Monday's
date for a given date passed to it


[--- Begin: Once_A_Week.vbs ---]

001. ' Windows Script Host - VBScript
002.
'---------------------------------------------------------------------------
-----
003. ' Name: Once A Week
004. ' By: Harvey Colwell
005. ' Version: 1.0
006. ' CopyRight: (c) Jan 2004, All Rights Reserved!
007. '
008.
'***************************************************************************
*****
009.
010. Option Explicit
011.
012. Dim oWS, dThisWeek, sRegKey, sRegVal, iRetCode
013.
014. Set oWS = WScript.CreateObject("WScript.Shell")
015.
016. sRegKey = "HKLM\Software\MyCompany\Scripts\ScriptName\"
017. sRegVal = sRegKey & "DateLastRan"
018.
019. Call TestReg(sRegKey, sRegVal)
020. dThisWeek = WeekOf(Date)
021.
022. If DateDiff("d", oWS.RegRead(sRegVal), dThisWeek) = 0 Then iRetCode
= 0 Else iRetCode = 1
023.
024. oWS.RegWrite sRegVal, dThisWeek, "REG_SZ"
025.
026. Set oWS = Nothing
027. WScript.Quit(iRetCode)
028.
029. '---------------------
030. Sub TestReg(sRegKey, sRegVal)
031. Dim aTmp, sTmp, iIndex1, iIndex2
032. Err.Clear
033. On Error Resume Next
034. oWS.RegRead(sRegKey)
035. If Err.Number <> 0 Then
036. aTmp = Split(sRegKey, "\")
037. For iIndex1 = 2 To UBound(aTmp) - 1
038. sTmp = ""
039. For iIndex2 = 0 To iIndex1
040. sTmp = sTmp & aTmp(iIndex2) & "\"
041. Next
042. oWS.RegWrite sTmp, ""
043. Next
044. End If
045. oWS.RegRead(sRegVal)
046. If Err.Number <> 0 Then oWS.RegWrite sRegVal, "1/1/2000"
047. On Error Goto 0
048. End Sub
049.
050. '---------------------
051. Function WeekOf(dWeekOf)
052. WeekOf = DateAdd("d", ((WeekDay(dWeekOf, vbMonday)) * -1) + 1,
dWeekOf)
053. End Function
054.

[--- End: Once_A_Week.vbs ---]


Torgeir Bakken (MVP)

unread,
Feb 11, 2004, 7:21:57 PM2/11/04
to
AK wrote:

> How do I search the registry for a particular CLSID and delete it.

Hi

If you know the CLSID value, why not just delete it directly:


Const HKLM = &H80000002
sKey = "SOFTWARE\Classes\CLSID\{842D44C9-C347-D1D1-8F64-10C04FB61111}"

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

DeleteRegistryKey HKLM, sKey


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


AK

unread,
Feb 12, 2004, 4:21:14 PM2/12/04
to
Hi Bakken, thanks for your help, my requirement is I need to search for a particular clsid on the machines of users, thats the reason I thought of writing a small script file which I can send it to them.
As of now I tried executing your script(as .vbs) with a Key value, but it didn't delete the entry from the registry, I tried it from VB also but no luck.(I deliberately created a key in HKLM and tried deleting it).
Thanks again.

Torgeir Bakken (MVP)

unread,
Feb 12, 2004, 4:38:43 PM2/12/04
to
AK wrote:

> Hi Bakken, thanks for your help, my requirement is I need to search for a particular clsid on the machines of users, thats the reason I thought of writing a small script file which I can send it to them.
> As of now I tried executing your script(as .vbs) with a Key value, but it didn't delete the entry from the registry, I tried it from VB also but no luck.(I deliberately created a key in HKLM and tried deleting it).

Hi

Then you must have done something wrong. If I create the key that my
example uses, and then run the script (as a .vbs file), the key gets
deleted (Win2k and WinXP).

Of course the user needs to be a local admin for this to work...

AK

unread,
Feb 13, 2004, 10:26:08 AM2/13/04
to
Hi Bakken, here is what I did:
These are the registry entries;
Windows Registry Editor Version 5.00

[HKEY_Local_Machine\Software\Test]

[HKEY_Local_Machine\Software\Test\Test_Key]
"default" = "Test_value1"

[HKEY_Local_Machine\Software\Test_Key]
"default" = "Test_Value2"

and this is the changes I made to your script:

Const HKLM = &H80000002
sKey = "test_key"

I was expecting the script to delete both the keys defined in the registry, is there anything else that I was supposed to do.
Thanks for your help.
AK

Torgeir Bakken (MVP)

unread,
Feb 16, 2004, 2:50:03 PM2/16/04
to
AK wrote:

Hi

The script I supplied supports only explicit key paths (deleting only the
key you point directly to), it will not do a "global" search and delete.

Here is a rewritten script that is able to do a "global" search and delete
for a key name in a registry branch. It will delete all keys with the
name "Test_Key" in the registry branch HKEY_LOCAL_MACHINE\SOFTWARE:


Const HKLM = &H80000002
sStartKeyPath = "SOFTWARE"
sKey = "Test_Key"

Set oReg = GetObject _
("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

SearchAndDeleteRegistryKey HKLM, sStartKeyPath, sKey


Sub SearchAndDeleteRegistryKey(ByVal sHive, ByVal sStartKeyPath, ByVal sKey)


Dim aSubKeys, sSubKey, iRC
On Error Resume Next

iRC = oReg.EnumKey(sHive, sStartKeyPath, aSubKeys)


If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If

SearchAndDeleteRegistryKey sHive, sStartKeyPath & "\" & sSubKey, sKey
Next
End If

If LCase(Right(sStartKeyPath, Len(sKey) + 1)) = "\" & LCase(sKey) Then
DeleteRegistryKey sHive, sStartKeyPath
End If
End Sub


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
Dim aSubKeys, sSubKey, iRC
On Error Resume Next
iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
If iRC = 0 And IsArray(aSubKeys) Then
For Each sSubKey In aSubKeys
If Err.Number <> 0 Then
Err.Clear
Exit Sub
End If
DeleteRegistryKey sHive, sKey & "\" & sSubKey
Next
End If
oReg.DeleteKey sHive, sKey
End Sub

--

0 new messages