Script to add a device to a computer group?

benducklow
Contributor III

Hey JN'rs! I am looking to see if there is a script out there that I can setup a policy in Self Service (scoped to our 1st level support staff) that would do this:

  1. Pop-up a dialog box for the tech to enter/add a computer name or serial #
  2. It would then get added to a pre-defined computer group (static or smart)
  3. That's it!

The idea is that the tech can essentially add a device to the scope of a policy so it runs when the devices checks-in next time. (NOTE: This support staff do not have access to Jamf Remote, or the ability to add/edit policies, computer groups, etc. Without granular permissions to allow specific LDAP groups or users to only edit/change identified policies/smart groups, Granularize Casper Permissions this would be a very useful ability/function to have.

Thanks for any thoughts or leads you would have on something like this!

16 REPLIES 16

sdagley
Esteemed Contributor II

@benducklow Search for computer_additions and you'll find some examples of adding computers to a Static Group via a script. You should find something you can build on in the results.

Hugonaut
Valued Contributor II

@benducklow - Here you go! Tested & Working - Added Script to Jamf Library, Created a Policy, Added Script to Policy & Ran via Self Service. Prompted me for Serial input, entered serial & added my computer to the group specified in the script.
Just gotta modify with your credentials

# API Credentials
#########################################################################################
apiuser="USERNAME HERE"
apipass="PASSWORD HERE"
jssURL="URL HERE"

&

Modify GROUP_ID w/ the Group Number (easy way to find this is to click on your static group in jamf dashboard & then look at the url bar, you will see something like "JAMFURL:8443/staticComputerGroups.html?id=999&o=r" - The 999 is what you want to enter into the Group_ID field)

& Group Name as you named it Jamf Side.

# API Group ID, NAME & URL Identifiers
#########################################################################################
#I Tag The Name of Group I am adding to Here, helps if multiple in script.
GROUP_ID="###"
GROUP_NAME="NAME OF GROUP"

& Boom goes the dynamite!

Do beware though, if the computer has the same name as another computer in your JAMF Database, either one of those computers could be placed, ie a common one might be "Macbook Pro" - since it is placing the computer into the group by Computer Name Record.

#!/bin/bash

# API Credentials
#########################################################################################
apiuser="USERNAME HERE"
apipass="PASSWORD HERE"
jssURL="URL HERE"

# Prompt Tech for Serial
#########################################################################################
SERIAL="$(osascript -e 'Tell application "System Events" to display dialog "Enter Serial Number:" default answer ""' -e 'text returned of result' 2>/dev/null)"

# Pull Computer Device Name from JAMF
#########################################################################################
COMPUTERNAME=$(curl -H "Accept: text/xml" -sfku "${apiuser}:${apipass}" "${jssURL}/JSSResource/computers/serialnumber/${SERIAL}/subset/general" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<name>/{print $3; exit}')


# API Group ID, NAME & URL Identifiers
#########################################################################################
#I Tag The Name of Group I am adding to Here, helps if multiple in script.
GROUP_ID="###"
GROUP_NAME="NAME OF GROUP"
GROUP_URL="JSSResource/computergroups/id/${GROUP_ID}"


# XML header information, used to PUT & PULL
#########################################################################################
xmlHeader="<?xml version="1.0" encoding="UTF-8"?>"


# Add Computer to Group
#########################################################################################
GROUP_Data="<computer_group><id>${GROUP_ID}</id><name>${GROUP_NAME}</name><computer_additions><computer><name>${COMPUTERNAME}</name></computer></computer_additions></computer_group>"


curl -sSkiu ${apiuser}:${apipass} "${jssURL}/${GROUP_URL}" 
    -H "Content-Type: text/xml" 
    -d "${xmlHeader}${GROUP_Data}" 
    -X PUT  > /dev/null

# Update Jamf
#########################################################################################
sudo jamf recon
________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

sdagley
Esteemed Contributor II

@Hugonaut You should remove the standalone="no" from the xmlHeader line in the script so that it reads xmlHeader="<?xml version="1.0" encoding="UTF-8"?>" as it is not necessary, and can cause problems.

Hugonaut
Valued Contributor II

Thanks @sdagley got me learnt today after reading http://www.xmlplease.com/xml/standalone/

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

benducklow
Contributor III

@Hugonaut Wow, without actually testing yet, the code looks and makes sense. Thanks much!

I assume the computer group be a static or smart one?

Hugonaut
Valued Contributor II

You're welcome! @benducklow - this only works for Static Groups

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

sdagley
Esteemed Contributor II

@benducklow You can't manually add a computer to a Smart Group. Well technically you could by adding a "Computer Name" criteria entry for each machine you're trying to scope and ORing them all together, but that'd be a baaaaad use of Smart Groups.

benducklow
Contributor III

Good point @sdagley! Defeats the use case for a 'smart' group ;)

sdunbar
Contributor

@Hugonaut Just trying your script, would it work for a cloud based instance?
I have tried, but it times out with an error "failed to contact to XXXX.jamfcloud.com"
Thanks

Hugonaut
Valued Contributor II

@sdunbar I have no idea. I have no experience with cloud based jss & i haven't seen a difference in how the cloud would be used. I have used developer.jamf.com for all my research - nothing here seems to explicitly state hosted only but it could be inferred by the way they display the url in examples. hopefully someone else with more info can jump in

Jamf Cloud api path might be able to help? your.jamfcloud.com/uapi (versus just /api)

https://developer.jamf.com/sample-code

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

sdunbar
Contributor

@Hugonaut WIll take a look, many thanks

cubandave
Contributor

I have a set of scripts here that do this but with the uuid.

It also check if the computer is a member. There’s a second script to remove a computer too.

https://github.com/cubandave/Jamf-Interaction-Toolkit/blob/master/Scripts/00-API-Add-Current-Computer-to-Static-Group.sh

Hugonaut
Valued Contributor II

@cubandave thanks for sharing, I like your use of UUID MUCH Better than my Computer Name method, 0 room for getting the wrong machine. I will be changing my api calls

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

tsylwest
Contributor

Hi @cubandave any chance there's an updated location for your script? the link here takes me to a 404 :-(

tsylwest
Contributor

Never mind, found the script elsewhere :-)

dlondon
Valued Contributor

@cubandave - Your scripts look great. I'm getting an odd error though and don't know if it's my newness to the API or a problem elsewhere. When I run the script in a Self Service policy as a test I get this failure in the log:

Details
[STEP 1 of 4]
Executing Policy Add Computer to NVivo12 Full Disk Access Group
[STEP 2 of 4]
Running script API-Add-Current-Computer-to-Static-Group...
Script exit code: 1
Script result: -:10: parser error : Opening and ending tag mismatch: br line 8 and p
</p> ^
-:11: parser error : Opening and ending tag mismatch: p line 8 and body
</body> ^
-:12: parser error : Opening and ending tag mismatch: body line 5 and html
</html> ^
-:13: parser error : Premature end of data in tag html line 1

^
groupNameIDLookup came back blank the group NVivo12-FullDiskAccess may not exist
Error running script: return code was 1.
[STEP 3 of 4]
[STEP 4 of 4]

Have you ever seen this before?

Regards,

David