1#!/bin/sh
2#!/bin/bash
3
4####################################################################################################
5declare -x appName="AD_ComputerName_Mismatch"
6declare -x appVer="1.0"
7declare -x appAuthor=""
8declare -x appDepartment=""
9declare -x appDate="27-Feb-16"
10declare -x appUpdate="27-Feb-16"
11declare -x templateLastModified="27-Feb-16"
12####################################################################################################
13#
14# Copyright (c) 2015, JAMF Software, LLC. All rights reserved.
15#
16# Redistribution and use in source and binary forms, with or without
17# modification, are permitted provided that the following conditions are met:
18# * Redistributions of source code must retain the above copyright
19# notice, this list of conditions and the following disclaimer.
20# * Redistributions in binary form must reproduce the above copyright
21# notice, this list of conditions and the following disclaimer in the
22# documentation and/or other materials provided with the distribution.
23# * Neither the name of the JAMF Software, LLC nor the
24# names of its contributors may be used to endorse or promote products
25# derived from this software without specific prior written permission.
26#
27# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
28# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
31# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37#
38####################################################################################################
39#
40# ABOUT THIS PROGRAM
41#
42# NAME: AD_ComputerName_Mismatch.sh
43#
44# SYNOPSIS: Script to match device names to record found in Active Directory.
45#
46#
47# DESCRIPTION: Query Active Directory for name assigned to binding for the device and
48# compare to the name found in ComputerName, HostName, and LocalHostName.
49#
50####################################################################################################
51#
52# HISTORY:
53# - Created 27-Feb-16
54#
55#
56# CHANGE LOG
57#
58# Date Version Description
59#--------------------------------------------------------------------------------------------------
60#
61####################################################################################################
62#Script logging
63####################################################################################################
64
65if [ -d /Library/Logs/JAMF ]; then
66 echo "Logging directory already exists."
67else
68 /bin/mkdir /Library/Logs/JAMF
69fi
70declare -x logFile="/Library/Logs/JAMF/com.casper.$appName.log"
71
72
73# sends all standard output and standard error to log file.
74exec >> $logFile
75exec 2>&1
76
77####################################################################################################
78#
79# DEFINE VARIABLES & READ IN PARAMETERS
80#
81####################################################################################################
82
83computerName=`scutil --get ComputerName`
84HostName=`scutil --get HostName`
85adName=`dsconfigad -show | grep "Computer Account" | awk '{print $4}' | rev | cut -c 2- | rev`</``
86
87####################################################################################################
88#Script Tag: Creates a time and date stamp for the execution of this script.
89####################################################################################################
90
91echo "-------------------------------------------"
92printf "Start: $(date)
93"
94printf "Program name: $appName
95"
96printf "ProgramVersion: $appVer
97"
98printf "Author: $appAuthor
99"
100printf "Development Department: $appDepartment
101"
102printf "Program Creation Date: $appDate
103"
104printf "Program Modification date: $appUpDate
105"
106printf "Client serial number: $(ioreg -d 2 -c IOPlatformExpertDevice | grep "IOPlatformSerialNumber" | sed 's/ "IOPlatformSerialNumber" = //' | sed 's/"//g')
107"
108printf "Client name: $(hostname)
109"
110echo "-------------------------------------------"
111
112####################################################################################################
113# SCRIPT CONTENTS
114####################################################################################################
115
116if [ $HostName == $computerName ]; then
117 echo "Computer name matches"
118else
119 scutil --set ComputerName $computerName
120 scutil --set HostName $computerName
121 scutil --set LocalHostName $computerName
122 computerName=`scutil --get ComputerName`
123 echo "Computer name has been set to $computerName"
124fi