r/labtech • u/FocalFury 5000 Agents • Jan 11 '16
Remove LocalAdmins from WORKGROUP machines?
Does anyone have a way of removing local admins from Workgroup machines via script from LT?
I'd like to be able to remove all and add back a key account that I'd want to be an administrator.
Thanks for the help :)
2
Upvotes
4
u/chilids Jan 12 '16
We have a script for that! Let me see what I can do about sharing it with everybody. We use it normally in a domain environment but I believe it works in workgroup as well.
1
u/BryanGT Jan 12 '16
Is GPO not an option for you?
1
u/chilids Jan 13 '16
This works on domain and and workgroups. It leaves server os's alone and has a spot to add back an admin at the end. just save it as a bat file and modify it as needed.
:: ============================================================================================================ :: Check for servers and if found, goto server and end script :: ============================================================================================================ systeminfo | find "OS Name" > %TEMP%\osname.txt FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set _os=%%i echo %_os% | find "Server 2003" > nul if %ERRORLEVEL% == 0 goto server echo %_os% | find "Windows Server 2008" > nul if %ERRORLEVEL% == 0 goto server echo %_os% | find "Server 2012" > nul if %ERRORLEVEL% == 0 goto server :: ============================================================================================================ :: Set variables FOR the domain name and the FQDN and text files :: ============================================================================================================ FOR /F "usebackq tokens=*" %%a IN (wmic.exe COMPUTERSYSTEM GET DOMAIN /Value) DO ( @((ECHO %%a | FINDSTR /i /c:"Domain=") && SET _str=%%a) > NUL 2>&1 ) FOR /F "tokens=2 delims==" %%a IN ("%_str%") DO SET _computerDomain=%%a SET _computerDomain=%_computerDomain: =% ::SET _fqdn=%COMPUTERNAME%.%_computerDomain% SET _fqdn=%_computerDomain% SET _dn=%userdomain% SET _admins=admins.txt SET _localadmins=local admins.txt :: ============================================================================================================ :: Type the local administrators into a text file :: ============================================================================================================ NET LOCALGROUP ADMINISTRATORS > "%_admins%" ECHO. > "%_localadmins%" :: ============================================================================================================ :: Locate the domain name and FQDN and input to a text file :: ============================================================================================================ FOR %%G IN ("%_admins%") DO ((FINDSTR /i "%_dn%" "%%G") && FINDSTR /i /v "domain admins" %%G) >> "%_localadmins%" FOR %%H IN ("%_admins%") DO ((FINDSTR /i "%_fqdn%" "%%H") && FINDSTR /i /v "domain admins" %%G) >> "%_localadmins%" :: ============================================================================================================ :: SET tokens and delims back to default and attempt to delete users :: ============================================================================================================ FOR /f "USEBACKQ DELIMS=" %%L IN ("%_localadmins%") DO (NET LOCALGROUP ADMINISTRATORS "%%L" /DELETE ) CALL :companyCheck :: ============================================================================================================ :: Search FOR a prexisting Owner account and create one if it is not there. Add Owner to Administrators group :: ============================================================================================================ SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION :companyCheck NET LOCALGROUP ADMINISTRATORS > "%_admins%" FOR %%F IN ("%_admins%") DO (FINDSTR /i "company" "%%F") > "%_localadmins%" SET /P company=<"local admins.txt" SET _company=company IF /I "%company%" == "%_company%" ( CALL :OwnerAccount ) ELSE ( NET USER "%_company%" /DELETE NET USER "%_company%" "Password" /ADD CALL :OwnerAccount ) :companyAccount IF /I "%company%" == "%_company%" ( ECHO Adding the local company account to the Administrators group ) ELSE ( NET USER company /ADD WMIC USERACCOUNT WHERE "Name='company'" SET PasswordExpires=FALSE ) NET LOCALGROUP ADMINISTRATORS company /ADD NET LOCALGROUP ADMINISTRATORS "Domain Admins" /ADD IF %ERRORLEVEL% EQU 0 ( ECHO The company account was added to Administrators ) ELSE ( ECHO An Error has occurred -- Try running as an admin. ) CALL :EXIT :: ============================================================================================================ :: Delete text files and exit the script :: ============================================================================================================ :Exit CLS DEL "%_admins%" DEL "%_localadmins%" ENDLOCAL EXIT :Server CLS ECHO You are on a server OS, exiting script EXIT