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.
@easyedc basically I'm trying to extract only the year from the reported Model for each mac..
@cjatsbm
FullModelName=`/usr/libexec/PlistBuddy -c "Print ${sysModel}:_LOCALIZABLE_:marketingModel" "$plistFile" | cut -d"(" -f2 | tr -d ")"`
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
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