Posted on 08-22-2024 03:27 AM
Hey Guys,
no real Question but i saw some similar posts and questions or request for something like "$sbuject_title" ..
I want to share my solution for this scenario and perhaps some of you has an idea to optimize it:
User got new mac and wants to transfer all Static Groups from the old to the new mac.
I provide this script as policy in Self Service:
#!/bin/zsh
#region - Prerequisites
################################################################################################################################################################################################
## Create tmp location
# Define the path for the temporary folder
TMP_DIR="/Users/Shared/tmp"
# Check if the folder already exists
if [ -d "$TMP_DIR" ]; then
echo "The folder $TMP_DIR already exists."
else
# Create the folder
mkdir -p "$TMP_DIR"
# Check if the creation was successful
if [ $? -eq 0 ]; then
echo "The temporary folder has been successfully created at $TMP_DIR."
# Set permissions (optional)
chmod 755 "$TMP_DIR"
echo "Permissions have been set to 755."
else
echo "Error: The folder could not be created."
exit 1
fi
fi
# Variable for tmp files
txt_tmp=/Users/Shared/tmp/inventory_details.txt
txt_tmp2=/Users/Shared/tmp/static_groups.txt
GROUP_ID_FILE=/Users/Shared/tmp/static_groups_ID.txt
# API login
bearerToken=""
url=<X>
client_id="X"
client_secret="X"
# Create Token
token=$(curl --location --request POST "$url/api/oauth/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=$client_id" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_name=Test" \
--data-urlencode "client_secret=$client_secret")
# Catch Token
bearerToken=$(echo "$token" | plutil -extract access_token raw -)
#endregion################################################################################################################################################################################################
#region - Getting Static Groups from old MacBook
################################################################################################################################################################################################
# Get the serial number using Dialog
read -r -d '' applescriptCode <<'EOF'
set dialogText to text returned of (display dialog "Bitte trage die alte MacBook-Seriennummer ein." default answer "no input")
return dialogText
EOF
serial_number=$(osascript -e "$applescriptCode");
# Prepend "MC-" to the serial number
computerName="MC-$serial_number"
# Get Jamf Computer ID from Serial Number
deviceID=$(curl -s -H "Accept: text/xml" -H "Authorization: Bearer ${bearerToken}" ${url}/JSSResource/computers/name/"$computerName" | xmllint --xpath '/computer/general/id/text()' -)
# Inventory Collection from Computer
inventory_details=$(curl -s -H "Accept: application/json" -H "Authorization: Bearer ${bearerToken}" "${url}/api/v1/computers-inventory-detail/$deviceID" -X GET)
echo "$inventory_details" >> $txt_tmp
# Use grep to delete everything except 2 Lines with: "smartGroup" : false
grep -B 2 '"smartGroup" : false' $txt_tmp > $txt_tmp2
# cutting only Group IDs and writing into txt file
awk -F'"' '/"groupId" :/ {print $4}' $txt_tmp2 > $GROUP_ID_FILE
#endregion################################################################################################################################################################################################
#region - Put new MacBook into Static Groups
################################################################################################################################################################################################
## Function to add computer to a static group
add_to_group() {
xml_data="<computer_group>
<id>$group_id</id>
<computer_additions>
<computer>
<id>$computer_id</id>
</computer>
</computer_additions>
</computer_group>"
response=$(curl -s -w "%{http_code}" -o /dev/null \
-X PUT \
-H "Authorization: Bearer $bearerToken" \
-H "Content-Type: application/xml" \
-d "$xml_data" \
"$url/JSSResource/computergroups/id/$group_id")
if [ "$response" = "201" ]; then
echo "Successfully added computer $computer_id to group $group_id"
else
echo "Failed to add computer $computer_id to group $group_id. HTTP response: $response"
fi
}
## Details new MacBook
# Get new MacBook-Name
read -r -d '' applescriptCode <<'EOF'
set dialogText to text returned of (display dialog "Bitte trage die neue MacBook-Seriennummer ein." default answer "no input")
return dialogText
EOF
serialNumber_new=$(osascript -e "$applescriptCode");
computerName_new="MC-$serialNumber_new"
# Get Jamf Computer ID from Serial Number
computer_id=$(curl -s -H "Accept: text/xml" -H "Authorization: Bearer ${bearerToken}" ${url}/JSSResource/computers/name/"$computerName_new" | xmllint --xpath '/computer/general/id/text()' -)
# Check if the group ID file exists
if [ ! -f "$GROUP_ID_FILE" ]; then
echo "Error: Group ID file not found at $GROUP_ID_FILE"
exit 1
fi
# Read group IDs from the file and add computer to each group
while IFS= read -r group_id || [[ -n "$group_id" ]]; do
# Trim whitespace
group_id=$(echo "$group_id" | tr -d '[:space:]')
if [[ -n "$group_id" ]]; then
echo "Adding computer $computer_id to group $group_id"
add_to_group "$computer_id" "$group_id"
fi
done < "$GROUP_ID_FILE"
echo "Software Gruppen wurden auf neuem MacBook $computerName_new übertragen"
#endregion################################################################################################################################################################################################
#region - cleanup tmp files and folder
################################################################################################################################################################################################
# rm $txt_tmp
# rm $txt_tmp2
# rm /Users/Shared/tmp
#endregion################################################################################################################################################################################################
Next Step is build a lovely Swift Dialog UI to glow it up!
If you have some Input feel free to share it!