-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfServiceAppStoreInstall.bash
More file actions
executable file
·138 lines (127 loc) · 7.01 KB
/
SelfServiceAppStoreInstall.bash
File metadata and controls
executable file
·138 lines (127 loc) · 7.01 KB
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
#!/bin/bash
# SelfServiceAppStoreInstall.bash
# Version: 1.0.1
# Created: 06-03-2022 by Michael Permann
# Modified: 07-13-2024
# Purpose: The script is for installing a Mac App Store app from Self Service. If app is running, the user will
# be notified to save unsaved work and quit the app before proceeding. There will be a countdown timer
# in seconds that will automatically quit the app and start the install if the user doesn't act themselves.
# Parameter 4 is the name of the app to be installed, parameter 5 is the name of the app process,
# parameter 6 is the Self Service policy URL to install the app, parameter 7 is the countdown timer in
# seconds. The script is relatively basic and can't currently kill more than one process or patch
# more than one app.
APP_NAME=$4
APP_PROCESS_NAME=$5
SELF_SERVICE_POLICY_URL=$6
TIMER=$7
CURRENT_USER=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }')
USER_ID=$(/usr/bin/id -u "$CURRENT_USER")
LOGO="/Library/Management/PCC/Images/PCC1Logo@512px.png"
JAMF_HELPER="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
JAMF_BINARY=$(which jamf)
DOCKUTIL="/usr/local/bin/dockutil"
DOCK_ICON_PRESENT="FALSE"
TITLE="Quit Application"
DESCRIPTION="Greetings PERMANNent Computer Consulting LLC Staff
You are attempting to install $APP_NAME which is already installed and currently running. Please return to $APP_NAME and save your work and quit the application BEFORE returning here and clicking the \"OK\" button to proceed with the update.
Caution: your work could be lost if you don't save it and quit $APP_NAME before clicking the \"OK\" button.
The app will automatically quit and the install start once the countdown timer reaches zero or you click the \"OK\" button.
Thanks! - IT Department"
TITLE2="Install Complete"
DESCRIPTION2="Thank You!
$APP_NAME has been installed on your computer. You can launch it now if you wish."
BUTTON1="OK"
DEFAULT_BUTTON="1"
# Add checks for app shortcut on the dock.
INDEX=$("$DOCKUTIL" --find "$APP_NAME" | grep -o 'slot [0-9]\+ in' | grep -o '[0-9]\+')
if [[ "$INDEX" =~ ^[0-9]+$ ]]
then
echo "Dock shortcut for $APP_NAME exists on dock at index position $INDEX"
DOCK_ICON_PRESENT="TRUE"
else
echo "No dock shortcut present no need to add shortcut"
fi
echo "App to Install: $APP_NAME Process Name: $APP_PROCESS_NAME"
echo "Policy URL: $SELF_SERVICE_POLICY_URL Timer Value: $TIMER"
APP_PROCESS_ID=$(/bin/ps ax | /usr/bin/pgrep -x "$APP_PROCESS_NAME" | /usr/bin/grep -v grep | /usr/bin/awk '{ print $1 }')
echo "$APP_NAME process ID $APP_PROCESS_ID"
if [ -z "$APP_PROCESS_ID" ] # Check whether app is running by testing if string length of process id is zero
then
echo "App is NOT running proceed with install"
/bin/rm -rf "/Applications/${APP_NAME}.app"
"$JAMF_BINARY" recon
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" open "$SELF_SERVICE_POLICY_URL"
# Check if app binary does not exist on disk, if it doesn't sleep and check again
while [ ! -e "/Applications/${APP_NAME}.app/Contents/MacOS/${APP_PROCESS_NAME}" ]
do
echo "App binary not on disk yet so sleep"
sleep 5
done
"$JAMF_BINARY" recon
if [ "$DOCK_ICON_PRESENT" = "TRUE" ]
then
echo "Dock icon was present so add it back"
"$DOCKUTIL" --add "/Applications/$APP_NAME.app" --position "$INDEX"
else
echo "Dock icon not present so proceed"
fi
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" "$JAMF_HELPER" -windowType utility -windowPosition lr -title "$TITLE2" -description "$DESCRIPTION2" -icon "$LOGO" -button1 "$BUTTON1" -defaultButton "$DEFAULT_BUTTON"
exit 0
else
echo "App is running so notify user it needs quit"
DIALOG=$(/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" "$JAMF_HELPER" -button1 "$BUTTON1" -windowType utility -title "$TITLE" -defaultButton "$DEFAULT_BUTTON" -alignCountdown center -description "$DESCRIPTION" -countdown -icon "$LOGO" -windowPosition lr -alignDescription left -timeout "$TIMER")
echo "$DIALOG"
if [ "$DIALOG" = "0" ] # Check if the default OK button was clicked or timer expired
then
echo "User chose $BUTTON1 or timer expired so proceeding with install"
APP_PROCESS_ID=$(/bin/ps ax | /usr/bin/pgrep -x "$APP_PROCESS_NAME" | /usr/bin/grep -v grep | /usr/bin/awk '{ print $1 }')
echo "$APP_NAME process ID $APP_PROCESS_ID"
if [ -z "$APP_PROCESS_ID" ] # Check whether app is running by testing if string length of process id is zero
then
echo "User chose $BUTTON1 and app NOT running so proceed with install"
/bin/rm -rf "/Applications/${APP_NAME}.app"
"$JAMF_BINARY" recon
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" open "$SELF_SERVICE_POLICY_URL"
# Check if app binary does not exist on disk, if it doesn't sleep and check again
while [ ! -e "/Applications/${APP_NAME}.app/Contents/MacOS/${APP_PROCESS_NAME}" ]
do
echo "App binary not on disk yet so sleep"
sleep 5
done
"$JAMF_BINARY" recon
if [ "$DOCK_ICON_PRESENT" = "TRUE" ]
then
echo "Dock icon was present so add it back"
"$DOCKUTIL" --add "/Applications/$APP_NAME.app" --position "$INDEX"
else
echo "Dock icon not present so proceed"
fi
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" "$JAMF_HELPER" -windowType utility -windowPosition lr -title "$TITLE2" -description "$DESCRIPTION2" -icon "$LOGO" -button1 "$BUTTON1" -defaultButton "$DEFAULT_BUTTON"
exit 0
else
echo "User chose $BUTTON1 and app is running so killing app process ID $APP_PROCESS_ID"
kill -9 "$APP_PROCESS_ID"
echo "Proceeding with app install"
/bin/rm -rf "/Applications/${APP_NAME}.app"
"$JAMF_BINARY" recon
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" open "$SELF_SERVICE_POLICY_URL"
# Check if app binary does not exist on disk, if it doesn't sleep and check again
while [ ! -e "/Applications/${APP_NAME}.app/Contents/MacOS/${APP_PROCESS_NAME}" ]
do
echo "App binary not on disk yet so sleep"
sleep 5
done
echo "App binary on disk can proceed"
"$JAMF_BINARY" recon
if [ "$DOCK_ICON_PRESENT" = "TRUE" ]
then
echo "Dock icon was present so add it back"
"$DOCKUTIL" --add "/Applications/$APP_NAME.app" --position "$INDEX"
else
echo "Dock icon not present so proceed"
fi
/bin/launchctl asuser "$USER_ID" /usr/bin/sudo -u "$CURRENT_USER" "$JAMF_HELPER" -windowType utility -windowPosition lr -title "$TITLE2" -description "$DESCRIPTION2" -icon "$LOGO" -button1 "$BUTTON1" -defaultButton "$DEFAULT_BUTTON"
exit 0
fi
fi
fi