Help! Why do I get exit code 200, the script fails?

gduncan_mbo
New Contributor II

Hi All,

I need help with this part of a script I am running:

username=$(/bin/launchctl asuser "$LoggedInUID" sudo -iu "$LoggedInUser" /usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter username name and click Submit:" default answer "" buttons {"Submit"} with icon caution' -e 'text returned of result' 2>/dev/null)

when I

echo $username

It returns nothing and I cannot figure out why. I also verified the other variables return a value

4 REPLIES 4

alexjdale
Valued Contributor III

Try just this, it works for me:

username=$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter username name and click Submit:" default answer "" buttons {"Submit"} with icon caution' -e 'text returned of result' 2>/dev/null)

gduncan_mbo
New Contributor II

@alexjdale Thanks for responding, I'll test this and report the results.

gduncan_mbo
New Contributor II

@alexjdale Hi Alex, Two things:
1. This does not execute the script as the logged-in user which then leads to the users getting a security prompt.
2. However, this led me down the path of setting up PPPC, which worked as a solution for using osascripts to call System Events.
If you are interested, here is a youtube video explaining it:
https://www.youtube.com/watch?v=Po_h3KdgYmw&feature=youtu.be&t=1436s
you can get the AppleEvents config profile here to upload https://github.com/jamf/JamfPrivacyPreferencePolicyControlProfiles
and you can download the PPPC utility here https://github.com/jamf/PPPC-Utility/releases

Thanks for your help!

zanb
New Contributor III

In addition to @gduncan.mbo's response, and a reply to the syntax in the original post:

In this instance, with a Jamf policy, you are running a command under a different user session (root), which launches launchctl, which then tries to adopt the user's UID bootstrap/kernel namespace, then it tries running sudo -iu as an adopted launchctl process - and because all of this is running outside the authorized sandbox, in which /usr/bin/osascript is allowed, the command fails in the background (I got errors with securityd, trustd, and osascript).

So the question is, what are you exactly trying to do with your users? Are you trying to run a policy to ask them a question? Why, in the context of your script, are you trying to run a command as a user, and ask the logged-in user for "the username", and store it in another variable username?

Is your goal to return the value of the name of the current logged in user? Try this:

#!/bin/bash
currentUser="$(ls -l /dev/console | awk '{ print $3 }')"
userRealName="$(dscl . read /Users/${currentUser} RealName | awk '{ print $NF}')"
echo "{userRealName}"