Skip to main content
Solved

Script to update user info via API


Forum|alt.badge.img+7

I have an an issue where I need to update some user fields in the API but I don't really know where to start.

In summary, I need to loop through all users and for every user whose 'managed_apple_id' field is blank/null I need to copy the data from the 'roster_managed_apple_id' field to the 'managed_apple_id' field.

Does that make sense? Is anyone able to help?

Best answer by chris_reynolds1

@awginger

Something like this may help you?

#!/bin/bash


jssURL="https://xxxxx.jamfcloud.com"

## DO NOT EDIT BELOW THIS LINE ##

echo "Please enter your JSS username:"
read jssUser

echo "Please enter your JSS password:"
read -s jssPass

usersToGetAtOneTime="10000"

# Make sure xmlstarlet is installed
if ! command -v xmlstarlet > /dev/null ; then
    echo "You must install xmlstarlet before using this script."
    echo "Try "brew install xmlstarlet""
    exit 1
fi

# Get list of users
echo "Downloading list of user IDs..."
while read -r line; do
    ids+=("$line")
done <<< "$(curl -X GET -k -s -u "$jssUser:$jssPass" "$jssURL/JSSResource/users" | xmlstarlet sel -t -v '/users/user/id')"

# Find users with roster_managed_apple_id in
echo "Gathering User Information"
count="$usersToGetAtOneTime"
for id in "${ids[@]}"; do
    managedAppleID=$(curl -X GET -k -s -u "$jssUser:$jssPass" "$jssURL/JSSResource/users/id/$id" | xmlstarlet sel -t -v '/user/roster_managed_apple_id')

# Update Managed Apple ID if Roster Managed Apple ID Exists   
if [ -z "$managedAppleID" ]
then
    echo "No Managed Apple ID, moving on"
else
    echo "$managedAppleID"
    curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -k -u "$jssUser:$jssPass" -d "<user><managed_apple_id>$managedAppleID</managed_apple_id></user>" "$jssURL/JSSResource/users/id/$id"
    ((count--))
fi
done

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

3 replies

Forum|alt.badge.img+3

@awginger

Something like this may help you?

#!/bin/bash


jssURL="https://xxxxx.jamfcloud.com"

## DO NOT EDIT BELOW THIS LINE ##

echo "Please enter your JSS username:"
read jssUser

echo "Please enter your JSS password:"
read -s jssPass

usersToGetAtOneTime="10000"

# Make sure xmlstarlet is installed
if ! command -v xmlstarlet > /dev/null ; then
    echo "You must install xmlstarlet before using this script."
    echo "Try "brew install xmlstarlet""
    exit 1
fi

# Get list of users
echo "Downloading list of user IDs..."
while read -r line; do
    ids+=("$line")
done <<< "$(curl -X GET -k -s -u "$jssUser:$jssPass" "$jssURL/JSSResource/users" | xmlstarlet sel -t -v '/users/user/id')"

# Find users with roster_managed_apple_id in
echo "Gathering User Information"
count="$usersToGetAtOneTime"
for id in "${ids[@]}"; do
    managedAppleID=$(curl -X GET -k -s -u "$jssUser:$jssPass" "$jssURL/JSSResource/users/id/$id" | xmlstarlet sel -t -v '/user/roster_managed_apple_id')

# Update Managed Apple ID if Roster Managed Apple ID Exists   
if [ -z "$managedAppleID" ]
then
    echo "No Managed Apple ID, moving on"
else
    echo "$managedAppleID"
    curl -X PUT -H "Accept: application/xml" -H "Content-type: application/xml" -k -u "$jssUser:$jssPass" -d "<user><managed_apple_id>$managedAppleID</managed_apple_id></user>" "$jssURL/JSSResource/users/id/$id"
    ((count--))
fi
done

exit 0

Forum|alt.badge.img+9
  • Contributor
  • 154 replies
  • April 14, 2020

If you are move comfortable manipulating CSVs than scripting, you can check out The MUT. There is a good How To Video on their page


Forum|alt.badge.img+7
  • Author
  • Contributor
  • 44 replies
  • April 18, 2020

Thanks @chris.reynolds that worked a treat! Also thanks @shaquir, I will check that out!


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