Create a script to add a user to certain windows groups
Posted: 03 Sep 2018 13:39
I'm trying to create a script to outomatize a custom installation of Windows where I put a script at startup and when I initialize the image the first did it perform certain pre-defined actions in the script, one of them would be to define which local groups the user belongs to, initially I tested by setting the group as follows:
@echo off
set "i=0"
set "usr1_name=user1"
set "usr1_g_admin=yes"
set "usr1_g_user_rdesktop=yes"
set "usr1_g_user_adv=no"
set "usr1_g_users=no"
set "net=%windir%\System32\net.exe"
for /f %%a in ('"%net% localgroup "Administrators" |findstr /i "%usr1_name%""') do set /a i=%i%+1
if "%i%" == "1" (
if /i "%usr1_g_admin%" == "no" (
%net% localgroup "Adminsitrators" "%usr1_name%" /delete > nul
pause
)
) else if "%i%" == "0" (
if /i "%usr1_g_admin%" == "yes" (
%net% localgroup "Administradores" "%usr1_name%" /add > nul
pause
)
)
OK, that way it works, but in Windows there are many groups, and as I said I would like to define which group that user belongs only by setting yes or no in the script, so I thought of something like:
@echo off
set "i=0"
set "usr1_name=user1"
set "usr1_g_admin=yes"
set "usr1_g_user_rdesktop=yes"
set "usr1_g_user_adv=no"
set "usr1_g_users=no"
set "net=%windir%\System32\net.exe"
:: Groups Windows
set "allgroupwin="Administrators","Remote Desktop Users","Power Users","Users""
:: Variable Groups
set "allgroupimg="usr1_g_admin","usr1_g_user_rdesktop","usr1_g_user_adv","usr1_g_users""
for /d %%a in (%allgroupwin%) do (
for /d %%b in (%allgroupimg%) do (
for /f %%b in ('"net localgroup %%a |findstr /i "%usr1_name%""') do (
set /a i=%i%+1
echo %%a
echo %%b
pause
if "%i%" == "1" (
if "%%b" == "no" (
%net% localgroup %%a "%usr1_name%" /delete > nul
)
) else if "%i%" == "0" (
if "%%b" == "yes" (
%net% localgroup %%a "%usr1_name%" /add > nul
)
)
)
)
)
The code above does not work, but I think the reasoning would be more or less this, so I'd like some help, to know if there is any way to accomplish this in a simpler way.
@echo off
set "i=0"
set "usr1_name=user1"
set "usr1_g_admin=yes"
set "usr1_g_user_rdesktop=yes"
set "usr1_g_user_adv=no"
set "usr1_g_users=no"
set "net=%windir%\System32\net.exe"
for /f %%a in ('"%net% localgroup "Administrators" |findstr /i "%usr1_name%""') do set /a i=%i%+1
if "%i%" == "1" (
if /i "%usr1_g_admin%" == "no" (
%net% localgroup "Adminsitrators" "%usr1_name%" /delete > nul
pause
)
) else if "%i%" == "0" (
if /i "%usr1_g_admin%" == "yes" (
%net% localgroup "Administradores" "%usr1_name%" /add > nul
pause
)
)
OK, that way it works, but in Windows there are many groups, and as I said I would like to define which group that user belongs only by setting yes or no in the script, so I thought of something like:
@echo off
set "i=0"
set "usr1_name=user1"
set "usr1_g_admin=yes"
set "usr1_g_user_rdesktop=yes"
set "usr1_g_user_adv=no"
set "usr1_g_users=no"
set "net=%windir%\System32\net.exe"
:: Groups Windows
set "allgroupwin="Administrators","Remote Desktop Users","Power Users","Users""
:: Variable Groups
set "allgroupimg="usr1_g_admin","usr1_g_user_rdesktop","usr1_g_user_adv","usr1_g_users""
for /d %%a in (%allgroupwin%) do (
for /d %%b in (%allgroupimg%) do (
for /f %%b in ('"net localgroup %%a |findstr /i "%usr1_name%""') do (
set /a i=%i%+1
echo %%a
echo %%b
pause
if "%i%" == "1" (
if "%%b" == "no" (
%net% localgroup %%a "%usr1_name%" /delete > nul
)
) else if "%i%" == "0" (
if "%%b" == "yes" (
%net% localgroup %%a "%usr1_name%" /add > nul
)
)
)
)
)
The code above does not work, but I think the reasoning would be more or less this, so I'd like some help, to know if there is any way to accomplish this in a simpler way.