I am trying to push a small script to a test mac.  The script will make the current logged in user an admin of the Mac.  The script works when I pass the username over to the Mac.  But as soon as I try to use a variable to get the logged in current user, the script fails.

 

This works (the current username is user2)

dseditgroup -o edit -a user2 -u localadmin -P mm1007mm -t user admin

 

This fails with the following (using $USER to get the current logged in user)

dseditgroup -o edit -a $USER -u localadmin -P mm1007mm -t user admin

 

2014-04-22 10:03:46: Alert not enabled, moving to next phase....

2014-04-22 10:03:52: Sending script script.sh to client....

2014-04-22 10:03:57: Script sent

2014-04-22 10:03:57: Executing script....

2014-04-22 10:04:01: Error 64 received while executing script

 

Any help would be greatly appreciated.

Answer Summary:
Cancel
1 Comment   [ + ] Show Comment

Comments

  • Thanks for the info. When I run this from xcode, it works great, but when its pushed via Kace, I get the same error.
Please log in to comment

Answers

0

Kace Agent runs under sudo context and unfortunately there is no $USER variable defined in it. Hence the error.

 

I would recommend using something like below command to get the active console user instead 

who | grep console | awk '{print $1}' 

Answered 04/22/2014 by: AbhayR
Fourth Degree Black Belt

  • Thanks for the info. When I run this from xcode, it works great, but when its pushed via Kace, I get the same error. - See more at: http://www.itninja.com/question/issue-pushing-shell-script-to-mac#sthash.fv0FdUPi.dpuf
    • When you run "who | grep console | awk '{print $1}' " you get the same error?
      • yes, it does return the user when I run it locally, but when I push it via Kace, I get the same error. But if I put the username in and remove the automated part, it works.
      • This is what I have

        #!/bin/sh

        #  test.sh
        #  
        #
        #  Created by Brian Elliott on 4/21/14.
        #

        who | grep console | awk '{print $1}' 

        dseditgroup -o edit -a $1 -u localadmin -P mm1007mm -t user admin
      • The first statement doesn't assign the username to $1 but rather it is telling awk to only print the first string passed to it (which happens to be the username). See AbhayR's answer for how to assign to a variable and then use the variable later in the script.
      • I tried that below and it still failed, but I got a different error this time

        Error 200 received while executing script

        Thanks for all the help!
Please log in to comment
0

I don't think you can use $1 like that as it is a awk variable.

Try this 

#!/bin/sh

#  test.sh
#  
#
#  Created by Brian Elliott on 4/21/14.
#

USER=`who | grep console | awk '{print $1}'`

dseditgroup -o edit -a $USER -u localadmin -P mm1007mm -t user admin
Answered 04/22/2014 by: AbhayR
Fourth Degree Black Belt

  • The command I use to add a user to the admin group is a little different:
    dseditgroup -o edit -a $USER -t user admin

    Since I normally run this command as root there's no need to authenticate with the -u and -P flags. That might be causing part of the problem for belliott since KACE will run the script as root also.
Please log in to comment
0

I was able to get this working using the code that AbhayR gave me.  But Chucksteel was also correct. I did not need the -u or -P since it was running as root as well.  So now my code works and it optimized.  Thanks everyone.

Answered 04/23/2014 by: belliott@millennialmedia.com
Senior White Belt

Please log in to comment
Answer this question or Comment on this question for clarity