在OS X上具有管理员权限的运行脚本 [英] Running script with admin permissions on OS X

查看:91
本文介绍了在OS X上具有管理员权限的运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽力找到了有关Stack Overflow和Internet上许多脚本问题的解决方案,但是我似乎找不到所需的解决方案.

I've tried my best to find out a solution with the many script questions on Stack Overflow and the internet, but I can't seem to find the solution I need.

我想要做的是创建一个自动化程度更高且点击次数更少的解决方案,以删除系统上所有Mobile缓存的用户帐户.我一直在登录并手动进入用户帐户,并通过单击-"按钮,然后单击立即删除"获取用户数据,一次删除一个用户.这是可行的,但很耗时,我的时间可以做得更好.因此,我知道必须有一种使用脚本执行此操作的方法.

What I want to do is create a more automated and less clicking solution to remove all the Mobile cached user accounts on a system. I've been logging in and manually going to user accounts and removing the users one at a time by clicking the "-" button, then clicking "Delete Immediately" for the user data. This works, but is time consuming and I have better things to do with my time. So I knew there had to be a way to do this with a script.

我遇到了以下代码:

for cuser in `dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
    dscl . -delete /Users/$cuser
done

如果我在终端中运行此命令,则会出现权限错误.所以我认为我需要使用sudo运行它.因此,我开始研究创建AppleScripts来运行脚本,但似乎找不到正确的方法.

If I run this in terminal I get permission errors. So I figured I need to run it with sudo. So I started looking into creating AppleScripts to run the script, but I can't seem to find the right way to do it.

有什么想法吗?顺便说一句,我是Mac上脚本编写的新手,所以请注释一下您的代码,以使我知道发生了什么,因此,我不只是运行一些脚本代码而不知道它会做什么.:)

Any ideas? By the way, I'm new to scripting on the Mac, so please comment your code so I know whats happening, and so I don't just run some script code without know what it'll do. :)

谢谢

推荐答案

要执行具有 sudo 或管理员权限的shell脚本,请在您的末尾附加具有管理员权限的 执行shell脚本行.例如:

To perform a shell script with sudo or administrator privileges append with administrator privileges to the end of your do shell script line. For example:

do shell script "/path/to/script/file.sh" user name "adminusershortname" password "password" with administrator privileges

您可以在有关处理 do shell的Apple技术说明中找到更多信息脚本

You can find more on Apple's technote dealing with do shell script

也就是说,将其另存为shell脚本并使用sudo运行shell脚本同样可以正常工作.

That said, saving this as a shell script and running the shell script using sudo would work just as well.

#! /bin/sh

for cuser in `/usr/bin/dscl . -list /Users AuthenticationAuthority | grep LocalCachedUser | awk '{print $1}' | tr '/n' ' '`; do
    /usr/bin/dscl . -delete /Users/$cuser
done

将其另存为removeUser.sh,使用 chmod 将其设置为可执行文件( chmod 755 ),然后运行它( sudo ./removeUser.sh)

Save it as say removeUser.sh, use chmod to set it as executable (chmod 755) and then run it (sudo ./removeUser.sh)

这篇关于在OS X上具有管理员权限的运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆