Created
April 30, 2015 00:38
-
-
Save loceee/fa65f4baaad436a84c53 to your computer and use it in GitHub Desktop.
jssVPPUserCreator.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# jssVPPUserCreator.sh | |
# | |
# i should be python | |
# | |
# feed me a csv | |
# user,domain,devicename,department,building | |
# ipad+01,gmail.com,iPad-01,cart1,school | |
# | |
# i assign a user (and create a user record) to mobile device (the device must already be enrolled) | |
# cutting out the requirement for LDAP records for shared device VPP! | |
# read only api user please! | |
apiuser="" | |
apipass="" | |
# if true will overwrite blank location data (eg. department / building) | |
overwriteblankdata=false | |
# if you are using a self signed cert for you jss, tell curl to allow it. blank will read admin | |
selfsignedjsscert=false | |
# if not specified will read from client prefs, and admin prefs | |
#jssurl="" | |
# | |
# | |
# end of configuration | |
# | |
# | |
jssurl="$(defaults read com.jamfsoftware.jss url)" | |
[ "$(defaults read com.jamfsoftware.jss allowInvalidCertificate)" == "1" ] && selfsignedjsscert=true | |
csvfile="${1}" | |
if ${selfsignedjsscert} | |
then | |
curlopts="-k" | |
else | |
curlopts="" | |
fi | |
[ -z "${csvfile}" ] && read -p "csvfile: " csvfile | |
[ -z "${apiuser}" ] && read -p "API/JSSadmin username: " apiuser | |
[ -z "${apipass}" ] && read -s -p "API/JSSadmin password: " apipass | |
if [ ! -f "${csvfile}" ] | |
then | |
echo "${csvfile} don't exist brah! " | |
exit 1 | |
fi | |
makeMobileDeviceXML() | |
{ | |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?> | |
<mobile_device> | |
<location> | |
<username>${username}</username> | |
<real_name>${username}</real_name> | |
<email_address>${email}</email_address> | |
<position>VPP Account</position>" >> "${mbdevxml}" | |
# add extra location data | |
if [ -n "${department}" ] | |
then | |
echo " <department>${department}</department>" >> "${mbdevxml}" | |
elif ${overwriteblankdata} | |
then | |
echo " <department></department>" >> "${mbdevxml}" | |
fi | |
if [ -n "${building}" ] | |
then | |
echo " <building>${building}</building>" >> "${mbdevxml}" | |
elif ${overwriteblankdata} | |
then | |
echo " <building></building>" >> "${mbdevxml}" | |
fi | |
echo " </location> | |
</mobile_device> | |
" >> "${mbdevxml}" | |
} | |
updateJSSRecord() | |
{ | |
echo -en "updating: ${devicename} ... " | |
putresult=$(curl ${curlopts} -H "Accept: application/xml" -s -u "${apiuser}:${apipass}" "${jssurl}JSSResource/mobiledevices/name/$(echo ${devicename} | sed -e 's/ /\+/g')" -T "${mbdevxml}" -X PUT) | |
rm "${mbdevxml}" | |
if [ "$(echo ${putresult} | grep "requires user authentication")" != "" ] | |
then | |
echo "ERROR: problem with api access or credentials" | |
exit 1 | |
fi | |
if [ "$(echo ${putresult} | grep "The server has not found anything matching the request URI")" != "" ] | |
then | |
echo -e "# NO DEVICE RECORD #" | |
else | |
# probably worked :P | |
echo -e "OK" | |
fi | |
} | |
# | |
# main csv parse | |
# | |
echo | |
echo "-------------- processing -----------------" | |
echo | |
while IFS="," read username domain devicename department building | |
do | |
#mbdevxml="$(mktemp -t mbldev.xml)" | |
email="${username}@${domain}" | |
mbdevxml=$(mktemp /tmp/mbdev.xml.XXXXXXXXXXXXXXX) | |
makeMobileDeviceXML | |
updateJSSRecord | |
done < "${csvfile}" | |
echo "--------------- all done ------------------" | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment