Posted on 11-07-2017 07:49 AM
Hey Guys,
I'm wondering if anyone can show me what I'm doing wrong? I wrote a Shell script to force a computer to restart (Using admin details). It works fine when I test it but when running it through Self Service on another computer I receive the error that "It could not be installed"
I feel like I'm missing something obvious:
set username to "MyAdminUsername"
set passwd to "MyPassword"
tell application "Finder" activate display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I'm ready to restart"} default button 1
do shell script "shutdown -r now" user name username password passwd with administrator privileges
end tell
end
Posted on 11-07-2017 07:59 AM
You're using AppleScript syntax but you're using it in a shell script. If you want to use AppleScript it should be scriptname.script. If you want to use applescript within a bash script you need to use the command
osascript -e
OR various other formats like so:
#!/bin/bash
osascript << EOF
tell application "Terminal"
activate
end tell
EOF
EDIT: Self service script run as root so no need for admin credentials
Posted on 11-07-2017 08:24 AM
Thanks for getting back to me. I had a feeling that the AppleScript was causing an issue. I'm a newbie to this. To clarify is this what it should look like?
osascript -e
tell application "Finder" activate display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I'm ready to restart"} default button 1
do shell script "shutdown -r now"
end tell
end
Posted on 11-07-2017 08:45 AM
When submitting code, use the code snippet prompt! Makes reading the code much easier.
#!/bin/bash/
osascript << EOF
tell application "Finder"
display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I am ready to restart"} default button 1
do shell script "shutdown -r now"
end tell
EOF
Posted on 11-07-2017 08:50 AM
@SJBradleyNY for future reference when you want to post a script use the ">_" see screen shot for the example:
#!/bin/bash
osascript << EOF
tell application "Terminal"
activate
end tell
EOF
Posted on 11-07-2017 09:00 AM
Will do - thanks so much!!
Posted on 11-07-2017 09:10 AM
#!/bin/sh
osascript << EOF
tell application "Finder"
display dialog "Thank you for using Fix My Mac - If you still have problems after the restart, contact I.T." buttons {"I am ready to restart"} default button 1
do shell script "shutdown -r now"
end tell
EOF
When self service runs this I still get the cannot be installed message. Should it be running as a .sh file?
Posted on 11-07-2017 09:26 AM
How do you have the policy configured?
Posted on 11-07-2017 09:31 AM
Uploading the shell script into Casper Admin and then have it set up to only be accessible through self service.
Posted on 11-07-2017 09:42 AM
The script runs as root when run from Self Service - does it need to be run as the logged-in user instead?
Posted on 11-07-2017 01:21 PM
@znilsson Since it's displaying a dialog I believe you're correct. I think it needs to run as the logged in user.
@SJBradleyNY The file extensions should be .sh since you're keeping as a shell file. If you were just making it an AppleScript file it would be .script
Posted on 11-07-2017 03:37 PM
There is no need to loop into osascript and back to shell, move the
shutdown -r now
Outside of the EOF tags and run it directly as part of the initial shell script, it probably works the way it is, it's just a bit messy.