DEP Notify and Monterey 12.2+3 Python call

Anonymous
Not applicable

I have been trying to sus out what is calling Python2(deprecated path) in my deployment workflow as we now get the JAMF Agent error that many others are seeing.  I have finally narrowed it down as to where it is...yet no idea how to correct it.  it looks like the script for calling/configuring DEP Notify that was supplied a while back has one single python call to get the current user:

# After the Apple Setup completed. Now safe to grab the current user.
  CURRENT_USER=$(/usr/bin/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");')
  echo "$(date "+%a %h %d %H:%M:%S"): Current user set to $CURRENT_USER." >> "$DEP_NOTIFY_DEBUG"

Does anyone have any recommendations as to how to correct/change this bit?  I've tried a few things and keep getting errors, yet this is a pretty complex script so I figured I would reach out.  

1 ACCEPTED SOLUTION

karthikeyan_mac
Contributor III

 

CURRENT_USER=$(/usr/bin/stat -f "%Su" /dev/console)

 

 - This is used in DEPNotify Starter but there are many options.

 

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

You can refer the below article by @arminBriegel where explains the difference of each method. Thank you @arminBriegel 

https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/

View solution in original post

12 REPLIES 12

arnoldtaw
New Contributor III
CURRENT_USER=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

Source: https://www.jamf.com/blog/python2-is-gone-apple-macos/?mkt_tok=NzA0LVZZUC01MjcAAAGCc7E92oVwPQc9mrlwI... 

Source: https://scriptingosx.com/2019/09/get-current-user-in-shell-scripts-on-macos/

Anonymous
Not applicable

@arnoldtaw ok so I didn't cut out the rest of the statement in the script like what you did there, I tried that exact statement prior to posting.  should I keep the echo statement below?  it should still work in theory.  I'll give it a whirl. Thank you

karthikeyan_mac
Contributor III

 

CURRENT_USER=$(/usr/bin/stat -f "%Su" /dev/console)

 

 - This is used in DEPNotify Starter but there are many options.

 

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

You can refer the below article by @arminBriegel where explains the difference of each method. Thank you @arminBriegel 

https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/

Anonymous
Not applicable

this was the full string that was tested:

#After the Apple Setup completed. Now safe to grab the current user.
  CURRENT_USER=$(/usr/bin/stat -f "%Su" /dev/console)
  echo "$(date "+%a %h %d %H:%M:%S"): Current user set to $CURRENT_USER." >> "$DEP_NOTIFY_DEBUG"

Works like a charm and eliminates any errors when running the workflow.

markdmatthews
Contributor

Also had good luck with curentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

gmorgan
New Contributor III

If we're talking about the DEP script from JAMF from 2018 (v2.0.1), what line in the script is this?  I think I'm having the same issue and want to check it out.

gmorgan
New Contributor III

Nevermind.  I found it.  Lines 512-514.

Anonymous
Not applicable

I was going to say somewhere near 500 - I ended up just searching for the python callouts with every script I had in the system using an editor so I could find them easier.  a number of replacements listed here will solve the issue vs re-tooling for v3.

user-vNWzUKNZkx
New Contributor

Hey, 

First time poster here I am struggling with this currently. The way we were doing this in the past was a pop up with prompt the user to put name of computer, asset tag, Site and if student or staff device. This step fails now as a result of this error 

Our old code was this 

# After the Apple Setup completed. Now safe to grab the current user.
CURRENT_USER=$(/usr/bin/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");')
echo "$(date "+%a %h %d %H:%M:%S"): Current user set to $CURRENT_USER." >> "$DEP_NOTIFY_DEBUG"

 

tried a few of the above but the current one is this:

# After the Apple Setup completed. Now safe to grab the current user.
CURRENT_USER=$(/usr/bin/stat -f "%Su" /dev/console)
echo "$(date "+%a %h %d %H:%M:%S"): curentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

 

I am getting this error:

Script result: /Library/Application Support/JAMF/tmp/DEPNotify Config Script - DEP: line 811: unexpected EOF while looking for matching `"'
/Library/Application Support/JAMF/tmp/DEPNotify Config Script - DEP: line 816: syntax error: unexpected end of file
Error running script: return code was 2.

 

I could use some help getting the functionality back. I worked with Jamf directly and was told to ask here also.

 

Thanks for any help you can provide! 

gmorgan
New Contributor III

You have errors in lines 811 and 816.  Missing parentheses or something. Not sure without knowing what line is which.  Here's ours:

# After the Apple Setup completed. Now safe to grab the current user and user ID
CURRENT_USER=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }')
CURRENT_USER_ID=$(id -u $CURRENT_USER)

That did the trick! replace the lines of code and its working again. Thanks!

I don't know why there were errors in line 811 but they went away. So no issues there.

Thanks again!

Great! Enjoy.