dockutil Extention Attribute Scripting Help

gskibum
Contributor III

I have been learning more about if statements, and came up with the idea of using one in an Extension Attribute along with dockutil to look for Self Service in user Docks, and if not found add it back.

In Terminal the command dockutil --find 'Self Service' will return things like:
"Self Service was found in persistent-apps at slot XX"
or
"Self Service was not found in /Users/username/Library/Preferences/com.apple.dock.plist"

However in an EA script I don't know how to capture that result so it can be acted on.

I'm not asking for anyone to write the entire EA script for me (at least for starters LoL), just with help on finding how to capture the result so I can act on it so I can move to the next part of writing the EA.

Any advice?

Thank you!

2 ACCEPTED SOLUTIONS

mm2270
Legendary Contributor III

You mean like a variable? As in:

SSInDock=$(dockutil --find 'Self Service')

Later in your if/then block, you can recall the "SSInDock" variable (or whatever you name it) and check the output in order to take the appropriate action. You may want to use a =~ string comparison to have it return a match on only part of the string (make sure to use a unique portion of the output). For example

if [[ "$SSInDock" =~ "not found" ]]; then
<do something>
else
<do something else>
fi

View solution in original post

mm2270
Legendary Contributor III

I should have also mentioned that in the case of dockutil and checking for Self Service, you'll need to put a bit more into the EA script to make it reliable. dockutil expects to be checking on the logged in user's Dock plist unless you tell it otherwise with a home directory path. In the case of an EA script, the user it will be looking at is root, not the current logged in user.
So, you'll need to check the owner of console and, if not root, then check that home folder as part of the dockutil command, i.e.

#!/bin/bash

loggedInUser=$(stat -f%Su /dev/console)

if [ "$loggedInUser" != "root" ]; then
     inDock=$(/usr/local/bin/dockutil --find 'Self Service' /Users/$loggedInUser)
     if [[ "$inDock" =~ "not found" ]]; then
         echo "<result>Not In Dock</result>"
     else
         echo "<result>In Dock</result>"
     fi
else
     echo "<result>No user logged in</result>"
fi

If you want to actually have the EA take action (known in JAMFNation circles as "Active EAs") then you'd need to do more than the echo lines in the above script. I personally don't use active EAs myself, though I know some folks do. My concern with them is, unless you also include some logging output its difficult to report on how its working, or if its working, generating errors, etc. EAs by default don't have logs that you can view in the JSS like policies do.

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

You mean like a variable? As in:

SSInDock=$(dockutil --find 'Self Service')

Later in your if/then block, you can recall the "SSInDock" variable (or whatever you name it) and check the output in order to take the appropriate action. You may want to use a =~ string comparison to have it return a match on only part of the string (make sure to use a unique portion of the output). For example

if [[ "$SSInDock" =~ "not found" ]]; then
<do something>
else
<do something else>
fi

mm2270
Legendary Contributor III

I should have also mentioned that in the case of dockutil and checking for Self Service, you'll need to put a bit more into the EA script to make it reliable. dockutil expects to be checking on the logged in user's Dock plist unless you tell it otherwise with a home directory path. In the case of an EA script, the user it will be looking at is root, not the current logged in user.
So, you'll need to check the owner of console and, if not root, then check that home folder as part of the dockutil command, i.e.

#!/bin/bash

loggedInUser=$(stat -f%Su /dev/console)

if [ "$loggedInUser" != "root" ]; then
     inDock=$(/usr/local/bin/dockutil --find 'Self Service' /Users/$loggedInUser)
     if [[ "$inDock" =~ "not found" ]]; then
         echo "<result>Not In Dock</result>"
     else
         echo "<result>In Dock</result>"
     fi
else
     echo "<result>No user logged in</result>"
fi

If you want to actually have the EA take action (known in JAMFNation circles as "Active EAs") then you'd need to do more than the echo lines in the above script. I personally don't use active EAs myself, though I know some folks do. My concern with them is, unless you also include some logging output its difficult to report on how its working, or if its working, generating errors, etc. EAs by default don't have logs that you can view in the JSS like policies do.