Script to set date and time if date and time is before 2012

GabeShack
Valued Contributor III

Hey all,
I'm trying to write a script to check date and time on a computer at startup to see if it is set before 2012. It would have to be locally run not from the JSS since its fixing the network connection.

The reason is when any of our machines run out of power they reset to a date before our networks certificate was created thereby keeping them from authenticating to our network.

I just need to have a script check the date and time and if its before 2012, set it to after june 2012 and then turn off wifi and turn it back on.

Thanks for any help you can provide!
Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools
2 ACCEPTED SOLUTIONS

davidacland
Honored Contributor II
Honored Contributor II

Here you go:

#!/bin/bash

macdate=$(date +"%Y%m%d")
comparison="20120101"

if [ $comparison -ge $macdate ]; then
    echo "Date is before 2012"
    date 0102010112
else
    echo "Date is after 2012"
fi

It sets the date to 2nd Jan 2012 if its the 1st Jan 2012 or earlier. If its not configured already, I would probably enable an NTP server on the clients after the script has run and they have a network connection again.

View solution in original post

davidacland
Honored Contributor II
Honored Contributor II

Try this one (I take it you are putting it into /Library/LaunchDaemons and want it to run on startup?):

<?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>Label</key>
    <string>com.setdateandtime.app</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Application Support/JAMF/setdateandtime.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

View solution in original post

4 REPLIES 4

davidacland
Honored Contributor II
Honored Contributor II

Here you go:

#!/bin/bash

macdate=$(date +"%Y%m%d")
comparison="20120101"

if [ $comparison -ge $macdate ]; then
    echo "Date is before 2012"
    date 0102010112
else
    echo "Date is after 2012"
fi

It sets the date to 2nd Jan 2012 if its the 1st Jan 2012 or earlier. If its not configured already, I would probably enable an NTP server on the clients after the script has run and they have a network connection again.

davidacland
Honored Contributor II
Honored Contributor II

Hi @gshackney, did that script do what you needed?

GabeShack
Valued Contributor III

Yes actually the script is working perfectly....I'm just not having luck getting the launch daemon to call it at startup.

Here's the modified script that also turns off wifi then turns it back on then waits 10 seconds then sets time to ntp time.

My Daemon doesn't seem to be working though.

#!/bin/bash
macdate=$(date +"%Y%m%d")
comparison="20150101"
if [ $comparison -ge $macdate ]; then
date 0102010115
networksetup -setairportpower en1 off
networksetup -setairportpower en1 on
sleep 10
ntpdate -u time.apple.com
fi

Here is the daemon:

<?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>Label</key>
        <string>com.setdateandtime.app</string>
        <key>ProgramArguments</key>
        <string>/bin/sh</string>
        <string>/Library/Application Support/JAMF/setdateandtime.sh</string>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
    </dict>
</plist>


Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

davidacland
Honored Contributor II
Honored Contributor II

Try this one (I take it you are putting it into /Library/LaunchDaemons and want it to run on startup?):

<?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>Label</key>
    <string>com.setdateandtime.app</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Application Support/JAMF/setdateandtime.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>