Use the user folder to set computername, local host name and host name via jamf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-19-2021 10:23 PM
We are working on deploying Jamf Connect and I am looking for a way where we can use the user folder to set the computer name, local host name and host name in the background via Jamf pro with user or admin interaction,
I am also looking into how can I append -mba if the computer is MacBookAir, and -mbp if computer is MacBook Pro
Does anyone have a script which can help this?
thank you,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-20-2021 02:15 AM
I think all you'd need to do is grep the name of the user folder, assign it to a variable and then use the standard rename commands in a script to assign those names to the value of that variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2021 03:20 AM - edited 12-20-2021 03:23 AM
You can try by querying the current logged in username and model identifier. $loggedInUser may be different before the user is logged in so test it.
#!/bin/sh
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )
getmodel=`sysctl -n hw.model`
if [[ $getmodel == *"MacBookPro"* ]]; then
scutil --set HostName "$loggedInUser-mbp"
scutil --set LocalHostName "$loggedInUser-mbp"
scutil --set ComputerName "$loggedInUser-mbp"
elif [[ $getmodel == *"MacBookAir"* ]]; then
scutil --set HostName "$loggedInUser-mba"
scutil --set LocalHostName "$loggedInUser-mba"
scutil --set ComputerName "$loggedInUser-mba"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-20-2021 03:04 PM
just testing the above i get the following
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )
awk: syntax error at source line 1
context is
/Name 😕 && ! /loginwindow/ >>> { <<<
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-20-2021 03:05 PM
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )
awk: syntax error at source line 1
context is
/Name 😕 && ! /loginwindow/ >>> { <<<
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2021 12:04 AM - edited 12-21-2021 12:07 AM
Attaching as screenshot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-21-2021 01:21 AM
Also, try this instead if that doesn't work:
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2021 03:34 AM - edited 12-20-2021 03:38 AM
You can actually instruct the script to wait until a user is logged-in with this little function. Add this to the start of the above script and you should be good to go, but do test it first as always!
function wait_for_user () {
# Wait for the dock to determine the current user
DOCK_STATUS=$(pgrep -x Dock)
echo "Waiting for Desktop..."
while [[ "$DOCK_STATUS" == "" ]]; do
echo "Desktop is not loaded; waiting..."
sleep 5
DOCK_STATUS=$(pgrep -x Dock)
done
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ { print $3 }' )
echo "$loggedInUser is logged in and at the desktop; continuing."
}
wait_for_user
Naturally, that function can be used for anything you want to run only when a user is actually logged-in to the Mac.
Remember that you'll need to still declare $loggedInUser outside of the function, because variables declared within functions only exist within the function. Therefore, you shouldn't need to change @karthikeyan_mac's script at all underneath the 'wait_for_user' function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2021 01:39 AM - edited 12-21-2021 01:41 AM
#!/bin/sh
originalName=$(networksetup -getcomputername)
echo "Computer name is currently $originalName"
# Create function to wait for the Dock to launch to confirm a user is logged-in
function wait_for_user () {
DOCK_STATUS=$(pgrep -x Dock)
echo "Waiting for Desktop..."
while [[ "$DOCK_STATUS" == "" ]]; do
echo "Desktop is not loaded; waiting..."
sleep 5
DOCK_STATUS=$(pgrep -x Dock)
done
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name && ! /loginwindow/ { print $3 }' )
echo "$loggedInUser is logged in and at the desktop; continuing."
}
# Calls the above function you just created
wait_for_user
# Get currently logged-in user and assign to variable
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name && ! /loginwindow/ { print $3 }' )
echo "Current user is $loggedInUser"
# Get Mac model and assign to variable
getModel=$(sysctl -n hw.model)
echo "Detected Mac model is $getModel"
# Put the two together and rename in the format 'username-macmodel'
if [[ $getModel == *"MacBookPro"* ]]; then
scutil --set HostName "$loggedInUser-mbp"
scutil --set LocalHostName "$loggedInUser-mbp"
scutil --set ComputerName "$loggedInUser-mbp"
elif [[ $getModel == *"MacBookAir"* ]]; then
scutil --set HostName "$loggedInUser-mba"
scutil --set LocalHostName "$loggedInUser-mba"
scutil --set ComputerName "$loggedInUser-mba"
fi
newName=$(networksetup -getcomputername)
echo "Hostname, Local name and Computer name are set to $newName"
if [[ $originalName == $newName ]]; then
echo "New name is the same as the original name, script failed"
exit 1
else
echo "Names changed successfully!"
exit 0
fi
This should be the full script. I've added comments so you/others can see what parts are doing what, and it makes it easier to troubleshoot if something goes wrong as there are echo commands for the various parts too. Please try it before pushing it!
I can't seem to do a lot about it changing the colon-forward slash to a 😕 so do bear that in mind.
