How to get extension attribute in script?

mhasman
Valued Contributor

Probably that is super-simple question, but please... How do I get the extension attribute value in the script?

There is EA with name, LDAP value $EXTENSIONATTRIBUTE_28, and it provides the correct info.

I need to use it in script. Should I send it to script as parameter? Should I call it in script anyhow?

Please help. Thanks!

19 REPLIES 19

mm2270
Legendary Contributor III

Can you explain further what you're looking to do here? Are you saying you want to use the same information that an EA script grabs in another script? Or did you mean something else?

EA scripts only get run at inventory collection, and the values they gather only get collected in order to send back to the JSS to update that computer's inventory record.
There are ways to send that gathered data from the EA into a local file each time its collected, or to take some action on the result, but I'm not sure if that's exactly what you're looking for here. If you can explain the situation a little more, I'm sure there is some solution that can be discussed.

mhasman
Valued Contributor

Yes, EA collects some information which I would like to use in script, and script is part of JSS policy.

Simple example - EA gets some user's info from LDAP, and I want to run simple notification policy shows me the pop-up window with EA value - or use that value for something in the script.

Thanks!

mm2270
Legendary Contributor III

I think what you're after is creating a variable, which is a stored piece of information that you can define in one part of a script and can be called up later in the script.
Without seeing the EA script, I assume you should be able to rip the line from the EA script that captures the information you're looking for, and drop it into a new script to use for the purpose you described. I don't see why that wouldn't be possible, but again, you haven't posted the EA script, so I'm only taking an educated guess.

The basic formats for capturing a variable is to either define it outright with syntax like

var="some string"

or capture the result of a command, more likely what you're talking about here, with something like:

var=$(commands that get the value go here)

Does that help?

mhasman
Valued Contributor

Yes. I am looking for how to make variable var getting the value of EA.

603ecec15c254e219080fa9dd9db2192

homeShare is the name of EA which gets info from LDAP. It works, shows in inventory screen correct value.

mhasman
Valued Contributor

I would like to use that value is policy, send it somehow to the script. Or just get value in the script - like we get $1, $2, $3, ect.

363007167b9d450d8c0eab0173fc6315

iJake
Valued Contributor

If you want to pull down the value of an already stored extension attribute in a script running on a client you'll need to do an API call to get that value. It's certainly doable but I'm not sure why you wouldn't just copy the code that gets that value and grab it again.

mhasman
Valued Contributor

@iJake I can not just copy the code because EA value is collected from LDAP, not via script

mm2270
Legendary Contributor III

Then use the second syntax format I mentioned above. For example, say the line in the script that gets the string you want does something like this:

dscl "/Active Directory/DOMAIN/All Domains" read /Users/$loggedInUser SMBHome | awk '{print $NF}'

Then copy/paste that line (the relevant line from your EA script) into a new script, and turn it into a variable like:

var=$(dscl "/Active Directory/DOMAIN/All Domains" read /Users/$loggedInUser SMBHome | awk '{print $NF}')

Now you can use that variable by calling it later in the script. Simple example:

echo "$var"

Note that a variable is called in a script by placing a $ in front of the name of the variable you assigned.
I hope this is making sense. If you post the actual EA script, sanitized if necessary, and also the other script you want to use it in, I can help give you something more specific if needed. But the above should get you in the right direction.

mhasman
Valued Contributor

@mm2270 Thank you! Yes, I thought about getting Directory Attribute in script itself, just wondered if it is possible to send inventory info from JSS to the script which runs under JSS policy.

Why to collect info again if we already have it in JSS, right? :)

iJake
Valued Contributor

@mm2270 's answer is definitely the way to go if your machines are bound but if they weren't and you needed to get this attribute it is definitely possible via the API. You'd want to have the credentials as script parameters in the policy for security assuming your JSS is locked down enough that they can exist there in clear text.

mm2270
Legendary Contributor III

Yes, as @iJake mentions, you could certainly pull the stored value from the Mac's JSS record using the API. It wasn't clear to me that's kind of what you may have been after. If so, then there are definitely threads that show how to grab an EA value using the API in a script.

One caveat to grabbing stored values with the API is that its only going to be as accurate as the Mac's last inventory collection. So that could mean its anywhere from minutes old to hours or even days out of date. That may not matter in this instance, but I thought I should mention that. Grabbing the value on the fly in a script (if applicable) will give you a more up to date result.

mhasman
Valued Contributor

Actually, that particular one is the bad example of that I am asking about.

Lets talk about some extra field in the JSS inventory, like "Internal Info". Might be just manual input or EA, it does not matter. Is there any way to send the value to the script which runs as part of JSS policy?

iJake
Valued Contributor

If its manual entry then you'll need to use the API but if its an EA that is filled by a script then definitely in that case use that same code over again and generate that value in a script.

mhasman
Valued Contributor

Do I understand correct that there is no easy way just "send" EA value to script anyhow?

chriscollins
Valued Contributor

@mhasman That is correct. If you want to use an EA value in the script, you have to pull it from the API.

mhasman
Valued Contributor

Thank you all for helping! You are the best!

thoule
Valued Contributor II

@mhasman I would modify my EA to save the data locally as wall as the JSS so I wouldn't have to use the API.

#!/bin/sh
#do magic EA stuff like LDAP lookups
echo "<result>$myEA</result>"
defaults write /Library/Preferences/com.bcbs.mhasman.plist EAResult -string $myEA.

then in the script you want that EA

#!/bin/sh
eaVal=$(defaults read  /Library/Preferences/com.bcbs.mhasman.plist EAResult)

mhasman
Valued Contributor

@thoule Thank you sir!

mhasman
Valued Contributor

...