Export a result of script to a CSV or Excel file (for reporting purposes)

raghdasi
New Contributor III

Hello There,

I am trying to get usernames and Full Names of Mac users within our environment that have Admin Access to their computer. i have a one line script that gets a username name but I need that result to be exported to a file including name of the computer. I really appreciate if anyone can help me about this.
The script that right now i have is: dscl . -read /Groups/admin GroupMembership

I know above script doesn't get the computer name but i am hoping someone out there can let me know how I can include that to the script too.

Thank you in advance.

4 REPLIES 4

PaulHazelden
Contributor III

Use the echo command. Put your variables in where I have the $username and put in the path and file you are using in the second section. The touch command should make sure the file exists to write in to.

touch  /File/Save/location/loggingfile.txt
echo $username >> /File/Save/location/loggingfile.txt
echo $Computername >> /File/Save/location/loggingfile.txt

OR

touch  /File/Save/location/loggingfile.txt
echo "The Username is -$username
The Computer name is - $Computername" >> /File/Save/location/loggingfile.txt

This should put the 2 lines in in one go, or you can have the echo all on one line, in which case it will put one long line into the file.

raghdasi
New Contributor III

Thanks @PaulHazelden. that is a good suggestion. In my case I am trying to write the result to a jss so everyone can access or we can just download the result. I change the scipt as follow:

!/bin/bash

outputFile="servername/file.text

outputFile="$HOME/Desktop/computersList.csv"

Get the computer name

computerName=$(scutil --get ComputerName)

Get the Admin User name:

groupmember=$(dscl . -read Groups/admin GroupMembership | cut -c 18- | sed -E -e 's/(root|yourjamfmanagementaccount)//g')
echo "<result>$groupmember</result>"

Publish it to output file:

echo "$computerName,$groupmember" >> "$outputFile"

exit 0

I want to change the $Home to server. I tired your suggestion and appointed to the server but didn't work.

PaulHazelden
Contributor III

The Server would have to already be mounted to the Mac in /Volumes is the most likely place to look for it.
Depending on the file system used on the server, and the security, it is possible for the script to mount the server and then unmount it after it had done the transfer.
The JSS already has this information, if you go to the Computers, and then click on one to get the details on it. Scroll down to find the Local User Accounts. In the info for this it will show up if an account is an admin account. Slow to search if you have hundreds of computers to go through.

mm2270
Legendary Contributor II

Just curious, but why not use an Extension Attribute for this? If you capture any local admin account names in the EA, you can generate a report in Jamf and export it with the computer names into a csv pretty easily.

Here's the EA I use to capture local admin accounts.

#!/bin/sh

## Script Name:             Local-Admin-Accounts.sh
## Purpose:                 Reports on any local or directory accounts with local admin privileges

## A list of the known local admins to be excluded (separate each name with a "|" character)
known_admins="admin|Admin"

## Initialize array
admin_list=()

for username in $(/usr/bin/dscl . list /Users UniqueID | awk '$2 > 500 {print $1}' | egrep -v "${known_admins}"); do
    if [[ $(/usr/sbin/dseditgroup -o checkmember -m "$username" admin | grep "^yes") ]]; then
    ## Any reported accounts are added to the array list
        admin_list+=("${username}")
    fi
done

## Prints the array's list contents
if [[ "${admin_list[@]}" != "" ]]; then
    echo "<result>${admin_list[@]}</result>"
else
    echo "<result>[ None ]</result>"
fi