EA to report type of Hard drive

GabeShack
Valued Contributor III

Anyone already create a script to return what kind of drive is installed? Just looking to group machines by SSD or non-SSD.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools
1 ACCEPTED SOLUTION

brock_walters
Contributor

Hey guys -

Here are 2 other versions on the same theme...

I really like the 1st one posted here (1-liners rule!) I used a case statement to customize the output string written to the Extension Attribute field in the JSS but the result is basically the same. This example also shows how to pattern match with just awk alone :

#!/bin/bash
type=$(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Medium Type/{print $NF}')
case $type in
       SSD) echo "<result>SSD</result>"
            ;;
Rotational) echo "<result>HDD</result>"
            ;;
esac

Below I'm using a loop & a counter but without having to write arrays to a local file which isn't necessary. I'm sure there are other (better) ways to do this just using awk for parsing but the output is formatted nicely & it shows both internal & external storage devices connected to your computers. The weird sed command string is simply for handling spaces nicely in the volume name & the media name strings.

The JSS already collects this information in the individual device record under Storage, but, this Extension Attribute displays the type (SSD, rotational, etc.) as well as the Volume Name & other pertinent info in 1 place. You could definitely remove lines & collect less data if you intended to create an Extension Attribute to be used as a Smart Group criterion. Happy Extension Attributing!

#!/bin/bash
mountpoint=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk -F ":" '/Mount Point/{$1="";print $0}' | /usr/bin/sed 's/ /-,--/g'))
mediumtype=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Medium Type/{print $NF}'))
internal=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Internal/{print $NF}'))
protocol=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Protocol/{print $NF}'))
medianame=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk -F ":" '/Media Name/{$1="";print $0}' | /usr/bin/sed 's/ /-,--/g'))
total="${#internal[@]}"
/bin/echo -n "<result>"
for ((counter=0; counter < "$total"; counter++))
{
    echo "volume: ${mountpoint[$counter]}" | /usr/bin/sed 's/-,--/ /g;s/  //g'
    echo "type: ${mediumtype[$counter]}"
    echo "internal: ${internal[$counter]}"
    echo "protocol: ${protocol[$counter]}"
    echo "media: ${medianame[$counter]}" | /usr/bin/sed 's/-,--/ /g;s/  //g'
    echo
}
echo "</result>"

7ce9da89b9ec4aef93053c195b635a91

View solution in original post

5 REPLIES 5

chris_kemp
Contributor III

Here's ours:

#!/bin/bash

Medium=$(system_profiler SPSerialATADataType | grep Medium Type | sed -e 's/^[Medium Type: ]*//')

echo "<result>$Medium</result>"

bpavlov
Honored Contributor

Perhaps you'll be interested in voting up this feature request so that the data is collected and can be used in smart groups: https://jamfnation.jamfsoftware.com/featureRequest.html?id=2837

flyboy
Contributor

This one deals with multiple storage devices so long as they aren't attached to a RAID card.

#!/bin/bash
set -o nounset                              # Treat unset variables as an error

IFS=$'
'

system_profiler SPSerialATADataType  | grep Medium | awk -F": " '{print $2}' > /tmp/tmp.txt

results=($(cat < /tmp/tmp.txt))
NUMDRIVES=${#results[@]}
counter=0
output=""

if [ $NUMDRIVES -gt 1 ]; then
    while [ $counter -lt `expr $NUMDRIVES - 1` ]; do
        output+=`printf "${results[$counter]};"`
        let counter+=1
    done
    output+=`printf "${results[$counter]}"`
else
    output+=`printf "${results[0]}"`
fi
srm -m /tmp/tmp.txt
echo "<result>$output</result>"

brock_walters
Contributor

Hey guys -

Here are 2 other versions on the same theme...

I really like the 1st one posted here (1-liners rule!) I used a case statement to customize the output string written to the Extension Attribute field in the JSS but the result is basically the same. This example also shows how to pattern match with just awk alone :

#!/bin/bash
type=$(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Medium Type/{print $NF}')
case $type in
       SSD) echo "<result>SSD</result>"
            ;;
Rotational) echo "<result>HDD</result>"
            ;;
esac

Below I'm using a loop & a counter but without having to write arrays to a local file which isn't necessary. I'm sure there are other (better) ways to do this just using awk for parsing but the output is formatted nicely & it shows both internal & external storage devices connected to your computers. The weird sed command string is simply for handling spaces nicely in the volume name & the media name strings.

The JSS already collects this information in the individual device record under Storage, but, this Extension Attribute displays the type (SSD, rotational, etc.) as well as the Volume Name & other pertinent info in 1 place. You could definitely remove lines & collect less data if you intended to create an Extension Attribute to be used as a Smart Group criterion. Happy Extension Attributing!

#!/bin/bash
mountpoint=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk -F ":" '/Mount Point/{$1="";print $0}' | /usr/bin/sed 's/ /-,--/g'))
mediumtype=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Medium Type/{print $NF}'))
internal=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Internal/{print $NF}'))
protocol=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk '/Protocol/{print $NF}'))
medianame=($(/usr/sbin/system_profiler SPStorageDataType | /usr/bin/awk -F ":" '/Media Name/{$1="";print $0}' | /usr/bin/sed 's/ /-,--/g'))
total="${#internal[@]}"
/bin/echo -n "<result>"
for ((counter=0; counter < "$total"; counter++))
{
    echo "volume: ${mountpoint[$counter]}" | /usr/bin/sed 's/-,--/ /g;s/  //g'
    echo "type: ${mediumtype[$counter]}"
    echo "internal: ${internal[$counter]}"
    echo "protocol: ${protocol[$counter]}"
    echo "media: ${medianame[$counter]}" | /usr/bin/sed 's/-,--/ /g;s/  //g'
    echo
}
echo "</result>"

7ce9da89b9ec4aef93053c195b635a91

brock_walters
Contributor