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 @rich.trouton: https://community.jamf.com/t5/jamf-pro/determine-if-mac-is-portable-or-desktop-via-script/m-p/50608
@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.
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 @rich.trouton: https://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
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
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.
@JDaher may I ask how can you get the building assigned to a Mac from Jamf Pro server? is it using API?
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
@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.
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.
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?
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
@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
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.
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).
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
Does this work for the following models?
Mac14,9
Mac14,10
Thank you!
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 ...
I don't know about those models. The last models I used the script on were 2021 machines.
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?
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.