Skip to main content

We have been needing to find a script that works to delete all the home folders in the Users directory except for four that we need to keep (map, tech, student, and admn). We have a setup of mobile home directories that are cached on each laptop when network users first use them. Here is the script that support helped build. I am getting a syntax error when running it at the first "for".



#!/bin/sh
########################################################################################################
#
# Copyright (c) 2013, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
# PURPOSE
# - Remove or Move local user directories from AD bound machines that have "Force Local Homes" checked
#
# HISTORY
# Version: 1.0
# - Created by Bram Cohen October 15. 2013
#
####################################################################################################

username1="map"
username2="tech"
username3="student"
username4="admn"


####################################################################################################
# SCRIPT OPERATIONS - - REALLY!!! - DO NOT MODIFY BELOW THIS LINE
####################################################################################################
RESULT=""
for U in /Users/*; do
if [ -d "$U" ]; then
if [ "$U" == "/Users/Shared" ] || [ "$U" == "/Users/Guest" ]; then
/bin/echo "Found $U, ignored"
USERNAME=`/bin/echo $U | tr '/' ' ' | awk '{print $NF}'`
RESULT=`echo "$RESULT$USERNAME-IGNORED "`
else
/bin/echo "Found $U, continuing..."
USERNAME=`/bin/echo $U | tr '/' ' ' | awk '{print $NF}'`
/bin/echo "Parsed username as: $USERNAME"
ADMINCHECK=`/usr/bin/dsmemberutil checkmembership -U $USERNAME -G admin | awk '{print $3}'`
if [ "$ADMINCHECK" == "not" ]; then
if [ "$USERNAME" == "$username1" ] || [ "$USERNAME" == "$username2" ] || [ "$USERNAME" == "$username3" ] || [ "$USERNAME" == "$username4" ]; then
/bin/echo "$USERNAME is on the exempt list, ignoring..."
RESULT=`echo "$RESULT$USERNAME-EXEMPT "`
else
/bin/echo "$USERNAME not an Admin nor exempt, taking action..."
RESULT=`echo "$RESULT$USERNAME-Action Taken "`
#/bin/echo "Forcing the removal of $U" && /bin/rm -rf $U
fi
else
/bin/echo "$USERNAME was found to be a Local Admin, ignoring"
RESULT=`echo "$RESULT$USERNAME-ADMIN "`
fi
fi
fi
done
echo ""
echo "============================================================"
echo "Summary: $RESULT"
echo "============================================================"

Here you go:



Running command ls -lO /Users/cbcolli/.Trash/CONTENTS/AUDIO/0001PL00.MXF...
Result of command:
-rwxrwxrwx 1 cbcolli POINTPARKStudents uchg 28381312 Apr 1 13:03 /Users/cbcolli/.Trash/CONTENTS/AUDIO/0001PL00.MXF


Wow! that was fast. That appears to be the user immutable flag. The solution to this is going to be to run 'chflags -R nouchg' on the user's directory prior to running the shutil.rmtree operation. I'll edit the script above so that i incorporates the chflags command into it.


Awesome, thanks for taking the time to diagnose this and update the script. It's a huge help!


Glad to help! I've just finished making the changes. I made a few other alterations to clean up a few things. Let me know if you have any questions about what i did.


I just tried out the new script and it worked great on the systems that were previously experiencing problems. Thanks again!


Sorry @denmoff been bogged down in deployment!
Can you send on a link to the updated script and I'll run it through tomorrow.
Thanks for the support!


@Zvordauk I've made the changes in my original script up above.


D'oh! That's the problem with skimming stuff after an 18 hour shift :(



I'm probably doing something very stupid but I'm getting the following error:



Script result: File "/Library/Application Support/JAMF/tmp/RemoveExtraUsers.py", line 41
except Exception, e:
^
SyntaxError: invalid syntax


@Zvordauk Probably because everything in the "try" statement is commented out. I've changed the script again so that has the print statement uncommented after the try statement. But don't forget that i've commented out the shutil.rmtree statement. You'll need to uncomment that line before it will actually do anything irrevocable.


Reply