Static group from search or a CSV

RaulSantos
Contributor

Thanks to @brysontyrrell for all his hard work. Extending the JSS via the API. https://github.com/brysontyrrell/StaticGroupFromSearch

13 REPLIES 13

stoneacheck
New Contributor III

This is awesome. This is so close to what I need...

Working in a school I often get requests from staff to add certain apps to a set of kids through VPP, for instance - Plague Inc. for Health class. There are 3 rotations of kids in health a year, so group A gets the app for a few months, group B for a few months, group C for a few months. Conveniently, a staff member can export out a list of the student emails from powerschool by class which are the same as the emails in the JSS thanks to AD, or truncated without the domain it's their full name.

What I really need is this functionality for the user search, not computers or mobile devices, since users is where we assign VPP apps.

I've tried replacing all instances of the script where it would have referenced computers and computer groups to match with users and user groups, but no luck, my result always returns no match on the search. Example error below:

Gaius:~ michael$ python /Users/michael/Downloads/StaticGroupFromSearch-master/StaticGroupFromSearch-2.py https://casper.kcsd96.org:8443 admin pass healthtest2 -u -f /Users/michael/Desktop/healthtest2.csv user_group users user
performing search on the JSS at: https://casper.kcsd96.org:8443/JSSResource/users/name/username
the JSS matched no results to the provided search value

But when I go to https://casper.kcsd96.org:8443/api/#!/users/findUsersByName_get and punch in that bolded username, I can get all the xml data out of the API page, so I know the entry exists.

I know nothing about python, so I've got no clue how to debug my find & replace method, or if the "match" API behavior will output the same data as a GET on the user's name.

I'm just trying to save myself lots of copy and paste from a CSV into a search field, rinse and repeat every few months.

stoneacheck
New Contributor III

Hmm, okay adapted Jehan's script and go it to work but ran into two things:

1.) The version I got working replaces all users instead of appending. The original script does append, though it does so by adding users to limitation scope, rather than directly to the group. Not a huge deal for our use case.

2.) I'm not sure if it's possible in a shell script to go through a column in a CSV like @brysontyrrell 's original python script posted above - that would be killer, because while I'm glad this works, copy and pasting a single username is equally efficient as just adding the user in the JSS regularly.

https://github.com/stoneacheck/JSS-API-AddUserToStaticGroup/blob/master/UserListTest1.sh - that's my modified version - I left the variables and comments largely named the same just for speed today, I'll clean it up if it actually works. The key hangup versus @jaziz 's script was that in the userList on line 60 he could drill down farther to $ConfigProfileID/subset/scope. Me knowing next to nothing about XML either, I tried replacing all the proper xml fields for user groups and it kept me telling it didn't exist. I assume that's why I'm having trouble with the replace instead of append. That being said, I don't mind replacing because in our use case, it's not the end of the world to just make a new group.

If anyone can point me towards some decent documentation on how to pull a column of usernames out of a simple csv and iterate through them as the variable instead of a static variable as "thisUser" in that'd be a lifesaver. I don't even know if iterating is the best way, are arrays better? This is what learning "programming" looks like folks, lol. I'm hoping one of you brilliant people have a better answer than the 20 mins I've spent reading stack exchange, etc

I'm nearing the edge where I've spent more time trying to figure out the script than it may take me to do it all by hand... and yet now that I'm close I want it to work even more!

BrysonTyrrell
Contributor II

Hey there,

@stoneacheck so if I understand correctly: same concept as my original script but you would also like the ability to generate a static group from a list of users?

I am working on an update to this script that will add two new options for "--update" which will replace an existing group with the new results and "--append" which will attempt to read an existing group and add in new additions without replacing the existing membership.

To your question about reading a CSV file in a bash script, you would use an 'in-field separator' in a loop like this:

while IFS=, read col1 col2 col3
do
    echo "$col1| $col2 | col3"
done < file.csv

In Python you use to super-handy 'csv' library to do all the work for you. In Python the rows of a CSV file are returned as a list object so you can pull out the values via their index:

import csv

with open('/path/to/file.csv', 'rU') as f:
    reader = csv.reader(f)
    for row in reader:
       print(row[0], row[1], row[2]

The 'rU' you see is explicitly saying "open the file in read-only mode using universal newline support" so it can read csv files generated on both Windows and non-Windows systems.

melegastudios
New Contributor II

Just used this to create groups from a CSV files and works perfectly! Thank you for the suggestion @RaulSantos !

nberanger
Contributor

@melegastudios Great you got this working. Can you share your final solution?

Thanks!

melegastudios
New Contributor II

Hey @nberanger ! I just used the python script that @RaulSantos provided at the top. It is from @brysontyrrell github page: https://github.com/brysontyrrell/StaticGroupFromSearch
Are you having issues getting the script to function?

nberanger
Contributor

Thanks @melegastudios From reading the posts, I took it that you had modified things. I'll take a look at that script.

Cheers!

melegastudios
New Contributor II

No problem @nberanger ! I don't know Python inside and out so I just fussed with it until I got it to work properly!

BrysonTyrrell
Contributor II

Would anyone like to submit a PR of their fixes? :)

nberanger
Contributor

@brysontyrrell I would, but I don't know Python at all and wasn't actually able to get this working the way I wanted.

musat
Contributor III

Sorry to bring this back from ages ago, but I am looking for the same thing: creating a user group from a CSV list of users. Before I started poking around figuring out Python in order to update this script to work with users, I was wondering if anyone could post their script to do this.

Tim

arajabzadeh
New Contributor

Hi Guys I was dealing with this and here it is the solution, easiest way to do this 

https://www.youtube.com/watch?v=V5ro96rhn24