Skip to content

Instantly share code, notes, and snippets.

@jc0b
Created June 30, 2024 19:28
Show Gist options
  • Save jc0b/d9151724aebfe5698061e179e904de8b to your computer and use it in GitHub Desktop.
Save jc0b/d9151724aebfe5698061e179e904de8b to your computer and use it in GitHub Desktop.
Munki Computer Namer nopkg
<?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>jc0b</string>
<key>creation_date</key>
<date>2024-06-19T21:07:01Z</date>
<key>munki_version</key>
<string>6.1.0.4536</string>
<key>os_version</key>
<string>15.0</string>
</dict>
<key>autoremove</key>
<false/>
<key>catalogs</key>
<array>
<string>testing</string>
</array>
<key>display_name</key>
<string>Set Computer Name</string>
<key>installcheck_script</key>
<string>#!/bin/zsh
####################################################################################################
# License information
####################################################################################################
#
# Copyright (c) 2024, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# Munki installcheck_script to set computer name
#
# Written by: Jacob Burley &lt;j@jc0b.computer&gt;
# Uses source from "Set Computer Name" (setComputerName.sh) by Armin Briegel
hardware_prefix=$(system_profiler SPHardwareDataType | awk -F ': ' '/Model Name/ {print $NF}' | sed 's/[^A-Z]//g')
computer_name=$(scutil --get ComputerName 2&gt; /dev/null)
local_hostname=$(scutil --get LocalHostName 2&gt; /dev/null)
hostname=$(scutil --get HostName 2&gt; /dev/null)
echo "ComputerName is \"${computer_name}\""
echo "LocalHostName is \"${local_hostname}\""
echo "HostName is \"${hostname}\""
# use serial number as identifier
identifier=$(system_profiler SPHardwareDataType | awk '/Serial Number/ {print $NF}')
# use n digits of the identifier
# depending on your fleet size, 4-6 digits are sensible values
numDigits=6
# get center digits (works best with pre-2021 serial numbers)
offset=$(( (${#identifier} - numDigits) / 2 ))
digits=${identifier:${offset}:${numDigits}}
# verify we actually got the right number of digits
if [[ ${#digits} -ne ${numDigits} ]]; then
echo "something went wrong parsing the identifier, $identifier, $digits"
exit 0
fi
# assemble the name
name="${hardware_prefix}-${digits}"
# clean out special chars for hostnames
new_name=$(tr -d "[:blank:]'&amp;()*%$\"\\\/~?!&lt;&gt;[]{}=+:;,.|^#@" &lt;&lt;&lt; "${name}")
echo "Computer name should be ${new_name}"
if [[ $computer_name != $new_name || $local_hostname != $new_name || $hostname != $new_name ]]; then
echo "Not everything matches, triggering a correction..."
# trigger an install
exit 0
fi
echo "Looks like everything matches, no correction required."
exit 1</string>
<key>installer_type</key>
<string>nopkg</string>
<key>minimum_os_version</key>
<string>10.4.0</string>
<key>name</key>
<string>hostnamer</string>
<key>postinstall_script</key>
<string>#!/bin/zsh
####################################################################################################
# License information
####################################################################################################
#
# Copyright (c) 2024, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# Munki postinstall_script to set computer name
#
# Written by: Jacob Burley &lt;j@jc0b.computer&gt;
# Uses source from "Set Computer Name" (setComputerName.sh) by Armin Briegel
hardware_prefix=$(system_profiler SPHardwareDataType | awk -F ': ' '/Model Name/ {print $NF}' | sed 's/[^A-Z]//g')
# use serial number as identifier
identifier=$(system_profiler SPHardwareDataType | awk '/Serial Number/ {print $NF}')
# use n digits of the identifier
# depending on your fleet size, 4-6 digits are sensible values
numDigits=6
# get center digits (works best with pre-2021 serial numbers)
offset=$(( (${#identifier} - numDigits) / 2 ))
digits=${identifier:${offset}:${numDigits}}
# verify we actually got the right number of digits
if [[ ${#digits} -ne ${numDigits} ]]; then
echo "something went wrong parsing the identifier, $identifier, $digits"
exit 1
fi
# assemble the name
name="${hardware_prefix}-${digits}"
# clean out special chars for hostnames
new_name=$(tr -d "[:blank:]'&amp;()*%$\"\\\/~?!&lt;&gt;[]{}=+:;,.|^#@" &lt;&lt;&lt; "${name}")
echo "Computer name will be set to ${new_name}"
echo "Setting ComputerName..."
scutil --set ComputerName "${new_name}"
echo "Setting LocalHostName..."
scutil --set LocalHostName "${new_name}"
echo "Setting HostName..."
scutil --set HostName "${new_name}"
echo "Done!"</string>
<key>unattended_install</key>
<true/>
<key>version</key>
<string>1.0</string>
</dict>
</plist>
#!/bin/zsh
####################################################################################################
# License information
####################################################################################################
#
# Copyright (c) 2024, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# Munki installcheck_script to set computer name
#
# Written by: Jacob Burley <j@jc0b.computer>
# Uses source from "Set Computer Name" (setComputerName.sh) by Armin Briegel
hardware_prefix=$(system_profiler SPHardwareDataType | awk -F ': ' '/Model Name/ {print $NF}' | sed 's/[^A-Z]//g')
computer_name=$(scutil --get ComputerName 2> /dev/null)
local_hostname=$(scutil --get LocalHostName 2> /dev/null)
hostname=$(scutil --get HostName 2> /dev/null)
echo "ComputerName is \"${computer_name}\""
echo "LocalHostName is \"${local_hostname}\""
echo "HostName is \"${hostname}\""
# use serial number as identifier
identifier=$(system_profiler SPHardwareDataType | awk '/Serial Number/ {print $NF}')
# use n digits of the identifier
# depending on your fleet size, 4-6 digits are sensible values
numDigits=6
# get center digits (works best with pre-2021 serial numbers)
offset=$(( (${#identifier} - numDigits) / 2 ))
digits=${identifier:${offset}:${numDigits}}
# verify we actually got the right number of digits
if [[ ${#digits} -ne ${numDigits} ]]; then
echo "something went wrong parsing the identifier, $identifier, $digits"
exit 0
fi
# assemble the name
name="${hardware_prefix}-${digits}"
# clean out special chars for hostnames
new_name=$(tr -d "[:blank:]'&()*%$\"\\\/~?!<>[]{}=+:;,.|^#@" <<< "${name}")
echo "Computer name should be ${new_name}"
if [[ $computer_name != $new_name || $local_hostname != $new_name || $hostname != $new_name ]]; then
echo "Not everything matches, triggering a correction..."
# trigger an install
exit 0
fi
echo "Looks like everything matches, no correction required."
exit 1
#!/bin/zsh
####################################################################################################
# License information
####################################################################################################
#
# Copyright (c) 2024, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# Munki postinstall_script to set computer name
#
# Written by: Jacob Burley <j@jc0b.computer>
# Uses source from "Set Computer Name" (setComputerName.sh) by Armin Briegel
hardware_prefix=$(system_profiler SPHardwareDataType | awk -F ': ' '/Model Name/ {print $NF}' | sed 's/[^A-Z]//g')
# use serial number as identifier
identifier=$(system_profiler SPHardwareDataType | awk '/Serial Number/ {print $NF}')
# use n digits of the identifier
# depending on your fleet size, 4-6 digits are sensible values
numDigits=6
# get center digits (works best with pre-2021 serial numbers)
offset=$(( (${#identifier} - numDigits) / 2 ))
digits=${identifier:${offset}:${numDigits}}
# verify we actually got the right number of digits
if [[ ${#digits} -ne ${numDigits} ]]; then
echo "something went wrong parsing the identifier, $identifier, $digits"
exit 1
fi
# assemble the name
name="${hardware_prefix}-${digits}"
# clean out special chars for hostnames
new_name=$(tr -d "[:blank:]'&()*%$\"\\\/~?!<>[]{}=+:;,.|^#@" <<< "${name}")
echo "Computer name will be set to ${new_name}"
echo "Setting ComputerName..."
scutil --set ComputerName "${new_name}"
echo "Setting LocalHostName..."
scutil --set LocalHostName "${new_name}"
echo "Setting HostName..."
scutil --set HostName "${new_name}"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment