Change the Site with an easy script

SvenB
New Contributor II

This is the script I use to change site on the computer its run on.

#!/bin/bash
# Provided as it is, no guaranties, don't blame me if something breaks!

# ——————————— Changes Site ———————————
apiURL="https://domain.jamfcloud.com"
apiUser="user"
apiPass="password"
newSite="Sitename"
idSite="2"

# ——————————— Getting the serial ———————————
computerserial="$(system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}')"


#Change the Site
echo "<computer><general><site><id>$idSite</id><name>$newSite</name></site></general></computer>" | curl -X PUT -fku $apiUser:$apiPass -d @- $apiURL/JSSResource/computers/serialnumber/$computerserial/subset/general -H "Content-Type: application/xml"
3 REPLIES 3

beatlemike
Release Candidate Programs Tester

I wonder if there is a way to do that more interactively, so like one script can be used to choose from all sites, sort of like the menu during user-initiated enrollment.

Sometimes I'd like site admins to be able to pass machines off to other site admins and not rely on them to do the work in Jamf itself (They be lazy).

mm2270
Legendary Contributor III

@beatlemike You can try this script. I adapted it from the one above by @SvenB It should prompt for a Site to assign from a list of all your Sites, and then update the computer record with that selection. Although I have some Sites set up on our server, I don't use them, so I can't effectively test this out.

#!/bin/bash

## API information
apiURL="https://domain.jamfcloud.com"
apiUser="user"
apiPass="password"

## Get a list of all sites and their IDs
allSiteData=$(curl -H "Accept: text/xml" -sfku "${apiUser}:${apiPass}" $apiURL/JSSResource/sites | xmllint --format - | awk -F'>|<' '/<name>|<id>/{print $3}')

## Split out Site Names and Site IDs into arrays
allSiteNames=("$(echo "$allSiteData" | awk 'NR % 2 == 0')")
allSiteIDs=($(echo "$allSiteData" | awk 'NR % 2 == 1'))

## Prompt for a Site selection
chosenSite=$(/usr/bin/osascript << EOF
tell application "System Events"
activate
set siteNames to do shell script "printf '%s\n' "${allSiteNames[@]}""
set namesForDisplay to paragraphs of siteNames
set chosenSite to choose from list namesForDisplay with prompt "Choose a Site"
end tell
EOF)

## If a Site was chosen, determine the Site ID
if [ "$chosenSite" != "false" ]; then
    x=0
    while read SITE; do
        if [ "$SITE" == "$chosenSite" ]; then
            chosenSiteID=${allSiteIDs[$x]}
        fi
        let x=$((x+1))
    done < <(printf '%s
' "${allSiteNames[@]}")
else
    exit 0
fi

## Get the computer serial number
computerSerial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')

## Update/change the Site for the computer
echo "<computer><general><site><id>$chosenSiteID</id><name>$chosenSite</name></site></general></computer>" | curl -X PUT -fku $apiUser:$apiPass -d @- "$apiURL/JSSResource/computers/serialnumber/$computerSerial/subset/general" -H "Content-Type: application/xml"

Since this uses an API username/password that would have rights to update computer records, you'd need to find an effective way to protect the credentials from being discovered.

beatlemike
Release Candidate Programs Tester

@mm2270 That worked perfectly! Thanks! I had tried something similar, but it didn't work. You're awesome!