Smart group based on content of folder

mconners
Valued Contributor

Hello Everyone,

Does anyone know of a way to spec out a smart group that looks for the existence of a folder? For instance, if a directory such as "work" exists in /Users/Shared I can find all of the computers where this folder exists in /Users/Shared/work.

I cannot find any criteria that seems to match up to this.

Thoughts?

Thank you as always!!

Mick

2 ACCEPTED SOLUTIONS

apizz
Valued Contributor

@mconners you'd have to create an extension attribute. Something I wrote real quick that would do what you want.

#!/bin/bash

if [ -d /path/to/directory ]; then
echo "<result>Has Work Folder</result>"
else
echo "<result>Does Not Have Work Folder</result>"
fi

exit

View solution in original post

apizz
Valued Contributor

@mconners It's not difficult. Basic steps:

  1. JSS Settings
  2. Go to Computer Management
  3. Click Extension Attributes
  4. Click New
  5. Give the EA a name
  6. Give the EA a description
  7. Leave the Data Type as "String"
  8. Choose where you'd like the EA to be displayed in inventory
  9. Set the Input Type to Script
  10. Add your script to the section that appears.
  11. Save
  12. Update inventory on a laptop and verify the EA populates with the desired text.
  13. Create your smart group based on the EA criteria

View solution in original post

17 REPLIES 17

apizz
Valued Contributor

@mconners you'd have to create an extension attribute. Something I wrote real quick that would do what you want.

#!/bin/bash

if [ -d /path/to/directory ]; then
echo "<result>Has Work Folder</result>"
else
echo "<result>Does Not Have Work Folder</result>"
fi

exit

apizz
Valued Contributor

You could then create a smart group that checks if the EA is not "Has Work Folder"

mconners
Valued Contributor

Thank you @aporlebeke for such a quick response. I have never written or tried to make my own extension attribute. Is this a daunting undertaking? By what you gave me, it looks fairly straight forward.

Mick

apizz
Valued Contributor

@mconners It's not difficult. Basic steps:

  1. JSS Settings
  2. Go to Computer Management
  3. Click Extension Attributes
  4. Click New
  5. Give the EA a name
  6. Give the EA a description
  7. Leave the Data Type as "String"
  8. Choose where you'd like the EA to be displayed in inventory
  9. Set the Input Type to Script
  10. Add your script to the section that appears.
  11. Save
  12. Update inventory on a laptop and verify the EA populates with the desired text.
  13. Create your smart group based on the EA criteria

mconners
Valued Contributor

Thank you again @aporlebeke, this worked just like envisioned. Now to put it all together with the other pieces I am fixing up.

Thanks again!!!

Mick

mconners
Valued Contributor
 

apizz
Valued Contributor

Happy to help :) I'm a bit of a scripting newbie, so when I saw something I knew how to do I jumped on it :P

JayDuff
Contributor II

For posterity, I was looking for a way to detect the presence of an alias on the desktop. I used this to do so:

#!/bin/bash

if [ -e /path/to/file ]; then
echo "<result>Has Alias</result>"
else
echo "<result>Does Not Have Alias</result>"
fi

exit

This will work for regular files too.

The trick is getting the right user directory. We have all devices auto-login to an account, called "default" so it was easy for us. For people who actually log IN to their device, instead of using a default account, you need to add some logic to get the proper directory.

Of course, if your users are smart enough to log in, maybe they're smart enough to not need aliases on the desktop...

c_archibald
Contributor II

Thanks! @aporlebeke I gave you credit in the script. ;)

daniel_ross
Contributor III

@apizz thank you for the solution to this!

Hate to bump this though but what if you wanted to find two directories at the same time in one EA? Good or bad to do?

apizz
Valued Contributor

Not inherently bad. Like anything it depends on your use case. Grouping together can make it easier or harder to use as criteria as part of a smart group. Details around your use case would help.

daniel_ross
Contributor III

@apizz Just shared below on another thread:

#!/bin/bash

if [ -d /Library/Application Support/CrowdStrike/Falcon ] && [ -d /Applications/Falcon.app/Contents/Resources ]; then
    echo "<result>Installed</result>"
else
echo "<result>Not Installed</result>"
fi

But it came back on a known good machine with not installed. So I thought ok break each one down and whats odd is that

/Library/Application Support/CrowdStrike/Falcon

Doesn't work and I'm wondering if thats due to SIP protection? Even on its own it shows no folder is "installed" but I can get to it without issue within finder or see it in terminal. Any thoughts?

apizz
Valued Contributor

@daniel_ross so the reason you're getting a folder does not exist is because you're not including quotes to encapsulate your first folder path. A computer doesn't know that the space in the 'Application Support' folder is part of the path and not a second argument.

I'd definitely encourage you to look up bash scripting best practices, but in general I use quotes and variables wherever possible to make things easier on myself. So I would make the following tweaks to your EA:

#!/bin/bash

PATH1="/Library/Application Support/CrowdStrike/Falcon"
PATH2="/Applications/Falcon.app/Contents/Resources"

if [ -d "$PATH1" ] && [ -d "$PATH2" ]; then
    RESULT="Installed"
else
    RESULT="Not Installed"
fi

/bin/echo "<result>$RESULT</result>"

Of course, you could go further (I would) to add elif statements to isolate specifically which specific folder is missing or that both are missing.

daniel_ross
Contributor III

@apizz thank you so much for the advice and helping out with that! I agree I need to dive more into bash scripting! Do you have any good pointers of where to start? I could really use it and love how this community helps and encourages one another!

skeenan07
New Contributor III

@daniel_ross I use this Bash Scripting Cheatsheet all the time.

daniel_ross
Contributor III

@skeenan07 thank you so much for that cheat sheet and I plan to dive more into all of this.

apizz
Valued Contributor

You may also find this helpful: https://google.github.io/styleguide/shellguide.html