-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFixComputerName.pkginfo
217 lines (166 loc) · 6.06 KB
/
FixComputerName.pkginfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>_metadata</key>
<dict>
<key>created_by</key>
<string>aysiu</string>
<key>creation_date</key>
<date>2017-02-14T20:24:42Z</date>
<key>munki_version</key>
<string>2.8.2.2855</string>
<key>os_version</key>
<string>10.12.3</string>
</dict>
<key>autoremove</key>
<false/>
<key>catalogs</key>
<array>
<string>testing</string>
<string>beta</string>
<string>live</string>
</array>
<key>category</key>
<string>NoPkg</string>
<key>description</key>
<string>Fixes the computer name to match display name, user, or notes in the client manifest.</string>
<key>developer</key>
<string>Alan Siu</string>
<key>display_name</key>
<string>Fix Computer Name</string>
<key>icon_name</key>
<string>Finder.png</string>
<key>installcheck_script</key>
<string>#!/bin/bash
# Uncomment only one of these for what key holds the value for the computer name you want to set.
#key_for_name='user'
key_for_name='display_name'
#key_for_name='notes'
cm_exists=0
# Location of client manifest on client
client_manifest='/Library/Managed Installs/manifests/client_manifest.plist'
if [ -f "$client_manifest" ]; then
cm_exists=1
else
# Get name of manifest
manifest_name=$(cat /Library/Managed\ Installs/Logs/ManagedSoftwareUpdate.log | grep "Using manifest" | tail -1 | awk -F "Using manifest: " '{print $2}')
# Alternate location (Munki 3?)
cm_alternate="/Library/Managed Installs/manifests/$manifest_name"
if [ -f "$cm_alternate" ]; then
client_manifest="$cm_alternate"
cm_exists=1
fi
fi
# Array of computer name types
name_types=('ComputerName'
'HostName'
'LocalHostName')
# Function to check desired name against current name. First parameter is name type, and second parameter is the desired name
check_name(){
# Let's get the current name for the name type
current_name=$(scutil --get "$1")
# If they don't match, it's not installed
if [ "$current_name" != "$2" ]; then
# It's not installed if any of these does not match
exit 0
fi
}
# Make sure the computer isn't joined to the domain
adconfig=$(dsconfigad -show)
if [ -z "$adconfig" ]; then
# Check the client manifest exists (it should)
if [ "$cm_exists"==1 ]; then
echo "Client manifest exists at $client_manifest"
# Check that the key we're looking for exists
key_check=$(/usr/libexec/PlistBuddy -c 'print' "$client_manifest" | grep "$key_for_name")
if [ ! -z "$key_check" ]; then
# Get the actual key value and trim whitespace
desired_name=$(/usr/libexec/PlistBuddy -c "print :$key_for_name" "$client_manifest")
# Make sure the desired_name isn't blank, because then you're renaming the computer to nothing
if [ ! -z "$desired_name" ]; then
# Loop through the array of name types and process those
for name_type in "${name_types[@]}"
do
check_name "$name_type" "$desired_name"
done
# End checking the desired name isn't blank
fi
# End checking key exists
fi
# End checking file exists
fi
# End checking not bound to AD
fi
# If we get this far, it's essentially "installed." If, for example, the value is just a bunch of stripped-out whitespace, there's no point in "installing" this item to not change anything.
exit 1</string>
<key>installer_type</key>
<string>nopkg</string>
<key>minimum_os_version</key>
<string>10.4.0</string>
<key>name</key>
<string>FixComputerName</string>
<key>postinstall_script</key>
<string>#!/bin/bash
# Uncomment only one of these for what key holds the value for the computer name you want to set.
#key_for_name='user'
key_for_name='display_name'
#key_for_name='notes'
# Location of client manifest on client
client_manifest='/Library/Managed Installs/manifests/client_manifest.plist'
if [ -f "$client_manifest" ]; then
cm_exists=1
else
# Get name of manifest
manifest_name=$(cat /Library/Managed\ Installs/Logs/ManagedSoftwareUpdate.log | grep "Using manifest" | tail -1 | awk -F "Using manifest: " '{print $2}')
# Alternate location (Munki 3?)
cm_alternate="/Library/Managed Installs/manifests/$manifest_name"
if [ -f "$cm_alternate" ]; then
client_manifest="$cm_alternate"
cm_exists=1
fi
fi
# Array of computer name types
name_types=('ComputerName'
'HostName'
'LocalHostName')
# Function to check desired name against current name. First parameter is name type, and second parameter is the desired name
fix_name(){
# Let's get the current name for the name type
current_name=$(scutil --get "$1")
# If they don't match, make them match
if [ "$current_name" != "$2" ]; then
sudo scutil --set "$1" "$2"
fi
}
# Make sure the computer isn't joined to the domain
adconfig=$(dsconfigad -show)
if [ -z "$adconfig" ]; then
# Check the client manifest exists (it should)
if [ "$cm_exists"==1 ]; then
# Check that the key we're looking for exists
key_check=$(/usr/libexec/PlistBuddy -c 'print' "$client_manifest" | grep "$key_for_name")
if [ ! -z "$key_check" ]; then
# Get the actual key value and trim whitespace
desired_name=$(/usr/libexec/PlistBuddy -c "print :$key_for_name" "$client_manifest")
# Make sure the desired_name isn't blank, because then you're renaming the computer to nothing
if [ ! -z "$desired_name" ]; then
# Loop through the array of name types and process those
for name_type in "${name_types[@]}"
do
fix_name "$name_type" "$desired_name"
done
# End checking the desired name isn't blank
fi
# End checking key exists
fi
# End checking file exists
fi
# End checking not bound to AD
fi</string>
<key>unattended_install</key>
<true/>
<key>version</key>
<string>1.1</string>
</dict>
</plist>