Page 1 of 2

Write result in registry

Posted: 25 Feb 2011 03:43
by jvuz
Hello,

I want to get the information about the members of the local administrators written to the registry.

I already have the following:

Code: Select all

ECHO Windows Registry Editor Version 5.00 > c:\Windows\System32\admin.reg
ECHO. >> c:\Windows\System32\admin.reg
net localgroup administrators >> c:\Windows\System32\admin.reg


The problem is I need to get only the lines I need, so only the the members, not the comment. Is this possible and if so, how can I achieve this?

Jvuz

Re: Write result in registry

Posted: 25 Feb 2011 13:40
by aGerman
It makes no sense to write the members into a .reg file without the destination key and value.
I hope the output of your command line is similar. There has to be a line with hyphens above the member names.

This extracts the members from the output

Code: Select all

@echo off &setlocal enabledelayedexpansion

set "foundhyphens="
set /a n=0
for /f "delims=" %%a in ('net localgroup administrators') do (
  if defined foundhyphens set /a n+=1
  set "member!n!=%%a"
  echo("%%a"|findstr /x "\"\-\-*\"" >nul &&set "foundhyphens=1"
)
set /a n-=1

for /l %%i in (1,1,%n%) do echo !member%%i!

pause

Regards
aGerman

Re: Write result in registry

Posted: 26 Feb 2011 04:25
by jvuz
Hello,

you're right, I forgot to add the line. I would like to add the list to following key:
HKEY_LOCAL_MACHINE\SECURITY\ under the key admins

This is the result of my net localgroup administrators. Is there also a way to leave the last line (The command completed successfully.) out of the result?


Code: Select all

Alias name     administrators
Comment        Administrators have complete and unrestricted access to the computer/domain

Members

-------------------------------------------------------------------------------
Administrator
Jvuz
The command completed successfully.

Re: Write result in registry

Posted: 26 Feb 2011 04:35
by jvuz
Also, if it would be possible to write it directly to the registry itself, it would even be better, but I don't know if that's possible.

Re: Write result in registry

Posted: 27 Feb 2011 08:37
by aGerman
jvuz wrote:This is the result of my net localgroup administrators. Is there also a way to leave the last line (The command completed successfully.) out of the result?

Didn't you try my code?

jvuz wrote:Also, if it would be possible to write it directly to the registry itself, it would even be better, but I don't know if that's possible.

This can be done using REG ADD.
I can't help you because one information is missing. You told us that the key is HKEY_LOCAL_MACHINE\SECURITY\admins and the data are Administrator and Jvuz in your case. But what are the value names for Administrator and Jvuz?

Regards
aGerman

Re: Write result in registry

Posted: 27 Feb 2011 12:36
by jvuz
What do you mean with value names?

Re: Write result in registry

Posted: 27 Feb 2011 13:27
by aGerman
Have a look at this screen shot:
http://en.wikipedia.org/wiki/File:Registry_Editor_Vista.png
On the left side you can see the path and the current key. On the right side: 1st column = value names, 2nd column = data type, 3rd column = data.
Well, I have no idea how this should look like in your case.

Regards
aGerman

Re: Write result in registry

Posted: 27 Feb 2011 21:42
by jvuz
OK, sorry, the value name should be admins and the goal is in this case to have administrator and jvuz in that key, seperated by a , or a ;. But I don't know if that's possible.

Value name Type Data
admins Reg_SZ (I think this is the best type) Administrator, Jvuz

Re: Write result in registry

Posted: 28 Feb 2011 08:10
by aGerman
OK, try that code:

Code: Select all

@echo off &setlocal enabledelayedexpansion

set "separator=,"

set "foundhyphens="
set /a n=0
for /f "delims=" %%a in ('net localgroup administrators') do (
  if defined foundhyphens set /a n+=1
  set "member!n!=%%a"
  echo("%%a"|findstr /x "\"\-\-*\"" >nul &&set "foundhyphens=1"
)
set /a n-=1

for /l %%i in (1,1,%n%) do (
  set "data=!data!%separator%!member%%i!
)

if defined data (
  reg add "HKLM\SECURITY" /v "admins" /t REG_SZ /d "%data:~1%" /f
)

pause


Regards
aGerman

Re: Write result in registry

Posted: 28 Feb 2011 12:44
by jvuz
Thanks again for all the trouble. I'm now getting an error message (access denied), but this is my Windows 7 machine. Although I'm having admin rights on my machine. Tomorrow I can try it on a Windows XP machine. I'll keep you posted.

Jvuz

Re: Write result in registry

Posted: 01 Mar 2011 00:54
by jvuz
I've tested it on an xp machine with admin rights, but there I'm getting an error message (access denied). :(

Re: Write result in registry

Posted: 01 Mar 2011 05:34
by aGerman
Looks like you have no rights to write in that key. Did you try to make this by hand (using regedit)?

Regards
aGerman

Re: Write result in registry

Posted: 01 Mar 2011 05:43
by jvuz
You're a genious!!!! Thank you very very much!

jvuz

Re: Write result in registry

Posted: 29 Mar 2011 02:51
by jvuz
aGerman,

we just noticed a problem with the Windows 2000 machines, on those pc's the bacht file doesn't work. Ik ran it seperately and i got an error message saying: "to many command line parameters" (it should say too many..., but it says to many...). I know, it's strange we still have Windows 2000 machines, but they are a great minority.

Jvuz

Re: Write result in registry

Posted: 03 Apr 2011 17:29
by aGerman
Not sure which of the commands doesn't work on the old machines. You could run the batch code without @echo off. Now the command prompt should show you where the error occurs.

Regards
aGerman