Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Monday, July 20, 2009

Script - Active Directory Group Extract (Windows)

There comes a time in every Sysadmin's life when they get a simple request from management: "..also, can you give me a list of everyone in that group?"

Simple enough, right?

Even though it would make perfect sense to right-click the security group and 'extract to text file', it's not THAT easy.

The quickest method to get a listing of users for a specific group is to use VBScript.

1. Copy the script below into a file named "export_security_group_users.vbs".
2. Fill in the information specific to your AD deployment (group, OU, domain).
3. Run the script from the command line: "cscript.exe export_security_group_users.vbs > extractfile.txt

"extractfile.txt" will have entries with the following format:
CN=USERID,OU=GROUPS,OU=SECONDLEVELOU,OU=TOPLEVELOU,DC=foobar,DC=com

export_security_group_users.vbs:
----------------------------------------------------------
'export_security_group_users.vbs
'This script exports users from a specific distribution group
'Usage: cscript.exe export_security_group_users.vbs > extractfile.txt
'
'Andrew Elliott
'17-july-2008
'

On Error Resume Next

Set objGroup = GetObject _
("LDAP://cn=EXTRACTTHISGROUP,ou=GROUPS,ou=SECONDLEVELOU,ou=TOPLEVELOU,dc=foobar,dc=com")
objGroup.GetInfo

arrMemberOf = objGroup.GetEx("member")

WScript.Echo "Members:"

For Each strMember in arrMemberOf
WScript.echo strMember
Next

----------------------------------------------------------

*-You'll need to have correct permissions to extract this information

Wednesday, July 8, 2009

Script - kill all users except root via 'skill'

Eventually you'll get to the point where you need a script that just kicks all the users off except the 'root' account (provided you're logged in as root or su).

This script:

checks to see that the user running the script is 'root'
does a 'w' listing (shows who's currently logged on) and an inverted grep '-v' to find the users
kills off the user accounts with 'skill'

(Enter other account exceptions into the bold text)

----------------------------------------------------------
#!/bin/bash
# logoffeveryone.sh
# Written by Andrew Elliott, 9-APR-2009
#

USER_LOG=/root/loggedonusers.txt
ROOT_UID=0

if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script!"
exit
fi

cd /root

echo "-----------------------------" > $CHAGE_LOG

w | cut -f 1 -d " " | grep -v 'root' | grep -v 'anyotheruser' | while read TEMP1
do
echo "Logging off user: $TEMP1"
skill -KILL -u $TEMP1
done

exit 0
----------------------------------------------------------

Script - Remove files older than 1 day from /root

A quick bash script that I wrote that does the following:

Checks to see if the user is root (good to do with all your scripts if you 'sudo')
finds all files older than 1 day (-ctime +1)
deletes the files
sends an email to 'root' with a small report of the deleted files

(the numb -3 is to calculate the number of files deleted - there are 3 header lines in the report)
(the statement in bold allows you to specify file formats to delete...*.csv in my case...)

------------------------------------------
#!/bin/bash
# rmroottemp.sh
# Written by Andrew Elliott, 20-feb-2009
#

RM_LOG=/root/rmroottemplog.txt
ROOT_UID=0

if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script!"
exit
fi

cd /root

echo "-----------------------------" > $RM_LOG
echo "Report for files removed from /root" >> $RM_LOG
echo "-----------------------------" >> $RM_LOG

find /root -ctime +1 -maxdepth 1 | grep 'searchtermhere' | while read TEMP1
do
echo "$TEMP1" >> $RM_LOG
rm -rf $TEMP1
done

NUMB=`cat $RM_LOG | wc | cut -c1-8 | sed 's/^[ ]*//'`
NUMB=$(($NUMB - 3))
/bin/mail -s "Root temp file cleanup $NUMB files deleted" root < $RM_LOG
rm -rf $RM_LOG
exit 0
------------------------------------------