Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#1
Post
by jvuz » 25 Feb 2011 03:43
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
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 25 Feb 2011 13:40
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
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#3
Post
by jvuz » 26 Feb 2011 04:25
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.
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#4
Post
by jvuz » 26 Feb 2011 04:35
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.
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#5
Post
by aGerman » 27 Feb 2011 08:37
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
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#6
Post
by jvuz » 27 Feb 2011 12:36
What do you mean with value names?
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#7
Post
by aGerman » 27 Feb 2011 13:27
Have a look at this screen shot:
http://en.wikipedia.org/wiki/File:Registry_Editor_Vista.pngOn 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
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#8
Post
by jvuz » 27 Feb 2011 21:42
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
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#9
Post
by aGerman » 28 Feb 2011 08:10
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
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#10
Post
by jvuz » 28 Feb 2011 12:44
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
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#11
Post
by jvuz » 01 Mar 2011 00:54
I've tested it on an xp machine with admin rights, but there I'm getting an error message (access denied).
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#12
Post
by aGerman » 01 Mar 2011 05:34
Looks like you have no rights to write in that key. Did you try to make this by hand (using regedit)?
Regards
aGerman
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#13
Post
by jvuz » 01 Mar 2011 05:43
You're a genious!!!! Thank you very very much!
jvuz
-
jvuz
- Posts: 38
- Joined: 25 Feb 2011 03:37
#14
Post
by jvuz » 29 Mar 2011 02:51
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
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#15
Post
by aGerman » 03 Apr 2011 17:29
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