Help with a naming script-

rbingham917
New Contributor III

Hey everyone! I am working on a workflow for my company's zero-touch setup. I have 4 scripts that I am testing out and it is failing on the last one. Script 1- Create a folder on root of drive called "NAME", chmod 777 to that folder, then- have an input box for the client to put in the asset tag information to upload it to JSS using recon-assettag - Verified working.

Script 2- Query the JSS for the Asset Tag information, and echo it into a text file called /name/asset.txt
Verified working

Script 3- run a sysctl command that echoes out the hardware model, simple if statement that says if the word "MacBook" is present, then echo the name "LM" into a text file called /name/model.txt, if not, then echo "DM" into same file (Company naming convention)

Here's where I am running into problems, maybe I'm looking at this the entirely wrong way. Script 4, I want to create a variable of the contents of both of those text files and use jamf setcomputername -name $V1$V2. Ex: LM1234

Here is my script, to the best of my knowledge it should be working, but should and are, are never the same. I have a feeling it will be something banally simple, but I'm still very wet behind the ears with scripting.

!/bin/bash

This script will pull the contents of 2 files model.txt and asset.txt and combine them into the machine name

Pull in Model information from Script 1

model= $(cat ‘/name/model.txt' )

Pull in asset tag information from Script 2

asset= $(cat ‘/name/assettag.txt' )

Just to prove that the logic works - will remove

echo $model$asset > /name/machinename.txt

Set computer name to modelAsset Normally errors out saying that I need the "-name" flag, which is present

jamf setcomputername -name $model$asset

Thanks for your input fellow Nationers.

6 REPLIES 6

mm2270
Legendary Contributor III

Just curious, but why did you decide to beak this up into 3 separate scripts? If the intent of your workflow is to generate a name for the computer using various commands to pull data from the Mac, and then name it, that can all be done within a single script. I'm not sure I see the reason for echoing out strings into files in each script and then pulling them back in to generate a name for the computer.

In any event, the likely issue with your script is that there seems to be a space between the = sign and the $ for the beginning of each command. By doing so, I don't think you're generating any variables. In bash, there needs to be no space between those to create a variable. You should probably also change the singlw quotes around path strings to double quotes.
For example, change:

model= $(cat ‘/name/model.txt' )

to

model=$(cat "/name/model.txt" )

Notice no space after the = sign.

rbingham917
New Contributor III

Thanks mm2270.

I broke the scripts up more for learning purposes on my end mostly. Once I get more comfortable with my scripting abilities then I'll combine them. I knew it would be something simple. Thank you for your assistance.

brock_walters
Contributor
#!/bin/bash

# collect asset tag from user input

while true
do
assetTag=$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Enter the asset tag number located on the bottom of your Mac or select Cancel." default answer ""' -e 'text returned of result' 2>/dev/null)
    if [ $? -ne 0 ]     
    then # user cancel
        exit
    elif [ -z "$assetTag" ]
    then # loop until input or cancel
        /usr/bin/osascript -e 'Tell application "System Events" to display alert "Please enter the asset tag number or select Cancel... Thanks!" as warning'
    else [ -n "$assetTag" ] # user input
        break
    fi
done

# submit asset tag to JSS

/usr/local/bin/jamf recon -assetTag "$assetTag"

# determine computer type

if system_profiler SPHardwareDataType | grep MacBook
then
    computerType=LM
else
    computerType=DM
fi

# combine computer type & asset tag to match naming convention & submit name to JSS

/usr/local/bin/jamf setComputerName -name "$computerType$assetTag"

Since you used the term "zero-touch" & I believe you've created some kind of input method for the end user to enter the asset tag I've used those as requirements for this workflow. I know you're experimenting but all of the operations can be performed in 1 script.

1st, it's probably more efficient if you avoid writing out files. That said, if you are going to be generating a lot of data & returning it to the JSS as an extension attribute (or doing something like creating a report for populating a bunch of different extension attributes) writing out to a file & reading from it rather then lengthening the time it takes for the update inventory event at the JSS is a good strategy!

2nd, If the desired result is to both populate the asset tag in the JSS & create a device name in the JSS using the asset tag string you can do this without sending data to the JSS & then getting that data back from the JSS.

Last, I may be missing something (if I am please help us all to learn!) but I am not sure what sysctl command you are using. The sysctl mib needed to get model ID is actually hidden:

Gathering system information in Swift with sysctl

In my example I am using the system_profiler binary. Hope this helps!

rbingham917
New Contributor III

Hi there Brock- Thanks for the input. I will definitely give that a go.

The sysctl command that I use (and I bet this is totally not "the correct" way of doing it) is running a "sysctl hw.model", output that to a variable, then I run a grep for the word "MacBook", which the result of the command gets placed into another variable and I just issue a simple boolean if statement. If the result is 1, then it's a macbook, and I will apply the name "LW", otherwise it's not a laptop, and it'll put the name DW.

The light is starting to flicker on in my "programming brain" and I think I might be starting to get the hang of this.

brock_walters
Contributor

No I actually like that way of getting the model ID. That is probably what is populating system_profiler anyway. There isn't a way (at least on the sysctl man page) to generate the hidden mib so I feel like system_profiler for most scripters would be the easier solution but sometimes Google-fu is the way to go. :) Good luck!

1 last point - if you run this as the payload of a JSS policy you would probably want to add the Maintenance payload set to "Update Inventory". If so, watch out for setting Scope to "All Computers" & Execution Frequency to "Ongoing"...

mm2270
Legendary Contributor III

I actually thought, despite it not showing up on the manpage for sysctl, that the hw.model mib was fairly common knowledge floating around. Perhaps or perhaps not.

FWIW, there's also ioreg for getting the model identifier.

ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/model/{print $4}'