Link to home
Start Free TrialLog in
Avatar of pgroth
pgrothFlag for United States of America

asked on

vbscript registry modification / append question

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.
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

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

Avatar of pgroth

ASKER

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.
Avatar of pgroth

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pgroth

ASKER

Thanks!
No problem. Thanks for the grade.

Rob.