Extract Data from plist

jec1
New Contributor II

I have the following data inside a plist

<dict> <key>AdminUnit</key> <dict> <key>Date</key> <date>2013-10-12T06:07:27Z</date> <key>Value</key> <string>GLX_DUD</string> </dict>

I'm interested in extracting the <string>GLX_DUD</string> to build a smart group from that string to create an extension attribute.

Here's what I've tried which is not working. Any help would be appreciated.

Thank you,

#!/bin/sh

BigFixUnit=$( defaults read /Library/Preferences/com.bigfix.BESAgent.plist AdminUnit )

echo "<result>$Department</result>"

2 ACCEPTED SOLUTIONS

wyip
Contributor

Hey another BigFix/Casper user!

You want to use PlistBuddy for this. Something like this:

#!/bin/sh
BigFixUnit=`/usr/libexec/PlistBuddy -c "Print :AdminUnit:Value" /Library/Preferences/com.bigfix.BESAgent.plist`
echo "<result> $BigFixUnit</result>"

You should check out the man page for PlistBuddy for more usage info: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.ht...

View solution in original post

wyip
Contributor

Knowing how the BESAgent plist is laid out, it sounds like you're missing something before the first colon in ":AdminUnit:Value". I would open up your BESAgent plist in xcode to take a look. Find the value you want to print out and note the names in the "path" down to the value you want.

E.g let's say I want to pull out whatever is stored in "Value" under "BootTime" from my com.bigfix.BESAgent.plist file. Here's a screencap of what it looks like in xcode:

external image link

You need to specify the exact location of this value in the PlistBuddy command or else it'll just give you the same error you saw. In my example, the PlistBuddy command would look like this:

/usr/libexec/PlistBuddy -c "Print :Options:GlobalOptions:BootTime:Value" /Library/Preferences/com.bigfix.BESAgent.plist

So you need to change that part of the command to match the "path" of the value inside the plist that you're looking for.

Hope this helps.

View solution in original post

6 REPLIES 6

wyip
Contributor

Hey another BigFix/Casper user!

You want to use PlistBuddy for this. Something like this:

#!/bin/sh
BigFixUnit=`/usr/libexec/PlistBuddy -c "Print :AdminUnit:Value" /Library/Preferences/com.bigfix.BESAgent.plist`
echo "<result> $BigFixUnit</result>"

You should check out the man page for PlistBuddy for more usage info: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.ht...

jec1
New Contributor II

Hey wyip, I tried your example and got the error message below:

Print: Entry, ":AdminUnit:Value", Does Not Exist
<result></result>

I'm not knowledgeable enough to solve it. Do I need to run the script from a certain directory? I'm running from my user/downloads folder.

gregneagle
Valued Contributor
#!/bin/sh

BigFixUnit=$( defaults read /Library/Preferences/com.bigfix.BESAgent.plist AdminUnit )

echo "<result>$Department</result>"

$Department is never defined! You probably want:

#!/bin/sh

BigFixUnit=$( defaults read /Library/Preferences/com.bigfix.BESAgent.plist AdminUnit )

echo "<result>$BigFixUnit</result>"

wyip
Contributor

Knowing how the BESAgent plist is laid out, it sounds like you're missing something before the first colon in ":AdminUnit:Value". I would open up your BESAgent plist in xcode to take a look. Find the value you want to print out and note the names in the "path" down to the value you want.

E.g let's say I want to pull out whatever is stored in "Value" under "BootTime" from my com.bigfix.BESAgent.plist file. Here's a screencap of what it looks like in xcode:

external image link

You need to specify the exact location of this value in the PlistBuddy command or else it'll just give you the same error you saw. In my example, the PlistBuddy command would look like this:

/usr/libexec/PlistBuddy -c "Print :Options:GlobalOptions:BootTime:Value" /Library/Preferences/com.bigfix.BESAgent.plist

So you need to change that part of the command to match the "path" of the value inside the plist that you're looking for.

Hope this helps.

GaToRAiD
Contributor II

@jec1 you need to use xpath it is designed to extract data from xml files such as plists.

What you want to do is something like this

#!/bin/sh
BigFixUnit=$( defaults read /Library/Preferences/com.bigfix.BESAgent.plist | /usr/bin/xpath "/dict/dict/string[preceding-sibling::key='AdminUnit4'][1]/text()" 2> /dev/null )

echo "<result>$BigFixUnit</result>"

Hope this helps. BTW, the actual path i used might work, of you might have to play around with it.

jec1
New Contributor II

Thank you to everyone who contributed on this request. I truly appreciate it. You guys are too smart. With the help of wyip stating that the full path needed to be added to the script, I was able to get the script working. Before the :AdminUnit line, the script needed :Settings:Client:AdminUnit - that did it.

Again, thank you so much.