Posted on 04-29-2014 10:49 AM
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
Posted on 08-06-2014 11:07 AM
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
Posted on 11-06-2014 08:05 PM
I tried to use this but got entries found, skipping.
Every single time.
Any ideas why?
@alexjdale @andyinindy
Posted on 07-24-2015 10:09 PM
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.
Posted on 10-09-2015 06:13 AM
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
Posted on 10-09-2015 08:53 AM
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.
Posted on 01-06-2016 03:45 PM
Oy, yea, it be broke. :(
Posted on 01-06-2016 06:06 PM
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.
Posted on 02-23-2016 08:01 PM
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.
Posted on 05-11-2016 09:34 AM
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
Posted on 05-13-2016 05:12 PM
@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>
$
Posted on 05-13-2016 05:17 PM
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.
Posted on 05-13-2016 05:34 PM
@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>
$
Posted on 05-13-2016 05:36 PM
@donmontalvo Sweet! You're welcome!
Posted on 05-13-2016 07:00 PM
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).
Posted on 05-13-2016 07:04 PM
@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.
Posted on 05-13-2016 07:17 PM
took link out of post
Posted on 05-13-2016 09:50 PM
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
Posted on 06-21-2017 07:18 AM
@cshepp11 Will this script work with Ipads?
Posted on 07-28-2017 10:12 AM
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?
Posted on 09-03-2017 11:26 PM
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
Posted on 11-14-2017 09:10 AM
Does anyone know if this script will work with iOS devices? Thanks!
Posted on 02-20-2018 04:34 AM
[edit to add]
Ignore the below. I was putting the group name not the group id in the script.
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?
Posted on 07-16-2018 02:10 PM
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
Posted on 07-16-2018 02:16 PM
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
Posted on 07-16-2018 02:24 PM
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.
Posted on 06-03-2019 10:54 AM
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:
Posted on 10-19-2019 10:45 PM
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.
Posted on 10-21-2019 06:02 AM
@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.
Posted on 10-30-2019 06:11 AM
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
currentuser=$(stat -f%Su /dev/console)
HostName=$(scutil --get ComputerName)
logdir="/Users/${currentuser}/Desktop/AdminExceptions.csv"
if [ ! -f "${logdir}" ]; then
echo "Input file not found, exiting"
exit 1
fi
IMPORTLIST="${logdir}"
JSS="jssportal.com"
JSSGROUPID="53"
JSSGROUPNAME="AdminRightExceptions"
JSSUSER="apiuser"
JSSPASS="apipwd"
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}"
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"
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
Posted on 10-31-2019 01:36 AM
@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?
Posted on 05-03-2021 11:18 AM
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