Skip to main content
Solved

Python script to PUT computer in static group


Forum|alt.badge.img+4

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?

Best answer by seb-marynicz

I solved my issue, thanks for help

View original
Did this topic help you find an answer to your question?

9 replies

Forum|alt.badge.img+4
  • New Contributor
  • 20 replies
  • January 7, 2021

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


Forum|alt.badge.img+9
  • Contributor
  • 137 replies
  • January 7, 2021

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

Forum|alt.badge.img+4
  • Author
  • New Contributor
  • 1 reply
  • Answer
  • January 7, 2021

I solved my issue, thanks for help


sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3532 replies
  • January 8, 2021

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


beeboo
Forum|alt.badge.img+7
  • Contributor
  • 119 replies
  • May 20, 2022
seb-marynicz wrote:

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.


Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • November 23, 2022

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.


beeboo
Forum|alt.badge.img+7
  • Contributor
  • 119 replies
  • November 23, 2022
okschl wrote:

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)


Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • November 23, 2022
beeboo wrote:

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)


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.


beeboo
Forum|alt.badge.img+7
  • Contributor
  • 119 replies
  • November 23, 2022
okschl wrote:

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings