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

9 REPLIES 9

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 III

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

hey @seb-marynicz ,

 

Im transitioning my script from sh to py and was curious what you did to achieve the move.

theres a postman collection that has the user to static group, but i cant find the computer version.

if you could share, thatd be awesome.

sdagley
Honored Contributor III

@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.

okschl
New Contributor

I am looking to add computers to a computer static group using "computer name", can someone please help? It would be much appreciated. I Tried MUT but it failed because it is by default using the 'serial number' which appears that you cannot change.

here it is in SH

curl -sk -u "$apiUSER":"$apiPASS" -H "Content-Type: text/xml" -d "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><computer_group><computer_additions><computer><id>$id</id></computer></computer_additions></computer_group>" "$jamfURL/JSSResource/computergroups/id/${staticCOMPUTERgroup}" -X PUT > /dev/null

 

here it is in python

url = f"{jamf_classic_url}/computergroups/id/{jamf_static_computer_group}"
header = {make your header}
payload = f"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<computer_group>\n    <computer_additions>\n        <computer>\n            <id>{each}</id>\n        </computer>\n    </computer_additions>\n</computer_group>"
response = requests.put(url, headers=headers, data=payload)

IIRC, the spaces were a PITA for xml and i wanna say either i couldnt find it or this functionality isnt available on Jamf Pro API (could be wrong though, but i took a quick peak at the postman collection and didnt see anything just now either)

okschl
New Contributor

Thank you very much for the quick response. But you you expound more on this? I do not know where to begin. I have a list of "Computer Name" in a csv file and i want to add the list of computers to a static group using the attribute "computer name". I do not have the serial numbers for the list of computers.

this is based off a script i wrote - im not using mut, so as with most things, you will have to adjust your input accordingly,

 

based on the examples ive seen and what ive used, it seems like computer_id is either the only way or the ideal way.

eg: if your computername was "cömputer" for example, it would be problematic or at least extra steps to check and convert the characters, let alone the issue of jamf machine names NOT required to be unique.

 

so however you do it (do some research here), get the computer_ids.

 

not sure how else to expound of this, but all the stuff in curly braces, {}, are variables due to f-strings.

eg: jamf_static_computer_group is a variable that represents the numerical value (in string) of that group, so mine is "jamf_static_computer_group = "211""

my each variable is just a for loop, where for each in....

each is that computer's computer_id value, eg "972" or something.

 

ultimately, for each element in my target list,

i run those commands to add them to the specific static group