Tag Archive - scripts

Windows Server 2003 – AutoAdminLogon

AutoAdminLogon is pretty handy for those instances where a computer or server must be logged in at all times with an interactive session.  Microsoft has documented a few changes that need to be done in the registry to enable this feature, http://support.microsoft.com/kb/324737


Please note, if your setting up AutoAdminLogon for a workstation or server, don’t forget to set the ‘DontDisplayLastUserName’ key to ’0′.  If it is non-existant or set to ’1′ the server will remember whoever was logged into the console last, over-riding the ‘DefaultUserName’ key that was set in the registry.  I’m not sure why Microsoft didn’t include this in their documentation, but it needs to be there.
For the servers that I’ve set this up on, I’ve also added a .bat file that will lock the screen after logging in.  Create a bat file and put the following in it.
rundll32.exe user32.dll, LockWorkStation

Put the bat file in the startup items folder.  Microsoft recommends you restart your server at that time to test it out.  If you don’t get the chance to restart it, make sure you test it out at some point.

The “Create Local Users and Do Other Stuff Too” script

This script does a lot in one fowl swoop.

1. It creates a new local user on the machine, unless the user already exists of course
2. Renames the local ‘Administrator’ account to another name
3. Changes the renamed ‘Administrator’ account’s password
4. Creates a file in ‘c:\windows’ so the script won’t run on the next startup
5. Adds a domain group to a local computer group

You can change the names/passwords accordingly. I put the things that need to be changed in italics. It works well in a .vbs script and used as a startup GPO to deploy it. The script will check for the file it created in c:\windows, if it doesn’t find it, it won’t run.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("C:\Windows\accountchanged.txt") Then
WScript.Quit
End if

If objFSO.FileExists("C:\winnt\system.ini") Then
WScript.Quit
End if

' Get's the computer name
set objNetwork=createobject("wscript.network")
 strComputer=objNetwork.computername

'Loads the administrators group
set objGroup=GetObject("WinNT://" & strComputer & "/Administrators,group")

' Run the Load method
Load

' Encapsulates the processing of this script
Sub Load()

' Create the users
CreateUser "PUT_USERNAME_HERE","PUT_PASSWORD_HERE","PUT_GROUP_HERE", "Local admin account"
' MsgBox "Complete!"

End Sub

' Create the local user
Sub CreateUser(userName, password, group, description)
' Check to see if the user exists; if so, then skip
If NOT CheckIfUserExists(userName) Then
Set objComputer = GetObject("WinNT://" & strComputer & "")
Set objUser = objComputer.Create("user", userName)
objUser.SetPassword password
objUser.FullName = userName
objUser.Description = description
objUser.Put "UserFlags", 65600  ' Sets Password Never Expires to TRUE
                                ' and sets User Can't Change Password to TRUE
objUser.SetInfo
objGroup.Add(objUser.ADsPath)
Else
' MsgBox userName & " already exists!"
End If
End Sub

' Check to see if user exists
Function CheckIfUserExists(userName)
Set objComputer = GetObject("WinNT://" & strComputer & "")
objComputer.Filter = Array("user")
intFound = 0

For Each User In objComputer
If lcase(User.Name) = lcase(userName) Then
intFound = 1  
End If   
Next

If intFound = 1 Then
CheckIfUserExists = True
Else
CheckIfUserExists = False
End If
End Function

'Rename Administrator account
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAccounts = objWMIService.ExecQuery _
("Select * From Win32_UserAccount Where LocalAccount = True And Name = 'Administrator'")

For Each objAccount in colAccounts
objAccount.Rename "PUT_NEW_USERNAME_HERE"
Next

' Change new username's password
Set objUser = GetObject("WinNT://" & strComputer & "/PUT_NEW_USERNAME_HERE,user")
objUser.SetPassword "PUT_PASSWORD_HERE"
objUser.SetInfo

' Add DOMAIN GROUP to local Administrators group
set objAdmins = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objGroup1 = GetObject("WinNT://DOMAIN/DOMAIN_GROUP")

if not objAdmins.ismember(objGroup1.adspath) then
 objAdmins.add objGroup1.adspath
end if

' Create check file
Set objOutFile = objFSO.OpenTextFile("C:\Windows\accountchanged.txt", 8, True)
objOutFile.WriteLine("Completed " & Date)
objOutFile.Close

 

Download the file here: CreateUser

NT Backup script

I was asked to create a script that would be an easy double click backup solution for sales people who are on the road. The following is a batch file that will do a differential backup of which ever folder you specify in the script. You only need to change the destination drive and source. It will look for a full backup, if one isn’t found, it will do one and do differential backups there after.

@echo off

:Variables – Change drive letter to match your backup drive
set destdrive=F:
set source=C:\Documents and Settings\cpratt

:You shouldn’t need to change any of these
set destination=%destdrive%\Backup
set server=%computername%
set textlogpath=%destdrive%\Backup\%computername%-Backup.txt
set archivepath=%destdrive%\Backup\OldLogs

:Set Time

for /F “tokens=1-4 delims=/ ” %%i in (‘date /t’) do (
set Month=%%j
set Day=%%k
set Year=%%l
set Date=%%j-%%k-%%l
set dirdate=%%j%%k%%l
)

for /f “Tokens=1-2 delims=/ ” %%i in (‘time /t’) do (
set tm=%%i
set ampm=%%j
)

echo.
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo Backup.bat v1
echo.
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo.

set logpath=C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data
if not exist “%archivepath%” md “%archivepath%” & echo “Archive Path did not exist, so it was created.” >> %textlogpath%
if exist “%textlogpath%” del “%textlogpath%” & echo “Previous %textlogpath% existed, so it was deleted.” >> %textlogpath%
if exist “%logpath%\*.log” del “%logpath%\*.log” & echo “Previous ntbackup logfile existed, so it was deleted.” >> %textlogpath%

:Backup Time

echo Backup started at %tm% %ampm% on %Year%-%Month%-%Day% into “%destination%” >> %textlogpath%
echo %server% backup starts %tm% %ampm% on %Year%-%Month%-%Day% into “%destination%”

:Check for full backup
if exist “%destination%\%server%-FullBackup.bkf” echo “Full backup exists” & c:\windows\system32\ntbackup.exe backup “@C:\Windows\backupdiff.bks” /a /d “%server%-Backup” /v:no /r:no /rs:no /hc:off /m differential /j “%server%-Backup” /l:s /f “%destination%\%server%-Differential.bkf” >> %textlogpath%
if not exist “%destination%\%server%-FullBackup.bkf” echo “Full backup does not exist” & c:\windows\system32\ntbackup.exe backup “@C:\Windows\backupdiff.bks” /a /d “%server%-Backup” /v:no /r:no /rs:no /hc:off /m differential /j “%server%-Backup” /l:s /f “%destination%\%server%-FullBackup.bkf” >> %textlogpath%

:RenameLogs

if exist “%logpath%\*.log” ren “%logpath%\*.log” %server%-Backup.log
if exist “%logpath%\%server%-Backup.log” copy “%logpath%\%server%-Backup.log” “%archivepath%” /y

echo NTBackup.exe .log file archived at “%archivepath%\%note%-%server%-%Year%-%Month%-%Day%.log” >> %textlogpath%
echo alcBackup.bat .txt file archived at “%archivepath%\%Year%-%Month%-%Day%-%server%-%note%.txt” >> “%textlogpath%”

:Archive
if exist “%archivepath%\templog.txt” del “%archivepath%\templog.txt”
if exist %textlogpath% copy “%textlogpath%” “%archivepath%\templog.txt”
if exist “%archivepath%\%Year%-%Month%-%Day%-%server%-%note%.txt” del %archivepath%\%Year%-%Month%-%Day%-%server%-%note%.txt

if exist %archivepath%\templog.txt ren %archivepath%\templog.txt “%Year%-%Month%-%Day%-%server%-%note%.txt”

if exist “%archivepath%\%note%-%server%-%Year%-%Month%-%Day%.log” del “%archivepath%\%note%-%server%-%Year%-%Month%-%Day%.log”

if exist “%archivepath%\%server%-Backup.log” ren “%archivepath%\%server%-Backup.log” “%note%-%server%-%Year%-%Month%-%Day%.log”

if exist “%archivepath%\%server%-Backup.log” del “%archivepath%\%server%-Backup.log”
if exist “%logpath%\%server%-Backup.log” del “%logpath%\%server%-Backup.log”

if exist “%textlogpath%” del “%textlogpath%”

goto eof
:eof