Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jhbush/3a521be2ac8af6aa7b5f18f8aa1e7ef2 to your computer and use it in GitHub Desktop.
Save jhbush/3a521be2ac8af6aa7b5f18f8aa1e7ef2 to your computer and use it in GitHub Desktop.
Import a list of Departments into your JSS from a text file
#!/bin/sh
# Import a text file containing a list of Departments into your JSS!
# The text file must be formatted with Unix (LF) line breaks
# Important note: if your text file contains & characters, replace them with &
# Change these for your environment
user="jssuser"
pass="jsspass"
jssurl="https://my.jss.org:8443"
file="file.txt"
# Don't modify anything below this line!
# Verify we can read the file
data=`cat $file`
if [[ "$data" == "" ]]; then
echo "Unable to read the file path specified"
echo "Ensure there are no spaces and that the path is correct"
exit 1
fi
# Find how many departments to import
departmentqty=`awk -F, 'END {printf "%s\n", NR}' $file`
echo "departmentqty= " $departmentqty
# Set a counter for the loop
counter="0"
# Loop through the CSV and submit data to the API
while [ $counter -lt $departmentqty ]
do
counter=$[$counter+1]
department=`echo "$data" | head -n $counter | tail -n 1`
echo "Attempting to add department $department"
# Department XML data to post
POSTxml="<department><name>"${department}"</name></department>"
echo "Posting as $POSTxml"
# Submit POST to JSS and write out HTTP return code
curl "$jssurl"/JSSResource/departments/id/0 --user "$user:$pass" -H "Content-Type: text/xml" -X POST -d "${POSTxml}" --write-out \\\n%{http_code} --output -
# Increment the ID variable for the next Department
id=$((id+1))
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment