Solved

Novice question on VBScript

Posted on 2016-10-21
8
37 Views
Last Modified: 2016-10-27
Hello folks,

I'm working on a writing a VBScript and I have no experience with VBS.  I can do this with a command line with no problem, however, it needs to be in VBS.

The script I'm working on checks to see if 2 registry key exist, and if so, deletes a third key.  I've been looking around on the net and found some code that works for what I want to do.  My issue is on the delete RegKey code.  I found this code which works fine, however, if I modify it to point to a different location, it doesn't work.

Here is the code I found and if I put a key in the registry, it deletes it just fine:  HKCU\Software\Test

Const HKEY_CURRENT_USER = &H80000001 

strComputer = "."
strKeyPath = "Software\Test" 

Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv") 

DeleteSubkeys HKEY_CURRENT_USER, strKeypath 

Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath) 
    objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys 

    If IsArray(arrSubkeys) Then 
        For Each strSubkey In arrSubkeys 
            DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey 
        Next 
    End If 

    objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath 
End Sub

Open in new window


But if I change the code to this, it doesn't work.  What am I doing wrong?  HKLM\Software\Test

Const HKEY_LOCAL_MACHINE = &H80000002 

strComputer = "."
strKeyPath = "Software\Test" 

Set objRegistry = GetObject("winmgmts:\\" & 
    strComputer & "\root\default:StdRegProv") 

DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath 

Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath) 
    objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys 

    If IsArray(arrSubkeys) Then 
        For Each strSubkey In arrSubkeys 
            DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey 
        Next 
    End If 

    objRegistry.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath 
End Sub

Open in new window

0
Comment
Question by:Jody Phillips
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:CFB_Surfgoddess
ID: 41854330
You have a variable strKeyPath, but I do not see that variable assigned.  Don't know if that is the problem, but I would look at that.  Also are you using anything like notepad++ to write this?
What line is the error on...?  You can put on error resume which may make it work.
0
 

Author Comment

by:Jody Phillips
ID: 41854352
I'm using MetaPad and verified that it's a DOS VBS file.  The only thing I changed is the first line:

Const HKEY_CURRENT_USER = &H80000001

to

Const HKEY_LOCAL_MACHINE = &H80000002

and all instances of HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE.  The first one works fine, but the second one doesn't.

Looks like I also have a typo in the second script.  In line 6 I forgot the underscore, but it's there in the script I have.
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 41854493
I forgot the underscore
that's why you should always include an Option Explicit statement
1
 
LVL 21

Accepted Solution

by:
JesterToo earned 250 total points
ID: 41854779
HKCU keys are for the current user and are readable/writable by the current user.  However, HKLM keys are shared by all users and are read-only by all users.  In order to modify an HKLM key you need to run the script with elevated permissions (aka as administrator).
1
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Jody Phillips
ID: 41860851
OK, so I've tried to use this to elevate the permissions, but it's STILL not working.  My script name is Axcess-regkey.VBS and it's in the same directory.

Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
     objShell.ShellExecute "wscript.exe", _ 
        Chr(34) & strPath & "\Axcess-regkey.VBS" & Chr(34), "", "runas", 1

Open in new window

0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 41860980
it's STILL not working
What error message do you get?
0
 
LVL 21

Assisted Solution

by:JesterToo
JesterToo earned 250 total points
ID: 41861004
Jody,
Your exact code (with the correction you mentioned above) works perfectly for me.
0
 

Author Comment

by:Jody Phillips
ID: 41862911
Not sure what I was doing wrong, but it seems to be working now.  Thanks for the help folks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now