?
Solved

vbscript registry modification / append question

Posted on 2011-05-06
7
Medium Priority
?
415 Views
Last Modified: 2012-05-11
Let me start by sayiing that I am new to vbscripting - I'm sure this code could and should be cleaned up, if anyone want to, that would be fine.

I am needing to run a file (so in this case I just chose Notepad) - but it can be any file - after notepad run successfully I need to create a key that says it was successful and then subkeys so I know who it ran for.

If someone has already run then it should prompt that it has already run - I have not gotten that far yet.
If it has run - and a different user logs on the machine and runs it, then it should write a new value under the users for each user that it successfully runs for.  (I am working on this right now)

I think that should just about do it for this one - I am stuck on the part where it allows any number of users to run it and create the new keys.

I appreciate any help.

( I also have some variables defined that I don't need anymore)

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath1 = "SOFTWARE\CompanyKey\Unique Software Run"
strKeyPath = "SOFTWARE\CompanyKey\Unique Software Run\Users"
strKeyUserPath = "Users Converted"
strValueName = "Run Status"
strValue = "Success"
strUserName = ""
strUserInfo = "The user migrated - key will be unique if run multiple machines"
strValueName1 = "User Converted"


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

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)

' Create the Registry Keys
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath


' determine who the migration ran for and populate key

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
    strUserName=objComputer.UserName
        WScript.Echo strUserName
       
Next



' Last Step - Write the Key

oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strValueName,strValue ' writes info to top level key
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strKeyUserPath,strUserName ' writes info to sub key.
0
Comment
Question by:pgroth
  • 3
  • 3
7 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 35712059
Do you need "all users" to be listed under the HKLM key ?
HKLM applies to the machine, and non-admin users will have difficulty updating the key on Vista/W7. (unless done as a startup script)

is using  HKEY_LOCAL_USER a possibility?

otherwise you will have to iterate over the list of existing keys, (named user01, user02 etc) to find the latest. Then add next one.

'last step
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strValueName,strValue ' writes info to top level key for latest user

oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath,  arrValueNames, arrValueTypes  'get all the names within key.

strValueName = "User" & str(UBound(arrValueNames)+1)   'the next number in sequence
oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath,  strValueName, strUserName


--->>
   SOFTWARE
       CompanyKey
           Unique Software Run
               Users
                  user 01 = name1
                  user 02 = name2
                  etc.


this is how windows stores the MostRecentlyUsed (MRU) keys for its software like excel.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35715192
Hi, I've made some modifications, based on what I think would suit a bit better.

What it will do is under this key:
HKLM\Software\CompanyKey\Unique Software Run\Users
it will create String value with names of
UserloginID1
UserloginID2
etc

and the value data will be Success or Failure

Then when the script is run again, it will see if the Value Name for the current user exists, and whether it was Success, and let the user know, otherwise it will run the program.

Regards,

Rob.
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath = "SOFTWARE\CompanyKey\Unique Software Run\Users"

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

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
strUserName = objNetwork.UserName

' Create the Registry Keys
On Error Resume Next
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
Err.Clear
On Error Goto 0

On Error Resume Next
' Determine if the program has not run yet, or failed in the most recent run
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strUserName,strResult
If Err.Number <> 0 Or strResult = "Failure" Then
	Err.Clear
	On Error Goto 0
	Return = objShell.Run("notepad " & WScript.ScriptFullName, 1, True)
	If Return = 0 Then		
		' Last Step - Write the Key
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strUserName,"Success" ' writes info to sub key.
	Else
		WScript.Echo "Software did not successfully run for " & strUserName
		' Last Step - Write the Key
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strUserName,"Failure" ' writes info to sub key.
	End If
Else
	Err.Clear
	On Error Goto 0
	' If the value name for the user existed, and the result was "Success" then display the message
	WScript.Echo "The program has already run successfully."
End If

Open in new window

0
 

Author Comment

by:pgroth
ID: 35721327
It creates the key and does check for the existance, but it does not run notepad (I tried it in Windows XP and Win7).

Also, for some reason it creates the user key, but does not create anything under it other than the default value.

I see in the script you are naming:  strUserName = objNetwork.UserName   - and I see where you are calling that in the creation of the key - so I am not sure why it is not creating the actual user key.
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.

 

Author Comment

by:pgroth
ID: 35721349
Robberbaron - I think what you are suggesting makes sense, it would run on Windows XP and Windows7 and not necessarily by admin users.

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35725543
Oh, I forgot one condition.

Change this line:
If Err.Number <> 0 Or strResult = "Failure" Then

to this
If Err.Number <> 0 Or strResult = "Failure" Or IsNull(strResult) Then

Regards,

Rob.
0
 

Author Closing Comment

by:pgroth
ID: 35756693
Thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35759787
No problem. Thanks for the grade.

Rob.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

864 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