Python script to PUT computer in static group

seb-marynicz
New Contributor

Hi there,

So Im trying to create some python script to put computer into a static group through API, but somehow i cant get this working . I tried to use requests lib, with no luck. Does anyone one got some ideas how to do it?

1 ACCEPTED SOLUTION

seb-marynicz
New Contributor

I solved my issue, thanks for help

View solution in original post

4 REPLIES 4

chase_g
New Contributor III

Have you checked out GitHub? Jamf has some API scripts, maybe slightly outdated but I know there are others who have some api scripts out there as well. https://github.com/jamf/API_Scripts

nelsoni
Contributor II

This is the API script I use to add computers to static groups. Works pretty well

#!/bin/sh

#API login info
apiuser="Username"
apipass="Password"
jamfProURL="https://org.jamfcloud.com"

#Grab serial number and OS Version of computer
SerialNumber=$(system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}')
macOSVersion=$(sw_vers -productVersion)

#update group with Group ID you want
GroupID="Group ID"
apiURL="JSSResource/computergroups/id/${GroupID}"

CheckIt=$(echo $macOSVersion | cut -d . -f 1)

#Add Computer to group based on macOS Version
if [[ "$CheckIt" == "11" ]]
    then

        GroupName=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/${apiURL}" | xpath -e '/computer_group/name/text()' 2>/dev/null)

    else

        GroupName=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/${apiURL}" | xpath '/computer_group/name/text()' 2>/dev/null)

fi

#XML header stuff
xmlHeader="<?xml version="1.0" encoding="UTF-8" standalone="no"?>"

#API command to add the serial number to the Static Group
apiData="<computer_group><id>${GroupID}</id><name>${GroupName}</name><computer_additions><computer><serial_number>$SerialNumber</serial_number></computer></computer_additions></computer_group>"

#Mix it all together
curl -sSkiu ${apiuser}:${apipass} "${jamfProURL}/${apiURL}" 
    -H "Content-Type: text/xml" 
    -d "${xmlHeader}${apiData}" 
    -X PUT  > /dev/null

#Run an Inventory Update
sudo /usr/local/jamf/bin/jamf recon

sleep 5

exit 0

seb-marynicz
New Contributor

I solved my issue, thanks for help

View solution in original post

sdagley
Honored Contributor II

@nelsoni and @Sebastian_Marynicz The xmlHeader line should be:

xmlHeader="<?xml version="1.0" encoding="UTF-8"?>"

Including the standalone attribute isn't necessary, and can cause problems when making multiple API calls.