Hi folks,
I ran across a need to add systems from a list of computer names to a static group in the JSS. Andrew Seago posted a script to add them by JSS ID, but that wasn't quite what I needed.
I also needed to get a list of systems that were not found in our JSS, since I knew this list came from a source besides Casper. I added an export function that will output to a CSV to identify systems it couldn't match. It is not case-sensitive in regards to the input list (the list I received had mixed cases).
I also had to deal with the fact that sometimes OS X will append (2) or something similar to the computer name when it thinks there is another computer with that name on the network. I'm using a wildcard search at the end of the name when I perform a lookup so those are captured. If more than one match is found, the line is skipped for safety reasons.
Anyways, I would love some input on this (it works great but if anyone else thinks it is useful and has feature requests, let me know). I know it's not super efficient because I am looking up each computer name as a separate API call, but I was more concerned about having something working in a couple hours and may perform rewrites later.
Just enter the JSS, JSSGROUPID, JSSUSER, and JSSPASS variables and you are ready to go. Please read the script notes as well.
#!/bin/bash
# Copyright 2014 by Alex Dale
# Add computers to a static group in a JSS from a plaintext list of computer names
#
# Make sure your target group is a static group and is empty, or it will be overwritten
# Input file must have one name per line.
# Usage: scriptname.sh /path/to/input/file
# Add "export" as an optional argument after input file to export a csv with failed system lookup info to /tmp/JSSFailures.csv
# Searches are performed with a wildcard at the end to catch names with (2), etc appended to the name by the OS
# Multiple matches (due to wildcard or dupe records) will be skipped, for safety
#
# Test this first on your dev JSS! I cannot account for all scenarios.
#Verify input file exists
if [ ! -f "$1" ]; then
echo "Input file not found, exiting"
exit 1
fi
IMPORTLIST=$1
EXPORTFLAG=$2
exportCSV="/tmp/JSSFailures.csv"
# Hostname of JSS
JSS=""
# Group ID for target static group
JSSGROUPID=""
# API service account credentials
JSSUSER=""
JSSPASS=""
# Start building XML for computer group, which will be uploaded at the end
GROUPXML="<computer_group><computers>"
if [ ! "$JSS" ] || [ ! "$JSSGROUPID" ] || [ ! "$JSSUSER" ] || [ ! "$JSSPASS" ]; then
echo "Required variables have not all been entered. Please validate and retry."
exit 1
fi
echo "Input file: $1"
# Read list into an array
inputArraycounter=0
while read line || [[ -n "$line" ]]; do
inputArray[$inputArraycounter]="$line"
inputArraycounter=$[inputArraycounter+1]
done < <(cat $IMPORTLIST)
echo "${#inputArray[@]} lines found"
foundCounter=0
for ((i = 0; i < ${#inputArray[@]}; i++)); do
echo "Processing ${inputArray[$i]}"
nameLookup=`curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computers/match/${inputArray[$i]}*`
sizeLookup=`echo $nameLookup | xpath //size 2>/dev/null | tr -cd [:digit:]`
if [ $sizeLookup = 1 ]; then
idLookup=`echo $nameLookup | xpath //id 2>/dev/null`
if [ "$idLookup" ]; then
GROUPXML="$GROUPXML<computer>$idLookup</computer>"
foundCounter=`expr $foundCounter + 1`
fi
echo "Match found, adding to group"
else
echo "$sizeLookup entries found, skipping."
if [ "$EXPORTFLAG" = "export" ]; then
echo "${inputArray[$i]},$sizeLookup computers matched">>$exportCSV
fi
fi
done
GROUPXML="$GROUPXML</computers></computer_group>"
echo "$foundCounter computers matched"
echo "Attempting to upload computers to group $JSSGROUPID"
curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computergroups/id/$JSSGROUPID -X PUT -HContent-type:application/xml --data $GROUPXML