50 lines
2.0 KiB
Bash
Executable File
50 lines
2.0 KiB
Bash
Executable File
#!/bin/sh
|
|
#########################################################################################
|
|
# Shellscript : Set Computername and read Userinformation
|
|
# Autor : Andreas Vogel, NEXT Enterprise GmbH 2022
|
|
#########################################################################################
|
|
# set -x
|
|
######################### Variablen #####################################################
|
|
jamfpro_url="${4}"
|
|
if [[ $jamfpro_url = "" ]]; then
|
|
echo "ERROR URL variable is empty"
|
|
exit 1
|
|
fi
|
|
|
|
Credentials="${5}"
|
|
if [[ $Credentials = "" ]]; then
|
|
echo "ERROR no Credentials"
|
|
exit 1
|
|
fi
|
|
|
|
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
|
|
authToken=$(/usr/bin/curl "${jamfpro_url}/api/v1/auth/token" --silent --request POST --header "Authorization: Basic ${Credentials}")
|
|
|
|
if [[ $(/usr/bin/sw_vers -productVersion | awk -F . '{print $1}') -lt 12 ]]
|
|
then
|
|
api_token=$(/usr/bin/awk -F \" 'NR==2{print $4}' <<< "$authToken" | /usr/bin/xargs)
|
|
else
|
|
api_token=$(/usr/bin/plutil -extract token raw -o - - <<< "$authToken")
|
|
fi
|
|
|
|
response=$(curl -X GET "$jamfpro_url/JSSResource/computers/serialnumber/$serial" -H "accept: application/xml" -H "Authorization: Bearer ${api_token}")
|
|
assetTag=$(echo $response | /usr/bin/awk -F'<asset_tag>|</asset_tag>' '{print $2}')
|
|
|
|
echo $assetTag
|
|
######################### Setting computername ##########################################
|
|
echo "Setting computer name...$assetTag"
|
|
scutil --set ComputerName "$assetTag"
|
|
scutil --set HostName "$assetTag"
|
|
scutil --set LocalHostName "$assetTag"
|
|
|
|
######################### read Username #################################################
|
|
currentUser=$(stat -f '%u %Su' /dev/console | cut -d ' ' -f 2)
|
|
|
|
endUsername=$(dscl . read /Users/$currentUser RecordName | awk {'print $2'})
|
|
realname=$(dscl . read /Users/$currentUser RealName | tail -n1)
|
|
email=$(dscl . read /Users/$currentUser dsAttrTypeStandard:NetworkUser | awk '{print $2}')
|
|
|
|
|
|
jamf recon -endUsername "$endUsername" -realname "$realname" -email "$email"
|
|
|
|
exit 0 |