Need some help please, skipping chunks of BASH script when run in a Policy.

TajChana
New Contributor II

I hope someone can help me.

The following is a part of a script that i am struggling with. When the following part of the script it run local or run from Casper Remote the script runs with no issues at all. When the script it run from a policy it will always skip the first two dialog boxes and go straight to the while loop and continue looping and never display my dialog boxes prompting the user to enter their Username.

Can someone help please..?

#!/bin/sh



checkAD=`/usr/bin/dscl localhost -list . | grep "Active Directory"`
loggedInUser=$(stat -f%Su /dev/console)
accountCheck=$(dscl . read /Users/$loggedInUser OriginalAuthenticationAuthority 2>/dev/null)
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print $2;exit}" )

mkdir /Users/"$loggedInUser"/Desktop/Log/
LOG=/Users/"$loggedInUser"/Desktop/Log/"$loggedInUser"_ProfileSetup.log
echo "<---------------Start--------------->"  >> $LOG 2>&1
date  >> $LOG 2>&1

# If the machine is not bound to AD, then there's no purpose going any further.
echo "Checking if Mac is Bound to AD"  >> $LOG 2>&1
if [ "${checkAD}" != "Active Directory" ]; then
ps axco pid,command | grep jamfHelper | awk '{ print $1; }' | xargs kill -9
notBound=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" 
-windowType utility 
-lockHUD 
-title "Travelex" 
-description "This Mac is not bound to AD, Please ensure it is bound to AD and re-run. You can do this from Self Service" 
-button1 "Exit" 
-defaultButton 1 
-cancelButton 1 
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns" 
)  >> $LOG 2>&1

#If Button 1 clicked exit otherwise show next window
if [ "$notBound" == "0" ]; then  >> $LOG 2>&1
echo "Already Bound, Exiting"  >> $LOG 2>&1
exit 0

fi
fi

echo "Mac is not bound to AD, Exiting.."  >> $LOG 2>&1

#Checking to see if the user account has already been converted.
echo "Checking to see if the user account has already been converted."  >> $LOG 2>&1
if [ "$accountCheck" != "" ]; then  >> $LOG 2>&1
ps axco pid,command | grep jamfHelper | awk '{ print $1; }' | xargs kill -9
alreadyConverted=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" 
windowType utility 
-lockHUD 
-title "Travelex" 
-description "This account is already an AD account. no need to complete this process." 
-button1 "Exit" 
-defaultButton 1 
-cancelButton 1 
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns" 
)  >> $LOG 2>&1
#If Button 1 clicked exit otherwise show next window
if [ "$alreadyConverted" == "0" ]; then  >> $LOG 2>&1
echo "no need to convert, Already and AD Account. Exiting.."  >> $LOG 2>&1
exit 0

fi
fi

echo "exiting JAMF Helper"  >> $LOG 2>&1
ps axco pid,command | grep jamfHelper | awk '{ print $1; }' | xargs kill -9

#Prompts user to enter their login Username
echo "First prompt for AD Username"   >> $LOG 2>&1
adUsername=`osascript -e 'text returned of (display dialog "Please enter your Travelex Username" default answer "" with icon file "System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GroupIcon.icns")'`  >> $LOG 2>&1
if test $adUsername == ""; then  >> $LOG 2>&1
echo "Exiting as username is blank"  >> $LOG 2>&1
exit 0
fi

ps axco pid,command | grep jamfHelper | awk '{ print $1; }' | xargs kill -9

#Confirm Username.
echo "First Prompt for AD Username Confirmation"   >> $LOG 2>&1
confirmadUsername=`osascript -e 'text returned of (display dialog "Please confirm your Travelex Username" default answer "" with hidden answer with icon file "System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GroupIcon.icns")'`  >> $LOG 2>&1
if test $confirmadUsername == ""; then  >> $LOG 2>&1
echo "Exiting as username confirmation is blank"  >> $LOG 2>&1
exit 0
fi

defaultAttempts=1


#Checks to make sure Usernames match, if they don't displays an error and prompts again.
echo "Checking to ensure entries Match"  >> $LOG 2>&1
while [ $adUsername != $confirmadUsername ] || [ -z $adUsername ]; do  >> $LOG 2>&1
echo "Presenting dialog box as usernames doesnt match"  >> $LOG 2>&1
usernameDoesntMatch=$("/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" 
-windowType utility 
-lockHUD 
-title "Travelex" 
-description "Your usernames do not match. Please Try Again.." 
-button1 "Continue" 
-defaultButton 1 
-icon "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns" 
)  >> $LOG 2>&1

echo "Presenting dialog box for AD username after loop"  >> $LOG 2>&1
adUsername=`osascript -e 'text returned of (display dialog "Please enter your Travelex Username" default answer "" with hidden answer with icon file "System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GroupIcon.icns")'`  >> $LOG 2>&1

echo "Presenting confirmation dialog box after loop"  >> $LOG 2>&1
confirmadUsername=`osascript -e 'text returned of (display dialog "Please confirm your Travelex Username" default answer "" with hidden answer with icon file "System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GroupIcon.icns")'`  >> $LOG 2>&1


defaultAttempts=$((defaultAttempts+1))

if [[ $defaultAttempts -ge 3 ]]; then  >> $LOG 2>&1
echo "exiting as loop has run 3 times"  >> $LOG 2>&1
exit 0
fi

done
0 REPLIES 0