Model Extension Attribute

cjatsbm
New Contributor II

I found a nice extension attribute to pull the Model Identifier

sysModel=sysctl -n hw.model

FullModelName=/usr/libexec/PlistBuddy -c "Print :${sysModel}:LOCALIZABLE:marketingModel" "$plistFile" 2> /dev/null
if [ -z "$FullModelName" ]
then echo "<result>UNKNOWN</result>"
else echo "<result>$FullModelName</result>"
fi

exit 0

How can I extract the resulting Year from full model name? Basically i want to extract the info between the opening and closing ( ) in the result.

5 REPLIES 5

easyedc
Valued Contributor II

So I'm curious why you're after this as an EA? Your JSS will report that natively for you and you can create searches/smart groups/policies based off that already. What's the benefit of the second query? Also your code didn't paste properly, it's missing part of the line to try and assist.d970a1aa91a1497eb057456cb12de96c

cjatsbm
New Contributor II

@easyedc basically I'm trying to extract only the year from the reported Model for each mac..

defiler
New Contributor III

@cjatsbm

FullModelName=`/usr/libexec/PlistBuddy -c "Print ${sysModel}:_LOCALIZABLE_:marketingModel" "$plistFile" | cut -d"(" -f2 | tr -d ")"`

mm2270
Legendary Contributor III

The script you posted isn't complete, because neither the ${sysModel} or "$plistFile" variables are defined in it. For the sake of completeness, the ${sysModel} would be the model identifier, which you can get with something like sysctl hw.model | awk '{print $NF}'
The $plistFile being referenced is

/System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist

which is, as you can see, pretty buried in the OS file system.

Here would be a full Extension Attribute to just get the year from the string that gets returned. Just be aware that this might be fragile. In looking through that plist, I see some older Macs that have no year mentioned in their "marketing model" strings, so there's no telling for sure if it will work with any Mac.

#!/bin/bash

plistFile="/System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist"

sysModel=$(sysctl hw.model | awk '{print $NF}')

modelYear=$(/usr/libexec/PlistBuddy -c "Print ${sysModel}:_LOCALIZABLE_:marketingModel" "$plistFile"  | tr ' ' '
' | grep -o "20[0-9][0-9]")

if [ ! -z "$modelYear" ]; then
     echo "<result>$modelYear</result>"
else
     echo "<result>N/A</result>"
fi

Steven5342
New Contributor III

This is what I used as the file path folders have different names for 10.14. and 10.15.

#!/bin/sh os_ver=$(sw_vers -productVersion | awk -F. '{print $2}') if [[ ${os_ver} -le 14 ]]; then plistFile="/System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist" sysModel=$(sysctl hw.model | awk '{print $NF}') modelYear=$(/usr/libexec/PlistBuddy -c "Print ${sysModel}:LOCALIZABLE:marketingModel" "$plistFile" | tr ' ' ' ' | grep -o "20[0-9][0-9]") if [ ! -z "$modelYear" ]; then echo "<result>$modelYear</result>" else echo "<result>N/A</result>" fi elif [[ ${os_ver} -ge 15 ]]; then plistFile="/System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/en.lproj/SIMachineAttributes.plist" sysModel=$(sysctl hw.model | awk '{print $NF}') modelYear=$(/usr/libexec/PlistBuddy -c "Print ${sysModel}:LOCALIZABLE:marketingModel" "$plistFile" | tr ' ' ' ' | grep -o "20[0-9][0-9]") if [ ! -z "$modelYear" ]; then echo "<result>$modelYear</result>" else echo "<result>N/A</result>" fi fi exit 0