Posted on 06-19-2019 09:16 AM
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:
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!
Posted on 06-19-2019 10:07 AM
@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.
Posted on 06-19-2019 10:36 AM
@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
Posted on 06-19-2019 12:12 PM
@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.
Posted on 06-19-2019 12:23 PM
Thanks @sdagley got me learnt today after reading http://www.xmlplease.com/xml/standalone/
Posted on 06-20-2019 06:26 AM
@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?
Posted on 06-20-2019 06:40 AM
You're welcome! @benducklow - this only works for Static Groups
Posted on 06-20-2019 06:46 AM
@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.
Posted on 06-20-2019 07:04 AM
Good point @sdagley! Defeats the use case for a 'smart' group ;)
Posted on 06-20-2019 07:47 AM
@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
Posted on 06-20-2019 07:58 AM
@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
Posted on 06-20-2019 08:00 AM
@Hugonaut WIll take a look, many thanks
Posted on 06-23-2019 09:52 PM
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
Posted on 06-24-2019 05:57 AM
@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
Posted on 10-20-2020 06:45 AM
Hi @cubandave any chance there's an updated location for your script? the link here takes me to a 404 :-(
Posted on 10-21-2020 03:45 AM
Never mind, found the script elsewhere :-)
Posted on 10-26-2020 12:50 AM
@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