HELP: API Put to location/username

winningham_2
Contributor

I am looking for syntax help on how to PUT the <username> within ../JSSResource/computers/id/$ID/subset/location using curl. Any ideas?

I should also state that I must use BASH to accomplish this task.

2 ACCEPTED SOLUTIONS

freddie_cox
Contributor III

Do you have a list of users that you need to assign to a computer or is this something you will need to do occasionally?

A very basic way:

echo "<computer><location><username>$userID</username></location></computer>" > ~/yourxmlfile.xml
curl -k -v -u CasperAPIUser:CasperAPIPassword $JSSURL -T ~/yourxmlfile.xml -X PUT

Notice the $userID variable and $JSSURL variable. I am assuming you would know the correct URL to be able to successfully post this.

Edit: I haven't tested this specific code as I was condensing an old script that I had, but it should work as you are wanting.

View solution in original post

winningham_2
Contributor

A word of note, I am not a coder AT ALL. But I have to do what I am assigned to do and this worked for me. I sure just in case anyone is just behind me in their development as a DevOp/SysMgr

#!/bin/bash


######################### Run this script against a computer name target ##################

#### NOTE: I assume this is running from a JSS policy

# Remember that $2 is reserved for computer name and we will use that here to allow the
#    script to know the username of the computer it is ran against.

# Remember that $3 is reserved for username and we MAY want that information to populate
#       up too so I bring that to our attention. 


## Set your JAMF JSS API paramenters
   apiUsername=$4
    apiPassword=$5
    jssServer=$6
    extensionAttributeName=$7

NOTE: You should probably hardcode the above variables and be sure to set the Last Login extension attribute template too. 

# Use parameter $2, which is the computerName, and use it to search extension_attributes
#   for the lastUser that logged into said computer.
    lastUser=$(curl -s -k -u "${apiUsername}:${apiPassword}" -H "Content-Type: application/xml" -X 'GET' "${jssServer}/JSSResource/computers/name/$2" | xmllint --xpath "( //computer/extension_attributes/extension_attribute/name )[ contains( ., '${extensionAttributeName}' ) ]/../value/text()" - )

# Now that the last user is known, and we have an ID to identify the computer, PUT that 
#   information up to the JSS within User and Location > Username field.
    curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -k -u "${apiUsername}:${apiPassword}" -d "<computer><location><username>$lastUser</username></location></computer>" "${jssServer}/JSSResource/computers/name/$2"

# TEST TEST and more TEST

View solution in original post

7 REPLIES 7

freddie_cox
Contributor III

Do you have a list of users that you need to assign to a computer or is this something you will need to do occasionally?

A very basic way:

echo "<computer><location><username>$userID</username></location></computer>" > ~/yourxmlfile.xml
curl -k -v -u CasperAPIUser:CasperAPIPassword $JSSURL -T ~/yourxmlfile.xml -X PUT

Notice the $userID variable and $JSSURL variable. I am assuming you would know the correct URL to be able to successfully post this.

Edit: I haven't tested this specific code as I was condensing an old script that I had, but it should work as you are wanting.

winningham_2
Contributor

Here was what I had that errors out with a bad request, so in other words, I am not even close to really pulling this off at the moment, but...

curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -s -k -u "${apiUsername}:${apiPassword}" -d "<username>"${lastUser}"</username>" "${jssServer}/JSSResource/computers/id/$computerID/subset/location

@freddie.cox][/url It appears that I may have my syntax out of order, but I certainly found some examples of placing an entire XML file up there, but for me, I am actually just writing data to a field. If that makes sense.

Error out put is:

<html> <head> <title>Status page</title> </head> <body style="font-family: sans-serif;"> <p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Conflict</p> <p>The request could not be completed due to a conflict with the current state of the resource</p> <p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10">here</a>.<br> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html>

winningham_2
Contributor

Actually, this worked for me eventually so I guess I just needed to take a break and complain and then come back to it...
Thanks for the help.

curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -k -u "${apiUsername}:${apiPassword}" -d "<computer><location><username>${lastUser}</username></location></computer>" ${jssServer}/JSSResource/computers/name/$2

freddie_cox
Contributor III

@winningham.2 Glad I could help and you were able to customize it to your needs!

winningham_2
Contributor

A word of note, I am not a coder AT ALL. But I have to do what I am assigned to do and this worked for me. I sure just in case anyone is just behind me in their development as a DevOp/SysMgr

#!/bin/bash


######################### Run this script against a computer name target ##################

#### NOTE: I assume this is running from a JSS policy

# Remember that $2 is reserved for computer name and we will use that here to allow the
#    script to know the username of the computer it is ran against.

# Remember that $3 is reserved for username and we MAY want that information to populate
#       up too so I bring that to our attention. 


## Set your JAMF JSS API paramenters
   apiUsername=$4
    apiPassword=$5
    jssServer=$6
    extensionAttributeName=$7

NOTE: You should probably hardcode the above variables and be sure to set the Last Login extension attribute template too. 

# Use parameter $2, which is the computerName, and use it to search extension_attributes
#   for the lastUser that logged into said computer.
    lastUser=$(curl -s -k -u "${apiUsername}:${apiPassword}" -H "Content-Type: application/xml" -X 'GET' "${jssServer}/JSSResource/computers/name/$2" | xmllint --xpath "( //computer/extension_attributes/extension_attribute/name )[ contains( ., '${extensionAttributeName}' ) ]/../value/text()" - )

# Now that the last user is known, and we have an ID to identify the computer, PUT that 
#   information up to the JSS within User and Location > Username field.
    curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -k -u "${apiUsername}:${apiPassword}" -d "<computer><location><username>$lastUser</username></location></computer>" "${jssServer}/JSSResource/computers/name/$2"

# TEST TEST and more TEST

freddie_cox
Contributor III

The Extension Attributes are only populated at each inventory update so depending on your settings could cause this data to be old.

If you need/want more up to date information in the JSS, you could configure a login policy to capture the user and then populate the username field using the API. The LDAP lookup would still only occur upon the inventory update if you are using this to populate the additional location fields which is why I am assuming they want this specific field filled in and aren't OK with just referencing the Last User Login Extension Attribute.

winningham_2
Contributor

@freddie.cox
Great point... thank you.