Script to add Computers to Static Group by Computer Name

alexjdale
Valued Contributor III

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
31 REPLIES 31

andyinindy
Contributor II

@alexjdale

I was able to get this to work with a few modifications. First, I had to change this:

done < <(cat $IMPORTLIST)

...to this:

done < "$IMPORTLIST"

I also had to change this:

nameLookup=`curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computers/match/${inputArray[$i]}*`

..to this:

nameLookup=`curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computers/name/${inputArray[$i]}`

And finally, I changed this:

if [ $sizeLookup = 1 ]; then

...to this:

if [ "$sizeLookup" != "" ]; then

Otherwise, it worked great! Thanks for sharing this with the JAMF community!

--Andy

RaulSantos
Contributor

I tried to use this but got entries found, skipping.
Every single time. Any ideas why?
@alexjdale @andyinindy

waqas
New Contributor III

I initially made the mistake of using the JSS URL (https://somehting.something.darkside:8443) for the variable JSS and after getting the errors that RaulSantos mentioned (entries found, skipping), realized that alex had the prefix (https) and postfix (8443) baked in the lines where the variable was being called. Doh!!!

Anyways, works like a charm. This saves a ton of time.

Thank you, Alex.

karthikeyan_mac
Valued Contributor

Hi,

I am trying to add the computers to the static group. I changed the script as @andyinindy mentioned but still I am getting "entries found, skipping". Do we need to change anything in the script to work with JSS 9.73?

I have created the plain text file with list of computer names as input.

We are looking for creating a static or smart group with 500 Username. Atleast If we have option to create static group using computer name will be helpful.

Thanks & Regards,
Karthikeyan

alexjdale
Valued Contributor III

Wow, this was resurrected! Yeah, I've come across the same issue but haven't gotten around to finding a fix. After the JNUC I'll see if I get this working properly again.

donmontalvo
Esteemed Contributor III

Oy, yea, it be broke. :(

--
https://donmontalvo.com

mm2270
Legendary Contributor III

Huh, I completely forgot about this thread, and @alexjdale's script, when I was trying to answer someone else's question on creating a Static Group from a list of user's. I could have pointed them here I guess, but since I didn't recall it, I wrote up and posted a separate script that creates a Static Group which can be found here in case anyone's interested. Its a little different in approach, but generally the same idea. The main differences are, the one I posted is for creating a new computer group, not adding to an existing one, it does not do the export to csv for systems not found for example, and was also designed around trying to look up usernames. However, I also have another version not posted yet that will work on computer names specifically. The posted one should work on any searchable string, like usernames and other items.

I've run it in testing a few times on our JSS 9.73 server and its worked every time. I can't attest to whether something would be different with 9.81/2 though.

johncasper
New Contributor

I was using this script without problem but lately it seems to have failed with the below error when running the line of ... echo $nameLookup | xpath //size 2>/dev/null | tr -cd [:digit:]

It throw out error as below.
at /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/XML/Parser.pm line 187.

Any ideas would be greatly appreciated.

cshepp11
New Contributor III

This script worked great for me with implementing @andyinindy's changes. Also, for JSS Hostname, I only input jssname.domain.com. I am currently running jss v9.82. Here is the script with changes implemented.

#!/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="jss.domain.com"
# Group ID for target static group
JSSGROUPID="inputyourgroupidhere"
# API service account credentials
JSSUSER="username"
JSSPASS="password"
# 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 < "$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/name/${inputArray[$i]}`
    sizeLookup=`echo $nameLookup | xpath //size 2>/dev/null | tr -cd [:digit:]`
    if [ "$sizeLookup" != "" ]; 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

donmontalvo
Esteemed Contributor III

@cshepp11 tried your script and got this...and I did verify the account name/password is correct...

$ ./importScript.sh importList.csv export
Input file: importList.csv
10 lines found
Processing MAC-001
 entries found, skipping.
Processing MAC-002
 entries found, skipping.
Processing MAC-003
 entries found, skipping.
Processing MAC-004
 entries found, skipping.
Processing MAC-005
 entries found, skipping.
Processing MAC-006
 entries found, skipping.
Processing MAC-007
 entries found, skipping.
Processing MAC-008
 entries found, skipping.
Processing MAC-009
 entries found, skipping.
Processing MAC-011
 entries found, skipping.
0 computers matched
Attempting to upload computers to group 1820
<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Unauthorized</p>
<p>The request requires user authentication</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>
$
--
https://donmontalvo.com

cshepp11
New Contributor III

Hello @donmontalvo

The way I got around that was to copy a list of machines out of excel (that is where I was getting my info) and copy them into a new textedit file, then select all and format as plain text, save to desktop then try again with that file.

It always failed if i tried to re-use a .txt file or edit a .txt file, i always had to create a new one.

Give that a try and let me know what you get.

donmontalvo
Esteemed Contributor III

@cshepp11 yep, that did it, thanks!!!

$ ./importScript.sh importList.csv export
Input file: importList.csv
10 lines found
Processing MAC-001
Match found, adding to group
Processing MAC-002
Match found, adding to group
Processing MAC-003
Match found, adding to group
Processing MAC-004
Match found, adding to group
Processing MAC-005
Match found, adding to group
Processing MAC-006
Match found, adding to group
Processing MAC-007
Match found, adding to group
Processing MAC-008
Match found, adding to group
Processing MAC-009
Match found, adding to group
Processing MAC-010
Match found, adding to group
10 computers matched
Attempting to upload computers to group 1820
<?xml version="1.0" encoding="UTF-8"?><computer_group><id>1820</id></computer_group>
$
--
https://donmontalvo.com

cshepp11
New Contributor III

@donmontalvo Sweet! You're welcome!

donmontalvo
Esteemed Contributor III

Spoke too soon...SCG shows 0 computers, even though 68 of 72 show as imported in output of script (log shows no matches for the remaining 4).

--
https://donmontalvo.com

cshepp11
New Contributor III

@donmontalvo check SCG id is correct? did you create a new static group first and then make sure it was empty? Even if you used a SCG that was previously used, it will just overwrite it.

cshepp11
New Contributor III

took link out of post

donmontalvo
Esteemed Contributor III

Got it sorted out, turned out to be the line feeds. Set to UNIX/UTF and it worked like a charm.

I agree with the folks who've called for a CSV import function in JSS. :)

Thanks!
Don

--
https://donmontalvo.com

lincolnking3
New Contributor

@cshepp11 Will this script work with Ipads?

christina_luis
Contributor

We use iPads and I am new to Jamf is there a way to use this for iPads? If so how do you use them?

BrentSlater
Release Candidate Programs Tester

Will this work for existing static computer groups? I am looking at adding a new machine to the 180 static groups that we have for one of our gfx departments.

Any ideas, none of the groups are currently empty

cguerra
New Contributor

Does anyone know if this script will work with iOS devices? Thanks!

Tangentism
Contributor III

[edit to add]

Ignore the below. I was putting the group name not the group id in the script.

Oh you didnt?

Sorry to wake this thread up but I'm trying to use this script but constantly get this message after it finds all the machines in the list (so its reading ok from the JSS):

The server has not found anything matching the request URI You can get technical details here: "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5"

The auth credentials are correct and the static group exists. I have even changed the name so that there are no special characters or spaces in it but no cigar.

Any ideas?

MacMaul
New Contributor II

Updated the syntax (the script stopped working after a macOS update, mainly changed the sections with ``s) and changed the script to match on serial number instead of object name.

#!/bin/bash

# Copyright 2014 by Alex Dale
# Updated 2018 by Phil Maul
#
# Add computers to a static group in a JSS from a plaintext list of computer serial numbers
#
# 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="yourjamfserver.com"
# Group ID for target static group
JSSGROUPID="0000"
# API service account credentials
JSSUSER="XXXXXXXXXXX"
JSSPASS="XXXXXXXXXXX"
# 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 < "$IMPORTLIST"
echo "${#inputArray[@]} lines found"

foundCounter=0
for ((i = 0; i < ${#inputArray[@]}; i++)); do
    echo "Processing ${inputArray[$i]}"
    serialLookup=$(curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computers/serialnumber/${inputArray[$i]})
    # echo "serialLookup variable is $serialLookup"
    sizeLookup=$(echo $serialLookup | xpath //size 2>/dev/null | tr -cd '[:digit:]')
    # echo "sizeLookup variable is $sizeLookup"
    if [ "$sizeLookup" != "" ]; then
        idLookup=$(echo $serialLookup | xpath //id 2>/dev/null)
        if [ "$idLookup" ]; then
            GROUPXML="$GROUPXML<computer>$idLookup</computer>"
            foundCounter=$((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

MacMaul
New Contributor II

Here's the mobile device version since folks were asking for it and I needed it so I made it:

#!/bin/bash

# Copyright 2014 by Alex Dale
# Updated 2018 by Phil Maul
#
# Add mobile devices to a static group in a JSS from a plaintext list of mobile device serial numbers
#
# 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="yourjamfserver.com"
# Group ID for target static group
JSSGROUPID="0000"
# API service account credentials
JSSUSER="XXXXXXXXXXX"
JSSPASS="XXXXXXXXXXX"
# Start building XML for mobile device group, which will be uploaded at the end
GROUPXML="<mobile_device_group><mobile_devices>"

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 < "$IMPORTLIST"
echo "${#inputArray[@]} lines found"

foundCounter=0
for ((i = 0; i < ${#inputArray[@]}; i++)); do
    echo "Processing ${inputArray[$i]}"
    serialLookup=$(curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/mobiledevices/serialnumber/${inputArray[$i]})
    # echo "serialLookup variable is $serialLookup"
    sizeLookup=$(echo $serialLookup | xpath //size 2>/dev/null | tr -cd '[:digit:]')
    # echo "sizeLookup variable is $sizeLookup"
    if [ "$sizeLookup" != "" ]; then
        idLookup=$(echo $serialLookup | xpath //id 2>/dev/null)
        if [ "$idLookup" ]; then
            GROUPXML="$GROUPXML<mobile_device>$idLookup</mobile_device>"
            foundCounter=$((foundCounter+1))
        fi
        echo "Match found, adding to group"
    else
        echo "$sizeLookup entries found, skipping."
        if [ "$EXPORTFLAG" = "export" ]; then
            echo "${inputArray[$i]},$sizeLookup devices matched">>$exportCSV
        fi
    fi
done

GROUPXML="$GROUPXML</mobile_devices></mobile_device_group>"

echo "$foundCounter devices matched"

echo "Attempting to upload devices to group $JSSGROUPID"
curl -s -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/mobiledevicegroups/id/$JSSGROUPID -X PUT -HContent-type:application/xml --data $GROUPXML

MacMaul
New Contributor II

I changed it to match on serial numbers, as we were mainly using this script to delete migrated objects from an old JSS instance after they migrated to a new JSS instance. The time between a successful object migration and deletion from the old JSS would vary (since I was exporting the list from the new instance and running the script to fill the static group on the old server manually when i got around to it) and consultants would end up repurposing objects so their names would change. Matching by serial number became the only way to guarantee finding the correct object to fill the target static group.

jpaseka
New Contributor

If you receive "0 computers matched" "Attempting to upload computers to group x" -- With the result that no computers get added to the static group:

  • A possible solution is to ensure your input file does not contain any extensions (.txt) in the filename!

Naren
New Contributor III

Thank you @alexjdale and @cshepp11 , for sharing this script. I was able to make half way.
Do you also have script to remove computers from static group? Please share with me if you got one ready.
I was trying to modify the given script to remove computers in it but it was deleting the static group itself.

Thank you.

sdagley
Esteemed Contributor II

@Naren Take a look at the thread Script to add a device to a computer group? for an example of how to add a computer to an existing Static Group rather than replacing the contents of the group. Following the example in that thread you can use computer_deletions instead of computer_additions to remove computers from a group instead of adding them.

Naren
New Contributor III

Thank you @sdagley for sharing the link. I was trying with two different scripts one to add multiple computers to static group other to remove from static group.
But when i was trying to execute the script it is adding or removing only the last entry from excel.
For example my .csv file has four computers and script is actually only reacting to the last one in it. @cshepp11

!/bin/bash

currentuser=$(stat -f%Su /dev/console)
HostName=$(scutil --get ComputerName)
logdir="/Users/${currentuser}/Desktop/AdminExceptions.csv"

Verify input file exists

if [ ! -f "${logdir}" ]; then echo "Input file not found, exiting" exit 1
fi
IMPORTLIST="${logdir}"

EXPORTFLAG=€1.80

exportCSV="/tmp/JSSFailures.csv"

Hostname of JSS

JSS="jssportal.com"

Group ID for target static group

JSSGROUPID="53"
JSSGROUPNAME="AdminRightExceptions"

API service account credentials

JSSUSER="apiuser"
JSSPASS="apipwd"

Start building XML for computer group, which will be uploaded at the end

GROUPXML="<computer_group><computer_deletions>"

if [ ! "$JSS" ] || [ ! "$JSSGROUPID" ] || [ ! "$JSSUSER" ] || [ ! "$JSSPASS" ]; then echo "Required variables have not all been entered. Please validate and retry." exit 1
fi

echo "Input file: ${logdir}"

Read list into an array

inputArraycounter=0
while read line || [[ -n "$line" ]]; do inputArray[$inputArraycounter]="$line" inputArraycounter=$[inputArraycounter+1]
done < "$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/name/${inputArray[$i]} sizeLookup=echo $nameLookup | xpath //size 2>/dev/null | tr -cd [:digit:] if [ "$sizeLookup" != "" ]; then idLookup=echo $nameLookup | xpath //id 2>/dev/null if [ "$idLookup" ]; then GROUPXML="$GROUPXML<computer>$idLookup</computer>" foundCounter=$((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</computer_deletions></computer_group>"
echo "$foundCounter computers matched"

deleting computer from casper with UUID

echo "Attempting to Delete computers from group $JSSGROUPID"
curl -s -v -k -u $JSSUSER:$JSSPASS https://$JSS:8443/JSSResource/computergroups/id/$JSSGROUPID -X PUT -HContent-type:application/xml --data $GROUPXML

nadim_hussein
New Contributor

@sdagley any idea why its picking up only last line in the lookup, for example if I have 3 values, a, b, c in the list it will only process value c and leave other 2 items?

gald
New Contributor

Hey, in order to run script in Big Sur, use xpath5.18

#!/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="jss.domain.com"
# Group ID for target static group
JSSGROUPID="inputyourgroupidhere"
# API service account credentials
JSSUSER="username"
JSSPASS="password"
# 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 < "$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/name/${inputArray[$i]}`
    sizeLookup=`echo $nameLookup | xpath5.18 //size 2>/dev/null | tr -cd [:digit:]`
    if [ "$sizeLookup" != "" ]; then
        idLookup=`echo $nameLookup | xpath5.18 //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