Page 1 of 1 1
Topic Options
#210381 - 2015-07-09 05:33 PM Write a specific Registry string if it doesn't exist (win7 x64)
Sparkdudimus Offline
Just in Town

Registered: 2015-07-09
Posts: 2
Loc: Reynoldsburg, Ohio
This is my Very first KIX script so please be gentle. I need to read the "run" key and see if a specific entry exists. If the entry doesn't exist, then I need to create it. This is what I have so far.....

 Code:
If (Not KeyExist("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "BGinfo")
  WriteValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "BGInfo", "REG_SZ", "C:\windows\BGInfo\bginfo LHBGinfo.bgi /taskbar /TIMER:0 /NOLICPROMPT") 


Basically I need to be sure this key doesn't exist, and if not then write it. I am adding a string value named "BGInfo" and pointed to "C:\Windows\BGInfo\bginfo LHBGinfo.bgi /taskbar /TIMER:0 /NOLICPROMPT"


Thank you so much.

Top
#210382 - 2015-07-09 08:18 PM Re: Write a specific Registry string if it doesn't exist (win7 x64) [Re: Sparkdudimus]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11579
Loc: CA
There are a few ways to do this, but there are also a couple of potential issues.

KeyExist cannot read values it is for checking if Keys exist. Your BGInfo entry is not a Key

You're writing to an HKLM section of the Registry which means the user needs Admin rights which is frowned upon.

The key you have listed would only work for a 64-bit program (BGInfo is not 64-bit) assuming you have 64-bit machines as many of us do.

To read that key you have to set an option as in example

You could also do IIF or SelectCase, etc. If you always want that value to be there then don't bother reading it, just write it. It's just as fast to write it as it is to read it and test.

Here is some example code to give an idea of what's happening.

 Code:
Break On
Dim $so, $rv, $wr
$so = SetOption ("WOW64AlternateRegView", "On")
$rv = ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","BGinfo")
If $rv
  ? "Value already exists: " + $rv
Else
  ? "Value not found so write it"
  $wr=WriteValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "BGInfo", "C:\windows\BGInfo\bginfo LHBGinfo.bgi /taskbar /TIMER:0 /NOLICPROMPT","REG_SZ") 
EndIf



Top
#210383 - 2015-07-10 06:24 PM Re: Write a specific Registry string if it doesn't exist (win7 x64) [Re: NTDOC]
Sparkdudimus Offline
Just in Town

Registered: 2015-07-09
Posts: 2
Loc: Reynoldsburg, Ohio
Ok, So I'm trying to understand this as well as "Do it". We have no managment here at my work. So I had a friend give me this enourmous KIX script that runs at Startup via GPO. It verifies versions of software, and installs from a network share as needed. I am trying to "reverse engineer" the script to make it work in our organization. Right now, I'm working on adding the BGInfo at the front of it so that we can make it easier on the two of us who have to manage 2000 computers.

Either way, here is a test KIX that I am trying to copy the BGInfo to the Windows folder if it doesn't exist and then add the info into the registry.

 Code:
$PrimaryServer = [i]MyServer[/i]
function
If Not Exist('C:\Windows\BGInfo')
		md "C:\Windows\BGInfo\"
		copy "$PrimaryServer\NETLOGON\Tech\BGInfo\*.*" "C:\Windows\BGInfo\*.*" /c /h /r /s
	EndIf
If (Not KeyExist("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "BGinfo")
		WriteValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "BGInfo", "C:\Windows\BGInfo\bginfo LHBGinfo.bgi /taskbar /TIMER:0 /NOLICPROMPT", "REG_SZ")
		
	EndIf
EndFunction


Obviously this is not using the code you posted. Would you be so kind as to maybe break it down for me so I can understand why you did what you did and why? I used to be great at batch scripting but its been a LONG time.

Top
#210384 - 2015-07-10 07:04 PM Re: Write a specific Registry string if it doesn't exist (win7 x64) [Re: Sparkdudimus]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11579
Loc: CA
Well again - generally speaking you're asking a logon script to do something that requires Admin rights. Personally I'd not recommend using folders under WINDOWS. I'd either go with an AppData folder or you're own off the root such as : C:\Company-utils or some variation of that. Even normal users should be able to create that folder.

Though I love KiX and have been using it for years I'm not a purist like many others here. To learn scripting is a good thing but I would probably use Robocopy for file copies. More robust and many more features.

Keep doing it as you're doing though little pieces at a time and test it out until you know for sure what it's doing and why. Test it out on your own machine or a test machine.

You can run it from DOS console to see what it does. Using a ? you can have it create a new line to out put any errors or success.

Here is some example code for creating a folder if it does not already exist.


 Code:
 
 ;Check if the folder exists, if not create it otherwise continue on.
  $Folder = "C:\MyCompany"
  If GetFileAttr($Folder) & 16
    ;Folder found do nothing
  Else
    MD $Folder
    If @ERROR
      Quit @ERROR
    EndIf
  EndIf


This allows you to check errors if wanted in the script and if needed take additional actions.

This example when placed in the section of code you're testing will help to show what errors if any are happening.

I don't want to just hand you SILVER PLATTER code otherwise you won't really learn as much. Read the Word file documentation that comes with the KiXtart download and look at other examples to help you better understand the coding.

 Code:
? "Error: " + @error +"  "+ @serror

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 164 anonymous users online.
Newest Members
GBMicros, Jodey, Hiker, GTRoberts, KatieQuinn
17685 Registered Users

Generated in 0.03 seconds in which 0.012 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org