Mac Set Name Script fails on Monterey 12.3

heavymeta80
New Contributor III

Greetings all,

I have a script (I did not create this) to allow techs to manually set the Mac computer, localhost, and host names that runs as part of a policy.  Until OS 12.3 came out, it worked just fine.  This is the script:

#!/bin/zsh
defaults=/usr/bin/defaults

CurrentUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
MacName=$(sudo -u "$CurrentUser" /usr/bin/osascript -e 'tell application "System Events" to set MacName to text returned of (display dialog "Enter the new Computer Name:" buttons "Continue" default button "Continue" default answer "" with icon 1)')

scutil --set ComputerName "$MacName"
scutil --set HostName "$MacName"
scutil --set LocalHostName "$MacName"
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "$MacName"

 

On 12.3 the script fails due to python command not being found.  So I found that with OS 12.3 Apple finally disabled Python 2.7 entirely so I thought installing Python3 as well as modifying the script to call python3 instead of python would do it.  

Nope!

Now, Jamf Pro shows this as the error in the log:

 

Script result: Traceback (most recent call last):
File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'SystemConfiguration' usage: sudo -h | -K | -k | -V usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user] usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command] usage: sudo [-AbEHknPS] [-C num] [-D directory] [-g group] [-h host] [-p prompt] [-R directory] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>] usage: sudo -e [-AknS] [-C num] [-D directory] [-g group] [-h host] [-p prompt] [-R directory] [-T timeout] [-u user] file ...

 

Scripting is still a weak area for me so I humbly ask the more script-savvy among you to help.  If there's a better way to display a prompt for my techs to manually enter a computer name than this, I'm open to suggestions!  If it's a script of some kind that can run as part of a policy that would be ideal. 

 

Thank you!

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

As of Monterey 12.3, you should move away from using Python, at least for the purpose of this script. There's a better or at least easier way to get the same "current user" info purely with the shell and some built in binaries.

Try replacing the entire CurrentUser line with this instead:

CurrentUser=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')

View solution in original post

11 REPLIES 11

fsteele
New Contributor II

One of the things that makes Python great is the available libraries, and the SystemConfiguration library is part of something called PyObjC, which lets Python talk to Mac Cocoa libraries. The Python you installed probably doesn't have PyObjC. There are some Mac admins who are building their own Python distributions for use with their fleet, and Greg Neagle has created a tool to build a custom Python framework containing PyObjC called relocatable-python. 

If that sounds like it might be too much work for you, you probably have other options. We use DEP_Notify as part of our provisioning workflow, and we prompt techs to enter the computer name within DEP_Notify's script.

JamfHelper is a utility that's part of your Jamf install, and it can display dialogs and much more, and you can trigger it from within essentially any policy. Here's a link to a presentation on JamfHelper at the University of Utah.

heavymeta80
New Contributor III

Thank you for the quick reply fsteele!

The PyObjC was the key alright.  I checked out the relocatable-python github but was having some trouble deploying it so I just ran the basic install 

$ pip3 install -U pyobjc

Once that ran, I was able to run my script with the python3 call and it worked the same way it always has.

So my next question is if there is an easy way to run the python3 install, the pip3 upgrade, and the pyobjc install as a script which I can set to run at the top of the policy before anything else so the script works.  I currently have this in my scripts as a possibility but I don't know if it will work:

python3
pip3 install --upgrade pip
pip3 install -U pyobjc

The script used to be just a python3 install using the first line which would prompt the user to download the command line tools.  This worked (even though the download and install took FOREVER) so I wonder if I can just add the pip lines and set that to Before in my policy.

Thank you again!

  

mm2270
Legendary Contributor III

As of Monterey 12.3, you should move away from using Python, at least for the purpose of this script. There's a better or at least easier way to get the same "current user" info purely with the shell and some built in binaries.

Try replacing the entire CurrentUser line with this instead:

CurrentUser=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')

heavymeta80
New Contributor III

mm2270, that is even better!  I replaced the line and it worked like a charm!  Thank you very much!

I want to test it on a machine without python at all just for my sanity but I'll be sure to mark this as the solution if it passes.

heavymeta80
New Contributor III

All set!  Thank you again!

Anonymous
Not applicable

Only an idea:
Jamf itself is grabbing some informations of the registered clients in variables.
maybe it could be easier, to use the Jamf variable " $3" this is the current user, too.

heavymeta80
New Contributor III

Well, for some reason the name reverts to the default of "username's Macbook Pro" post-reboot in the sharing app while the NetBios name does show correctly under the Computer Name field.  When I launch the Terminal, the hostname is the default as well.  Anyone know why this is happening?

mm2270
Legendary Contributor III

Try adding this right after the new name is applied

/usr/bin/dscacheutil -flushcache

I've seen cases where the system aggressively caches the existing name, and flushing the cache as above can help make sure the new name sticks.

heavymeta80
New Contributor III

Good call!  Would putting that with a blank line between the last line of code (last line is line 10 so this would be line 12) work or would somewhere else work better?  

heavymeta80
New Contributor III

yep, that did it!  name shows up on all devices as well as Jamf Pro.  Thanks again!