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.
1#!/bin/bash23# Copyright 2014 by Alex Dale4# Add computers to a static group in a JSS from a plaintext list of computer names5#6# Make sure your target group is a static group and is empty, or it will be overwritten7# Input file must have one name per line.8# Usage: scriptname.sh /path/to/input/file9# Add "export" as an optional argument after input file to export a csv with failed system lookup info to /tmp/JSSFailures.csv10# Searches are performed with a wildcard at the end to catch names with (2), etc appended to the name by the OS11# Multiple matches (due to wildcard or dupe records) will be skipped, for safety12# 13# Test this first on your dev JSS! I cannot account for all scenarios.1415#Verify input file exists16if [ ! -f "$1" ]; then17 echo "Input file not found, exiting"18 exit 119fi20IMPORTLIST=$121EXPORTFLAG=$222exportCSV="/tmp/JSSFailures.csv"23# Hostname of JSS24JSS=""25# Group ID for target static group26JSSGROUPID=""27# API service account credentials28JSSUSER=""29JSSPASS=""30# Start building XML for computer group, which will be uploaded at the end31GROUPXML="<computer_group><computers>"3233if [ ! "$JSS" ] || [ ! "$JSSGROUPID" ] || [ ! "$JSSUSER" ] || [ ! "$JSSPASS" ]; then34 echo "Required variables have not all been entered. Please validate and retry."35 exit 136fi3738echo "Input file: $1"3940# Read list into an array41inputArraycounter=042while read line || [[ -n "$line" ]]; do43 inputArray[$inputArraycounter]="$line"44 inputArraycounter=$[inputArraycounter+1]45done < <(cat $IMPORTLIST)46echo "${#inputArray[@]} lines found"4748foundCounter=049for ((i = 0; i < ${#inputArray[@]}; i++)); do50 echo "Processing ${inputArray[$i]}"51 nameLookup=`curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computers/match/${inputArray[$i]}*`52 sizeLookup=`echo $nameLookup | xpath //size 2>/dev/null | tr -cd [:digit:]`53 if [ $sizeLookup = 1 ]; then54 idLookup=`echo $nameLookup | xpath //id 2>/dev/null`55 if [ "$idLookup" ]; then56 GROUPXML="$GROUPXML<computer>$idLookup</computer>"57 foundCounter=`expr $foundCounter + 1`58 fi59 echo "Match found, adding to group"60 else61 echo "$sizeLookup entries found, skipping."62 if [ "$EXPORTFLAG" = "export" ]; then63 echo "${inputArray[$i]},$sizeLookup computers matched">>$exportCSV64 fi65 fi66done67GROUPXML="$GROUPXML</computers></computer_group>"6869echo "$foundCounter computers matched"7071echo "Attempting to upload computers to group $JSSGROUPID"72curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computergroups/id/$JSSGROUPID -X PUT -HContent-type:application/xml --data $GROUPXML