Skip to main content
Solved

Custom Patch Management Workflow


Did this topic help you find an answer to your question?
Show first post

112 replies

Forum|alt.badge.img+1
  • New Contributor
  • 6 replies
  • June 4, 2020

Make sure the 'Later -actions "Quit & Update"' is correct in your script.

    updateAnswer="$(/bin/launchctl asuser "$currentUserUID" /usr/local/bin/alerter -title "$4" -sender com.jamfsoftware.selfservice.mac -message "Update Required. Please save your work and close the application." -closeLabel Later -actions "Quit & Update" )"

Forum|alt.badge.img+8

This is the script

#!/bin/bash


# If app is open, alert user with the option to quit the app or defer for later. If user chooses to install it will quit the app, trigger the installation,
# then alert the user the policy is complete with the option to reopen the app. If the app is not open it will trigger the installation without alerting
# Quit and Open path have 2 entries for the times you are quiting/uninstalling an old version of an app that is replaced by a new name (for example quiting Adobe Acrobat Pro, which is replaced by Adobe Acorbat.app)

################################DEFINE VARIABLES################################

# $4 = Title
# $5 = App ID
# $6 = Process Name
# $7 = Jamf Policy Event
# $8 = Quit App Path
# $9 = Open App Path

#Defining the Sender ID as self service due to setting the Sender ID as the actual app being updated would often cause the app to crash
sender="com.jamfsoftware.selfservice.mac"
#Jamf parameters can't be passed into a function, redefining the app path to be used within the funciton
quitPath="$8"
openPath="$9"

################################SETUP FUNCTIONS TO CALL################################

fGetCurrenUser (){
currentUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

  # Identify the UID of the logged-in user
  currentUserUID=`id -u "$currentUser"`
}

fQuitApp (){
cat > /private/tmp/quit_application.sh <<EOF
#!/bin/bash

/bin/launchctl asuser "$currentUserUID" /usr/bin/osascript -e 'tell application "$quitPath" to quit'
EOF

/bin/chmod +x /private/tmp/quit_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/quit_application.sh"
/bin/rm -f "/private/tmp/quit_application.sh"
}

fOpenApp (){
  cat > /private/tmp/open_application.sh <<EOF
#!/bin/bash

/usr/bin/open "$openPath"
EOF

/bin/chmod +x /private/tmp/open_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/open_application.sh"
/bin/rm -f "/private/tmp/open_application.sh"
}

################################SETUP TIMER FILE################################

## Set up the software update time if it does not exist already
if [ ! -e /Library/Application Support/JAMF/.$5.timer.txt ]; then
  echo "2" > /Library/Application Support/JAMF/.$5.timer.txt
fi

## Get the timer value
timer=`cat /Library/Application Support/JAMF/.$5.timer.txt`

################################ALERTER MESSAGE OPTIONS################################

saveQuitMSG="must be quit in order to update. Save all data before quitting."
updatedMSG="has been updated. Thank you."

################################START 'UPDATE WITH ALERTER' PROCESS################################

# Look if app is open via process name
appOpen="$(pgrep -ix "$6" | wc -l)"

# if the app is open and the defer timer is not zero
if [[ $appOpen -gt 0 && $timer -gt 0 ]]; then
    fGetCurrenUser
    updateAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$saveQuitMSG" -closeLabel "Defer ($timer)" -actions "Quit & Update" -timeout 3600)"
    if [[ $updateAnswer == "Quit & Update" ]]; then
        #quit app, install the update, then prompt the user when complete and ask if they want to reopen the app. Message will time out after 60 secs.
        fQuitApp
        /usr/local/bin/jamf policy -event "$7"
        reopenAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$updatedMSG" -closeLabel Ok -actions Reopen -timeout 60)"
        if [[ $reopenAnswer == Reopen ]]; then
            fOpenApp
        fi
        #reset timer after updating
        echo "2" > /Library/Application Support/JAMF/.$5.timer.txt

    else
        let CurrTimer=$timer-1
        echo "User chose to defer"
        echo "$CurrTimer" > /Library/Application Support/JAMF/.$5.timer.txt
        echo "Defer count is now $CurrTimer"
        exit 0
    fi
# if app is open and defer timer has run out
elif [[ $appOpen -gt 0 && $timer == 0 ]]; then
    fGetCurrenUser
    /bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$saveQuitMSG" -actions "Quit & Update" -closeLabel "No Deferrals Left " -timeout 3600
    fQuitApp
    /usr/local/bin/jamf policy -event "$7"
    reopenAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$updatedMSG" -closeLabel Ok -actions Reopen -timeout 60)"
    if [[ $reopenAnswer == Reopen ]]; then
        fOpenApp
    fi
    #reset timer after updating
    echo "2" > /Library/Application Support/JAMF/.$5.timer.txt

else
    # app is not open, reset timer and run updates
    echo "2" > /Library/Application Support/JAMF/.$5.timer.txt
    /usr/local/bin/jamf policy -event "$7"
fi

Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • June 4, 2020

@Captainamerica

Not sure what is going on, I just took your exact script and ran it locally while hard coding the $4-$9 variables and it worked as expected. Are you running it via Jamf or have you tried running it locally and seen if any errors are called out? If it has only been from within Jamf, perhaps check your variable usage? Here's what I used for mine in the test just now, with Chrome:

title="Google Chrome"
appID="com.google.chrome"
process="Google Chrome"
policy="install_googleChromeUpdate"
openpath1="/Applications/Google Chrome.app"
quitpath1="/Applications/Google Chrome.app"


Forum|alt.badge.img+8

hmm - it still just shows the same picture(see in botom). Script looks like below. It is been working probably before is it because some kind of files is some where hidden that it act like this and does not show defer option ?

Script I execute is this

#!/bin/bash


# If app is open, alert user with the option to quit the app or defer for later. If user chooses to install it will quit the app, trigger the installation,
# then alert the user the policy is complete with the option to reopen the app. If the app is not open it will trigger the installation without alerting
# Quit and Open path have 2 entries for the times you are quiting/uninstalling an old version of an app that is replaced by a new name (for example quiting Adobe Acrobat Pro, which is replaced by Adobe Acorbat.app)

################################DEFINE VARIABLES################################

title="Google Chrome"
appID="com.google.chrome"
process="Google Chrome"
policy="install_googleChromeUpdate"
openpath1="/Applications/Google Chrome.app"
quitpath1="/Applications/Google Chrome.app"

#Defining the Sender ID as self service due to setting the Sender ID as the actual app being updated would often cause the app to crash
sender="com.jamfsoftware.selfservice.mac"
#Jamf parameters can't be passed into a function, redefining the app path to be used within the funciton
quitPath="$8"
openPath="$9"

################################SETUP FUNCTIONS TO CALL################################

fGetCurrenUser (){
currentUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

    # Identify the UID of the logged-in user
    currentUserUID=`id -u "$currentUser"`
}

fQuitApp (){
cat > /private/tmp/quit_application.sh <<EOF
#!/bin/bash

/bin/launchctl asuser "$currentUserUID" /usr/bin/osascript -e 'tell application "$quitPath" to quit'
EOF

/bin/chmod +x /private/tmp/quit_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/quit_application.sh"
/bin/rm -f "/private/tmp/quit_application.sh"
}

fOpenApp (){
    cat > /private/tmp/open_application.sh <<EOF
#!/bin/bash

/usr/bin/open "$openPath"
EOF

/bin/chmod +x /private/tmp/open_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/open_application.sh"
/bin/rm -f "/private/tmp/open_application.sh"
}

################################SETUP TIMER FILE################################

## Set up the software update time if it does not exist already
if [ ! -e /Library/Application Support/JAMF/.$5.timer.txt ]; then
    echo "2" > /Library/Application Support/JAMF/.$5.timer.txt
fi

## Get the timer value
timer=`cat /Library/Application Support/JAMF/.$5.timer.txt`

################################ALERTER MESSAGE OPTIONS################################

saveQuitMSG="must be quit in order to update. Save all data before quitting."
updatedMSG="has been updated. Thank you."

################################START 'UPDATE WITH ALERTER' PROCESS################################

# Look if app is open via process name
appOpen="$(pgrep -ix "$6" | wc -l)"

# if the app is open and the defer timer is not zero
if [[ $appOpen -gt 0 && $timer -gt 0 ]]; then
        fGetCurrenUser
        updateAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$saveQuitMSG" -closeLabel "Defer ($timer)" -actions "Quit & Update" -timeout 3600)"
        if [[ $updateAnswer == "Quit & Update" ]]; then
                #quit app, install the update, then prompt the user when complete and ask if they want to reopen the app. Message will time out after 60 secs.
                fQuitApp
                /usr/local/bin/jamf policy -event "$7"
                reopenAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$updatedMSG" -closeLabel Ok -actions Reopen -timeout 60)"
                if [[ $reopenAnswer == Reopen ]]; then
                        fOpenApp
                fi
                #reset timer after updating
                echo "2" > /Library/Application Support/JAMF/.$5.timer.txt

        else
                let CurrTimer=$timer-1
                echo "User chose to defer"
                echo "$CurrTimer" > /Library/Application Support/JAMF/.$5.timer.txt
                echo "Defer count is now $CurrTimer"
                exit 0
        fi
# if app is open and defer timer has run out
elif [[ $appOpen -gt 0 && $timer == 0 ]]; then
        fGetCurrenUser
        /bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$saveQuitMSG" -actions "Quit & Update" -closeLabel "No Deferrals Left " -timeout 3600
        fQuitApp
        /usr/local/bin/jamf policy -event "$7"
        reopenAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$updatedMSG" -closeLabel Ok -actions Reopen -timeout 60)"
        if [[ $reopenAnswer == Reopen ]]; then
                fOpenApp
        fi
        #reset timer after updating
        echo "2" > /Library/Application Support/JAMF/.$5.timer.txt

else
        # app is not open, reset timer and run updates
        echo "2" > /Library/Application Support/JAMF/.$5.timer.txt
        /usr/local/bin/jamf policy -event "$7"
fi


Forum|alt.badge.img+1
  • New Contributor
  • 6 replies
  • June 8, 2020

What version of Alerter are you using? I haven't upgraded yet. Version 2 seems to work for me.


Forum|alt.badge.img+8

have downloaded the latest with catalina support and also worked before. I guess there must be somekind of trigger that causes this message to show instead of the defer option


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • January 7, 2021

Anyone got this working in Big Sur ?. I see the same issue as @Captainamerica I see the notification, but there is not any defer or install button that is seen ?


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • January 7, 2021

Update: It seems the button are now in the "option" dropdown, but the option button does not appear before hovering on it - not to smart. but I just want the buttons to be shown right away as it used to work ?!


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • January 7, 2021

@jameson Looks like is an Apple notification change, not Alerter. The same thing applies for certain native Apple notifications, but not ones like AirDrop receiving.

One project I haven't taken the time to test out but might have different results would be Notifier by dataJAR. Biggest difference I've seen so far is if you want custom logos you would have to modify the Xcode project and re-sign it since it is an App.


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • January 8, 2021

The strange thing is that the alerter notifications does not stay active on the screen but dissapear again. Before Big Sur they stayed active until user did something. If that just could work it would be usable


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • January 8, 2021

@jameson How long until they disappear for you? I'm not having that experience, or at least yet. It's been about 20min and my test notification is still there while I continue to work.


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • January 11, 2021

It just stay 2-3 seconds and then dissapear. Can you try and show the script you are using, so I can compare. I am on Big sur BTW.

EDIT: also tried to test on catalina 10.15.7 - but then just got an message that "alerter will damage the computer". even the file is not in qurantine. So if it works for you which version of alerter do you use ? - I have downloaded the latest on the github that should support big sur


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • January 11, 2021

@jameson I haven't started deploying the newest version of Alerter, but I am running Big Sur on my computer with Alerter working without issue and a lot of our fleet is on 10.15.7 and I haven't heard or noticed any issues.

Here's what I use in my script, with variables being either defined within the policy script options or in a different section of the script:

/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/alerter -title "$4" -sender "$sender" -message "$saveQuitMSG" -closeLabel "Defer ($timer)" -actions "Quit & Update" -timeout 3600)

Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • January 11, 2021

Thanks - can you try and paste the hole script. Do you have any PPPC etc made ?

EDIT: I got this working now. Thanks


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • January 11, 2021

This should still be accurate, with the only difference I can think of is I now use com.apple.AppStore as the Sender ID for all alerts. I wonder if that could be part of your issue? I know at one point I was having crashing errors if I used the actual app ID of the app I was trying to update so I switched to have all use the App Store assuming that it probably wasn't open and would provide an App Icon with the impression of a legit app update being requested.


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • January 12, 2021

I got it working with the new big sur release and did remove the quarantine flag on the file. But you write that you still use the old alerter release (and not the one that was released 3 weeks ago). But the old one will not work on big sur and is catched as malware, for me. So that wonders me you can get this working, unless of course you have disabled something on the build in spam catcher (if that is even possible to do)


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • January 12, 2021

Not doing anything special besides deploying it via a pkg that puts in in the Jamf Application Support folder, not sure what would cause the difference in behavior.


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • March 25, 2021

Anyone still using this on big sur ?. is A PPPC needed so the alerter notification will be showed ? Actually have a strange issue, where on one big sur client the notification from alerter appear, while on another mac also on big sur the message does not appear


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • March 25, 2021

Just last week my computer started flagging Alerter for malware but I have not seen any other reports in our fleet seeing the same behavior. Even tried grabbing the more recent release and it is flagged as well. I have had it included in our Notifications payload since the beginning so there shouldn't be any connection between approving the notifications and it getting flagged.

Due to this issue, I have rewritten the process to use JamfHelper as it will be a more reliable method going forward. Definitely not the same interface I would prefer but it seems the most future proof method at this time. I haven't deployed it yet or done all my testing with it but seems to be keep the desired behavior.

The basic command structure I'm using in the script is this, note I have added a new Script variable for App Icon Path that calls on the .icns file within each app's App Bundle:

/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "$4 must be quit in order to update. Save all data before quitting." -button1 "Quit & Update" -button2 "Defer ($timer)" -timeout 3600


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • March 25, 2021

OK. What I think is really strange that I use the exact same policy and on one big sur it works as expected and other not?. Have you tried it on M1 mac´s, as it is actually on M1 I see the issue where the notification never show up


Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • March 26, 2021

@kendalljjohnson

I have tried to change to jamf helper - but seems something is wrong

#!/bin/sh
#!/bin/bash


# If app is open, alert user with the option to quit the app or defer for later. If user chooses to install it will quit the app, trigger the installation,
# then alert the user the policy is complete with the option to reopen the app. If the app is not open it will trigger the installation without alerting
# Quit and Open path have 2 entries for the times you are quiting/uninstalling an old version of an app that is replaced by a new name (for example quiting Adobe Acrobat Pro, which is replaced by Adobe Acorbat.app)

################################DEFINE VARIABLES################################

#$4 = Title
#$5 = App ID
#$6 = Process Name
#$7 = Jamf Policy Event
#$8 = Quit App Path
#$9 = Open App Path



quitPath="$8"
openPath="$9"

################################SETUP FUNCTIONS TO CALL################################

fGetCurrenUser (){
currentUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

  # Identify the UID of the logged-in user
  currentUserUID=`id -u "$currentUser"`
}

fQuitApp (){
cat > /private/tmp/quit_application.sh <<EOF
#!/bin/bash

/bin/launchctl asuser "$currentUserUID" /usr/bin/osascript -e 'tell application "$quitPath" to quit'
EOF

/bin/chmod +x /private/tmp/quit_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/quit_application.sh"
/bin/rm -f "/private/tmp/quit_application.sh"
}

fOpenApp (){
  cat > /private/tmp/open_application.sh <<EOF
#!/bin/bash

/usr/bin/open "$openPath"
EOF

/bin/chmod +x /private/tmp/open_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/open_application.sh"
/bin/rm -f "/private/tmp/open_application.sh"
}

################################SETUP TIMER FILE################################

## Set up the software update time if it does not exist already
if [ ! -e /Library/Application Support/JAMF/.$5.timer.txt ]; then
  echo "2" > /Library/Application Support/JAMF/.$5.timer.txt
fi

## Get the timer value
timer=$(cat /Library/Application Support/JAMF/.$5.timer.txt)

################################ALERTER MESSAGE OPTIONS################################


################################START 'UPDATE WITH ALERTER' PROCESS################################

# Look if app is open via process name
appOpen=$(pgrep -ix "Google Chrome" | wc -l)

# if the app is open and the defer timer is not zero
if [[ $appOpen -gt 0 && $timer -gt 0 ]]; then
    fGetCurrenUser
    updateAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "Micorosoft Outlook must be quit in order to update. Save all data before quitting." -button1 "Quit & Update" -button2 "Defer ($timer)" -timeout 3600)

    if [[ $updateAnswer == "Quit & Update" ]]; then
        #quit app, install the update, then prompt the user when complete and ask if they want to reopen the app. Message will time out after 60 secs.
        fQuitApp
        /usr/local/bin/jamf policy -event "$7"
        reopenAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "Micorosoft Outlook must be quit in order to update. Save all data before quitting." -button1 "Quit & Update" -button2 "Defer ($timer)" -timeout 3600)
        if [[ $reopenAnswer == Reopen ]]; then
            fOpenApp
        fi
        #reset timer after updating
        echo "2" > /Library/Application Support/JAMF/.$5.timer.txt

    else
        let CurrTimer=$timer-1
        echo "User chose to defer"
        echo "$CurrTimer" > /Library/Application Support/JAMF/.$5.timer.txt
        echo "Defer count is now $CurrTimer"
        exit 0
    fi
# if app is open and defer timer has run out
elif [[ $appOpen -gt 0 && $timer == 0 ]]; then
    fGetCurrenUser
    /bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "Micorosoft Outlook must be quit order to update. Save all data before quitting" -button1 "Quit & Update" -button2 "Defer ($timer)" -timeout 3600)

    fQuitApp
    /usr/local/bin/jamf policy -event "$7"
    reopenAnswer="$(/bin/launchctl asuser "$currentUserUID" /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "Micorosoft Outlook must be quit in order to update. Save all data before quitting." -button1 "Quit & Update" -button2 "Defer ($timer)" -timeout 3600)

    if [[ $reopenAnswer == Reopen ]]; then
        fOpenApp
    fi
    #reset timer after updating
    echo "2" > /Library/Application Support/JAMF/.$5.timer.txt

else
    # app is not open, reset timer and run updates
    echo "2" > /Library/Application Support/JAMF/.$5.timer.txt
    /usr/local/bin/jamf policy -event "$7"
fi

Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • March 26, 2021

You would also have to update the if statements since JamfHelper's output is different than Alerter and I don't think it hurts having it but JamfHelper doesn't require the launchctl asuser aspect. Here's my full script that I've been playing with so far:

#!/bin/bash


# If app is open, alert user with the option to quit the app or defer for later. If user chooses to install it will quit the app, trigger the installation,
# then alert the user the policy is complete with the option to reopen the app. If the app is not open it will trigger the installation without alerting

################################DEFINE VARIABLES################################

# $4 = Title
# $5 = App ID
# $6 = Process Name
# $7 = App Icon Path
# $8 = Jamf Policy Event
# $9 = Quit App Path
# $10 = Open App Path

#Jamf parameters can't be passed into a function, redefining the app path to be used within the funciton
iconPath="$7"
quitPath="$9"
openPath="$10"

################################SETUP FUNCTIONS TO CALL################################

fGetCurrenUser (){
currentUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

  # Identify the UID of the logged-in user
  currentUserUID=`id -u "$currentUser"`
}

fCheckAppIconPath (){
#Check if iconPath is still valid, use PLNU logo if not found
if [ -f "$iconPath" ]; then
    echo "App Icon Path still valid"
else
    echo "App Icon not found, use generic logo"
    iconPath="/Applications/App Store.app/Contents/Resources/AppIcon.icns"
fi
}

fQuitApp (){
cat > /private/tmp/quit_application.sh <<EOF
#!/bin/bash

/bin/launchctl asuser "$currentUserUID" /usr/bin/osascript -e 'tell application "$quitPath" to quit'
EOF

/bin/chmod +x /private/tmp/quit_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/quit_application.sh"
/bin/rm -f "/private/tmp/quit_application.sh"
}

fOpenApp (){
  cat > /private/tmp/open_application.sh <<EOF
#!/bin/bash

/usr/bin/open "$openPath"
EOF

/bin/chmod +x /private/tmp/open_application.sh
/bin/launchctl asuser "$currentUserUID" sudo -iu "$currentUser" "/private/tmp/open_application.sh"
/bin/rm -f "/private/tmp/open_application.sh"
}

################################SETUP TIMER FILE################################

## Set up the software update time if it does not exist already
if [ ! -e /Library/Application Support/JAMF/.$5.timer ]; then
  echo "2" > /Library/Application Support/JAMF/.$5.timer
fi

## Get the timer value
timer=`cat /Library/Application Support/JAMF/.$5.timer`

################################ALERTER MESSAGE OPTIONS################################

saveQuitMSG="must be quit in order to update. Save all data before quitting."
updatedMSG="has been updated. Thank you."

################################START 'UPDATE WITH ALERTER' PROCESS################################

# Look if app is open via process name
appOpen="$(pgrep -ix "$6" | wc -l)"


# if the app is open and the defer timer is not zero
if [[ $appOpen -gt 0 && $timer -gt 0 ]]; then
    fGetCurrenUser
    fCheckAppIconPath
    updateAnswer="$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "$4 must be quit in order to update. Save all data before quitting." -button1 "Quit & Update" -button2 "Defer ($timer)" -timeout 3600)"
    if [[ $updateAnswer == "0" ]]; then
        #quit app, install the update, then prompt the user when complete and ask if they want to reopen the app. Message will time out after 60 secs.
        fQuitApp
        /usr/local/bin/jamf policy -event "$8"
        reopenAnswer="$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -description "$4 has been updated" -button1 "Reopen" -button2 "Ok" -timeout 60)"
        if [[ $reopenAnswer == "0" ]]; then
            fOpenApp
        fi
        #reset timer after updating
        echo "2" > /Library/Application Support/JAMF/.$5.timer

    else
        let CurrTimer=$timer-1
        echo "User chose to defer"
        echo "$CurrTimer" > /Library/Application Support/JAMF/.$5.timer
        echo "Defer count is now $CurrTimer"
        exit 0
    fi
# if app is open and defer timer has run out
elif [[ $appOpen -gt 0 && $timer == 0 ]]; then
    fGetCurrenUser
    fCheckAppIconPath
    /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -heading "Update Available" -description "$4 must be quit in order to update. Save all data before quitting." -button1 "Quit & Update" -button2 "No Deferrals Left" -timeout 3600
    fQuitApp
    /usr/local/bin/jamf policy -event "$8"
    reopenAnswer="$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -windowPosition ur -icon "$iconPath" -iconSize 50 -description "$4 has been updated" -button1 "Reopen" -button2 "Ok" -timeout 60)"
    if [[ $reopenAnswer == "0" ]]; then
        fOpenApp
    fi
    #reset timer after updating
    echo "2" > /Library/Application Support/JAMF/.$5.timer

else
    # app is not open, reset timer and run updates
    echo "2" > /Library/Application Support/JAMF/.$5.timer
    /usr/local/bin/jamf policy -event "$8"
fi

Forum|alt.badge.img+10
  • Contributor
  • 194 replies
  • March 28, 2021

Great thanks - I will give it a go


  • 0 replies
  • March 30, 2021

Thanks a lot


Forum|alt.badge.img+16
  • Author
  • Valued Contributor
  • 105 replies
  • March 31, 2021

Just discovered one issue with the script above when performing the reopen process: the $10 parameter needs to be ${10}. It was interpreting $1 and then a 0, so it it became /0.

Edit line 20 to be:

openPath="${10}"

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings