Script to Determine If a Device is a Laptop or Workstation

JDaher
Contributor

Hello,

I'm pretty sure one of you will know this. I stumbled upon a script left by the previous administrator in our Jamf instance. It's a script to name machines. The naming convention uses Building + Laptop or Workstation + Last 7 digits of serial. For example, a laptop in the San Francisco building should be named like this: SFOMLP-xxxxxxx. A workstation would be named SFOMWS-xxxxxxx. Everything works well except the part where it determines whether it's a laptop or a workstation. That part of the script looks like this:

# Determine if portable or desktop
comptype=$(/usr/sbin/system_profiler SPBluetoothDataType | awk '/Complete/ { print $NF }')
if [[ $comptype == 'Portable' ]];then 
base="MLP"
else
base="MWS"

The problem is that it names everything with the base MWS, which suggests to me that the comptype variable needs to be edited because it's not grabbing the correct info from the system. Perhaps that line to grab info from System Profiler is no longer correct (?). 
 
Any insights are appreciated. 
Thank you 
 
2 ACCEPTED SOLUTIONS

gabe2385
Contributor

Hey @JDaher looks like they where looking for Bluetooth but it may be better to use SPHardwareDataType instead check out this post and see if this works for you by @rtroutonhttps://community.jamf.com/t5/jamf-pro/determine-if-mac-is-portable-or-desktop-via-script/m-p/50608 

View solution in original post

sydowl
New Contributor II

Hi JDaher - try using wildcards

comptype=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book")
if [[ $comptype == *'Book'* ]];then 
base="MLP"
else
base="MWS"
fi

View solution in original post

18 REPLIES 18

gabe2385
Contributor

Hey @JDaher looks like they where looking for Bluetooth but it may be better to use SPHardwareDataType instead check out this post and see if this works for you by @rtroutonhttps://community.jamf.com/t5/jamf-pro/determine-if-mac-is-portable-or-desktop-via-script/m-p/50608 

Hi Gabe,

Thank you for the suggestion. I'm a little bit further but it's still failing. 

I ran the command in terminal, just to test: 
/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book"
This is the result: Model Identifier: MacBookPro18,3

So then I edited the script to: 

# Determine if portable or desktop
comptype=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book")
if [[ $comptype == 'Book' ]];then 
base="MLP"
else
base="MWS"

It's still not working. I even tried editing the if part of the statement from Book to MacBookPro, to *.Book, and even to the exact result: MacBookPro18,3

 

sdagley
Esteemed Contributor II

@JDaher Try this:

model=$(ioreg -l | grep "product-name" | cut -d "=" -f 2 | sed -e s/[^[:alnum:]]//g | sed s/[0-9]//g)

# Default to a Desktop
base="MWS"
isMacbook=$(echo "$model" | grep "Book")
if [ -n $isMacbook ]; then
base="MLP"
fi
 
That could be simplified but the script I pulled it from also does things like check to see if the Mac is a VM. Apologies for the formatting but the BLEEPING forum software appears to have have removed the ability to define a code block in a post.

cbrewer
Valued Contributor II

Another option would be to check the power management for whether or not an internal battery is present.

if pmset -g batt | grep -i internalbattery > /dev/null; then
  base="MLP"
else
  base="MWS"
fi

 

I like this approach. Simplified our rename script. 

JDaher
Contributor

Thank you all for your suggestions. It seems like there's a few ways to determine whether laptop or workstation, and the script can be edited accordingly. I think the problem I'm having is on this part of the statement: 

if [[ $comptype == 'Book' ]];then 

base="MLP"

My scripting skills are very basic, but it looks to me as if the line [[ $comptype == 'Book' ]] is not interpreting the result of the $comptype query, so the rest of the script defaults to else, in this case to the base, MWS. 

MacJunior
Contributor III

@JDaher may I ask how can you get the building assigned to a Mac from Jamf Pro server? is it using API?

The building is assigned manually in Jamf Pro, after a device has been enrolled in Jamf. 

yeah but I meant how do you get the building of a Mac enrolled in Jamf via a script?

@MacJunior Here's the full script. I took out my server's address (see bold text in script).  


#!/bin/bash
 
# Grab full serial for API call
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
 
 
# Grab the last 7 characters of a device serial number
shortsn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | rev | cut -c -7 | rev)
echo $shortsn
 
# Determine if portable or desktop
comptype=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book")
if [[ $comptype == *'Book'* ]]
then 
base="LP"
else
base="WS"
fi
echo $base
 
 
# Get the building code
response=$(curl -k -H "accept: application/xml" -u apiuser:Summersfo2018! <Jamf Server Address>:443/JSSResource/computers/serialnumber/${serial}/subset/location)
building=$(echo $response | /usr/bin/awk -F'<building>|</building>' '{print $2}');
echo $building
 
 
if [[ $building == "New York City" ]];then
shortBuilding="NYC"
elif [[ $building == "San Francisco" ]]; then
shortBuilding="SFO"
elif [[ $building == "Tokyo" ]]; then
shortBuilding="TOK"
    elif [[ $building == "Los Angeles" ]]; then
shortBuilding="LAX"
    elif [[ $building == "Remote" ]];then
shortBuilding="REM"
    
fi
 
echo $shortBuilding
 
 
computerName=$shortBuilding'M'$base'-'$shortsn
echo $computerName
 
 
# Setting computername
echo "Setting computerName..."
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
 
exit 0

Worked like a charm ... many thanks

sydowl
New Contributor II

Hi JDaher - try using wildcards

comptype=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book")
if [[ $comptype == *'Book'* ]];then 
base="MLP"
else
base="MWS"
fi

Bingo! I had tried that earlier but I goofed on the syntax and wrote *.Book instead of *Book*. Seems so obvious now...

Thank you and everyone else who pitched in, all your suggestions helped me sort things out and I learned a few things along the way. I really appreciate you all.  

sydowl
New Contributor II

Ah, you were thinking of regular expression notation which is even better and would like this:

if [[ $comptype =~ "Book" ]];then

(You won't want to use .* because the non-alpha characters in the result throw it off).

Does this work for the following models?
Mac14,9

Mac14,10

Thank you!

Well, it turned out that it is not working for the latest MacBooks ...

JDaher
Contributor

I don't know about those models. The last models I used the script on were 2021 machines. 

JevermannNG
Contributor II

Their "Model Name" does no longer contain "Book". Jamf knows them under e.g. model "Mac14,9" ...

Is there any other way to determine if it is a laptop or desktop device, ethernet port?