Thursday, January 13, 2011

Active Directory - Updating login scripts for user accounts in AD

To continue from previous posts, I was able to get listings of what login scripts were set and who didn’t have one at all. The next step now that I had this info was to start updating them. With the output files created from before I was able to parse out groups of users I wanted to update into separate lists.
So take for example the master list that showed everyone and their script. I could bring that into excel and sort by scripts, remove anyone listed I didn’t want to update, and remove any content other than the username.  Why? Well because I want to take that new list, put it back into a text file so that it contains nothing but a list of usernames I want to change and run a script against it. If the file contains commas and comments, etc I take all that out. Find and replace makes it simple as does splitting data into columns and then removing columns. There’s different ways to do it, whatever works. 

You can do this different ways, one at a time, all at once, etc, and lots of modifications can be made to the script to do things differently or to do more but let’s keep it simple. I have a text file with one username on each line. I want to update the login script for all these users to bluebananas.bat. In comes another batch file to the rescue.  Simply update the name of the text file in the batch file to reflect yours then scroll down and update the name of the login script you want them to have. Save it and run it, ensure your user has permission to edit AD. The batch file will create two log files to verify which users successfully updated and which ones did not. That’s it, in just few moments you could update the login script for every user in your organization.

@ECHO OFF

SET UserList=userstoupdate.txt

SET SuccessFile=All-Successful.log
SET FailedFile=All-Failed.log

echo ----------------------------------- >> %FailedFile%
echo         %date:~-10,10% %time:~-11,5% >> %FailedFile%
echo ----------------------------------- >> %FailedFile%

echo ----------------------------------- >> %SuccessFile%
echo         %date:~-10,10% %time:~-11,5% >> %SuccessFile%
echo ----------------------------------- >> %SuccessFile%

REM -----------------------------------

CLS
ECHO.
IF NOT EXIST "%UserList%" (
  ECHO Cannot locate Machine List: %UserList%
  PAUSE>NUL
  GOTO :EOF
)


FOR /f "tokens=*" %%M in (%UserList%) do CALL :CHECK "%%M"

GOTO :EOF

:CHECK

SET USER=%~1
SET USER=%USER: =%

NET USER %USER% /scriptpath:bluebananas.bat

IF %ERRORLEVEL% NEQ 0 ECHO %USER%>>%FailedFile%
IF %ERRORLEVEL% NEQ 1 ECHO %USER%>>%SuccessFile%

EXIT /B

:EOF

4 comments:

  1. For the logon script path can I use unc path to a directory?

    ReplyDelete
    Replies
    1. You should be able to put in whatever you'd like for that field, be it a filename of a script located in the netlogon folder or a unc path. If you can put it manually into AD and it works you should be able to automate it with this script as well. I would test putting your path in AD manually first, long as that works you should be ok.

      Delete
  2. I keep getting the error: "The username cannot be found"
    I got the list of usernames and put them into a text file.
    I looks like the script is checking the usernames on the server its running on and not the Domain....any ideas?

    ReplyDelete
    Replies
    1. Just update this line as below and insert your domain name:

      NET USER %USER% /DOMAIN mydomain.com /scriptpath:login.bat

      Delete