Showing posts with label bat file. Show all posts
Showing posts with label bat file. Show all posts

Monday, January 29, 2007

RestartComputer.bat: Restart server

Author: Robert Lawson
Environment: Windows Server, Active Directory
Description: This script will restart a windows server. This is handy to schedule a restart "after hours", after updates have been applied.
Code:
rem file   : RestartComputer.bat
rem purpose: restart computer
rem author : Robert Lawson, 29-Jan-2007
rem /l = logs current user off immediately!
rem /r = restart after shutdown
SHUTDOWN /r /d p:0:0 /c "Monthly restart to fix haning EAS client" /l

Monday, December 25, 2006

dsq1.bat: common AD queries

Author: Robert Lawson
Environment: Windows Server, Active Directory
Description: These are the most common queries I use to gather information from Active Directory, and usually import results into Excel for auditing and/or reporting. I do save most common OU's to simply cut-and-paste, else I'll use ADSI-edit to get the OU. I simply remove the REM from query I want to use, then execute as such:

dsq1.bat>dsq1.txt
Uses:
- Stale passwords: report users who password is due to expire, call and educate before it expires. I did this for 1 year… no longer an issue
- Inactive computers: monitored for a year, removed manually. Then on negotiating with User Services, agreed to 9month and automatically remove.
- Mailbox store: audit that conventions were being followed: students, employees, non-named accounts, inactive…
- Group membership: quick way to get e-list of members
- Security: members of Domain Admin, Enterprise Admin..
- Ad hoc queries: total number of active students, employees….
Code:

rem file   : dsq1.bat
rem purpose: AD Query's
rem create : 25-dec-2006/r lawson
rem
rem notes
rem - un-REM query you want to use
rem - execute to verify no syntax error
rem - dsq1.cmd>dsq1.txt
rem - Import dsq1.txt into Excel for reporting

rem computers, in AV, inactive for 1 year
rem dsquery computer "DC=school,DC=edu" -inactive 52 -limit 0 dsget computer -samid -dn

rem all computer names
rem
rem dsquery computer "OU=AvServers,OU=location,DC=school,DC=edu" -limit 0 dsget computer -samid

rem computers, inactive for x weeks
rem dsquery computer "OU=Computer Accounts,DC=school,DC=edu" -limit 0 dsget computer -samid -sid -desc -loc
rem dsquery computer "OU=Computer Accounts,DC=school,DC=edu" -o rdn -limit 0 -inactive 12
rem dsquery * "OU=Computers,OU=Calabasas,DC=school,DC=edu" -attr cn operatingSystem operatingSystemServicePack lastlogonTimeStamp -limit 0


rem users, in Calabasas campus, with disabled accounts
rem dsquery user "OU=Calabasas,DC=school,DC=edu" -disabled -limit 0

rem all users, inactive for 1 year
rem dsquery user "DC=school,DC=edu" -inactive 26 -limit 0 dsget user -samid -display -disabled -dn

rem all users, who have not changed pw for 60 days
rem dsquery user "DC=school,DC=edu" -stalepwd 60 -limit 0
rem users who's password will expire in 5 days (90-85)
rem dsquery user "OU=Staff,OU=Users,OU=location,DC=school,DC=edu" -stalepwd 83 -limit 0 dsget user -samid -email -empid -display -title -dept -tel -office -disabled -canchpwd -pwdneverexpires -acctexpires -mustchpwd

rem all email enabled users
rem dsquery * "DC=school,DC=edu" -filter "(&(objectCategory=Person)(objectClass=User)(homeMDB=*)) " -attr sAMAccountName displayName distinguishedName homeMDB userAccountControl -limit 0


rem all Users in an OU
dsquery * "OU=Students,DC=school,DC=edu" -filter "(&(objectCategory=Person)(objectClass=User))" -attr sAMAccountName displayName givenName description sn employeeID employeeNumber department homeMDB userAccountControl -limit 0

rem another form of users in an OU
rem dsquery user "OU=janitors,OU=location,DC=school,DC=edu" -limit 0 dsget user -samid -display -disabled -canchpwd -pwdneverexpires -acctexpires

rem single user, display info
rem dsquery user "CN=edummy,OU=Consultants And Temporary Users,OU=All AV Employees,OU=location,DC=school,DC=edu" dsget user -samid -display -disabled -canchpwd -pwdneverexpires -acctexpires

rem get members of a group
rem dsquery group "OU=ADgroups,OU=location,DC=school,DC=edu" -name "grpUGClassOf2012"dsget group -members -expanddsget user -samid -fn -ln -title -dept -disabled -mustchpwd -pwdneverexpires

rem groups
rem dsquery * "OU=ADgroups,OU=location,DC=school,DC=edu" -filter "(&(objectCategory=Group)(objectClass=Group))" -limit 0 -attr sAMAccountName displayName name description mail member

rem users 1: AD attributes, LDAP filter
rem dsquery * "DC=school,DC=edu" -filter "(&(objectCategory=Person)(objectClass=User))" -limit 0 -attr sAMAccountName displayName employeeID employeeNumber userAccountControl delivContLength submissionContLength mdbUseDefaults mDBStorageQuota mDBOverHardQuotaLimit msExchHideFromAddressLists memberOf

rem Users 2: AD attributes
rem dsquery * "OU=Students,OU=location,DC=school,DC=edu" -limit 0 -attr sAMAccountName displayName employeeID employeeNumber telephoneNumber ciscoEcsbuDtmfId physicalDeliveryOfficeName createTimeStamp whenCreated department title userAccountControl msExchHideFromAddressLists mail protocolSettings homeMDB memberOf

rem Users 3: Query elements
rem dsquery user "OU=NewStudents,OU=Users,DC=school,DC=edu" -limit 0 dsget user -samid -email -empid -display -title -dept -tel -office -disabled -pwdneverexpires -acctexpires -mustchpwd


rem Users 3: Query elements, brief
rem dsquery user "OU=NewStudents,DC=school,DC=edu" -limit 0 dsget user -samid -fn -ln

rem computers with bogus container setting

rem dsquery * "OU=Computer Accounts,DC=school,DC=edu" -limit 0 -attr sAMAccountName CN msDS-Approx-Immed-Subordinates memberOf

rem find oddities with T/S (doesn't seem to work May 2006)
rem dsquery * "OU=Users,DC=school,DC=edu" -limit 0 -attr sAMAccountName displayName department title scriptPath TerminalServicesProfilePath TerminalServicesHomeDirectory TerminalServicesHomeDrive TerminalServicesWorkDirectory TerminalServicesInitialProgram

Sunday, December 25, 2005

UdriveSetup.cmd: Create network drive

Author: Robert Lawson
Environment: Windows Server, Active Directory
Description: This command is used to manually setup a network share for a user. This was used to test syntax and use of xcacls. Domain Admin membership required to run.
Code:
@ECHO OFF
rem file : UdriveSetup.cmd
rem purpose: setup network share drive
rem create : 25-dec-2005/r lawson
rem
rem notes
rem 1. Download xcacls from microsoft
rem 2. Follow setup from KB# 825751
rem 3. Change user name for 'edummy'
rem
rem UdriveSetup.cmd edummy \\server1\UserStore
rem UdriveSetup.cmd edummy c:\temp
SET THISCOMMAND=%0
SET LOGFILE=%TEMP%\UdriveSetup.log

:checkForHelp
@if /i [%1] == [-h] (goto usage)
@if /i [%1] == [/h] (goto usage)
@if /i [%1] == [-?] (goto usage)
@if /i [%1] == [/?] (goto usage)
@if /i [%1] == [-help] (goto usage)
@if /i [%1] == [/help] (goto usage)

:verifyParams
@if [%1] == [] (goto parameterError)
@if [%2] == [] (goto parameterError)
SET NEWDIRECTORY=%2\%1
goto verifyDirectory

:parameterError
@Echo %DATE% %TIME% FAILED: Not enough parameters>>%LOGFILE%
goto end

:verifyDirectory
rem @if /i NOT EXIST [%NEWDIRECTORY%] (goto Execute)
DIR %NEWDIRECTORY%>NUL
IF ERRORLEVEL 1 GOTO Execute
@Echo %DATE% %TIME% FAILED: Directory already exists>>%LOGFILE%
goto end

:usage
@echo Create a network U drive for a user
@echo
@echo Usage: %THISCOMMAND% [sAMAccountname] [UNC]
@echo
@echo [sAMAccountname] = user id
@echo [UNC] = Where to create directory using the sAMAccountname
@echo
pause
goto end

:Execute
MKDIR %NEWDIRECTORY%
rem remove the /Q to display
"%SystemRoot%\system32\cscript.exe" "%SystemRoot%\system32\xcacls.vbs" %NEWDIRECTORY% /O SOKA/%1 /I ENABLE /Q
@Echo %DATE% %TIME% SUCCESS: Directory created: %NEWDIRECTORY%>>%LOGFILE%

:end
ENDLOCAL

Tuesday, September 20, 2005

RmvOldComputers.bat: remove unused computer objects

Author: Robert Lawson
Environment: Windows Server, Active Directory
Description: This script runs nightly to remove Active Directory computer objects that have not been used in a 9 month period of time. Why 9 months? I think it is too long, but that's the guesstimate the longest time a student would be off-campus, including semester abroad. Removing the object effectively dis-joins computer from the AD domain. This also logs results to audit-log file... to help explain any issues.
Code:
rem File: RmvOldComputers.bat
rem
rem Purpose: Remove computers from domain older than 'x' weeks
rem
rem Author/Date: Robert Lawson/20-Sep-2005
rem
rem Notes:
rem 1. inactive is expressed in weeks (9 months = 36 weeks).
rem 2. computers that had shared printer will fail ("..is container and contains objects..")
rem
date/t>>C:\Temp\ListOldComputers.txt
dsquery computer "OU=Computer Accounts,DC=uni,DC=edu" -o rdn -limit 0 -inactive 36>>C:\temp\ListOldComputers.txt
rem
rem dsquery computer "OU=Computer Accounts,DC=uni,DC=edu" -limit 0 -inactive 36dsrm -noprompt -c -q

Monday, April 11, 2005

FindPST.cmd: Find Outlook pst files on user's computer

Author: Robert Lawson
Environment: Windows Server, Active Directory
Description: This script was used to find pst files on user's desktop systems. This was used with a higher level script that included all employee's and their comptuer name. This was final phase of getting all Outlook pst files back into our Exchange/EAS email system.
Code:
rem FindPST.cmd
rem Parm1 Computer Name Required
rem Parm2 Domain Admin User Required
rem Parm3 DA password Required
rem T:w (last written to), T:a (last access)
ECHO Checking %1 on C drive on %DATE% at %TIME%
ECHO Checking %1 on C drive on %DATE% at %TIME%>>FindPST.txt
PING %1>> FindPST.txt
NET USE \\%1\ipc$ %3 /USER:soka\%2
DIR \\%1\c$\*.pst /S /Q /T:w>> FindPST.txt
NET USE \\%1\ipc$ /DELETE