300 dages returret
DAG-TIL-DAG LEVERING
FRI FRAGT FRA 500 KR
100.000 DANSKERE BRUGER OS

Fun with Apple Remote Desktop 3 Scripting

Fun with Apple Remote Desktop 3 Scripting

Since I've posted, and recently updated my Apple Remote Desktop Script Collections, I thought it might be fun to walk through some of them. None are all that big, but they do make some common tasks easier. Obviously, these require Apple Remote Desktop 3 or later, since prior to 3, Apple Remote Desktop wasn't scriptable. I'm not going to do them all in one article either.

 

One of the things that annoys me is how, even though Apple Remote Desktop can install Apple Install Packages to hundreds of remote machines, you have to select the machines manually, and drag and drop the installer, set the options, etc.

 

That's monkey work, and smart administrators automate that. Instead of using Automator for this, you just use vanilla AppleScript and folder actions. (If you want to be really geeky, you could use launchd to handle the folder action part, but there's not a lot of advantage to this since you have to be running in a console session to use Apple Remote Desktop anyway.)

 

The simplest one is an action I use to update my Virex installs. First, I created a computer list called Virex Updatesand populated it with the systems that are running Virex. Then I created the script and attached it to a folder on my desktop. Now, I just drop the Virex update package in the folder and Apple Remote Desktop handles it from there. I don't have to do anything else. Much simpler. If I really wanted to be slick, I could tie this into a launchd item that only ran when I was logged in that looked for new updates automatically, and did this all for me. This particular script doesn't use a task server, but you could easily use one with it. The code with line numbers follows:



    1. on adding folder items to theFolder after receiving theAddedItems --this starts the folder action

    1.      repeat with x in theAddedItems --theAddedItems is the list of items you dropped on the folder

    1.           set theFileInfo to info for x --get info for the downloading file(s)

    1.           set theBaseSize to size of theFileInfo --get initial size

    1.           delay 3 --wait 3 seconds

    1.           set theFileInfo to info for x --get info again

    1.           set theCompareSize to size of theFileInfo --get a newer size

    1.           repeat while theCompareSize ? theBaseSize --if they don't equal, loop until they do

    1.                set theBaseSize to theCompareSize --new base size

    1.                delay 3 --wait three seconds

    1.                set theFileInfo to info for x --get info

    1.                set theCompareSize to size of theFileInfo --get a newer size

    1.           end repeat --once the sizes match, the download is done

    1.      end repeat

    1.      tell application "Remote Desktop" --The actual Apple Remote Desktop part

    1.         set thePackageInstall to make new install package task with properties {delegating to task server:false, encrypting:true, packages:theAddedItems, stopping on error:false} --build the install task

    1.           set theTaskResult to execute thePackageInstall on computer list "Virex Updates" --execute the install task

    1.           display dialog (status of theTaskResult as text) giving up after 60 --OPTIONAL. A little indicator of what happened.

    1.      end tell

    1. end adding folder items to --end of script

 

Now, going through line by line:

 

Lines one and twenty are the folder action handler. In this case, "adding folder items to", since that's the action that kicks everything off.

 

Lines 2 through 14 are there to deal with the fact that there's not a great way of monitoring the status of a file in all situations, and the fact that the "adding items to" action will happily kick off, even though the item's not finished being moved or copied. The Finder Busy Flag doesn't always kick in for all transfer types, so I just use a three second delay and compare loop. If the size hasn't changed in three seconds, it must be done. If you need a longer delay, adjust as necessary.

 

One point of reference; no matter how many items you drop on a folder with this kind of action attached to it, they're always referenced as a list. This can be inconvenient sometimes, but as we'll see, for scripting Apple Remote Desktop installer actions, it's the perfect way to do this.

 

Now, lines 15 through 19 are the Apple Remote Desktop tell block.

 

Line 16 is where we build the install task. With Apple Remote Desktop, pretty much everything you do via scripting is a task, so the standard algorithm is build the task, execute the task. The task we're building is an install package task, so we set thePackageInstall to make new install package task. The specific properties are as follows:



    • delegating to task server:false. [OPTIONAL] If you're using a task server, (and I highly recommend it), set this to true.

    • encrypting:true. [OPTIONAL]This is just too easy to use, and adds a layer of protection to your installs. There's no reason to not enable this.

    • packages:theAddedItems. [MANDATORY]This property is a list of packages you want to install. Remember how I said that the adding items folder action uses a list? Well, so does this property, so we just use the same list that was created when you dropped the packages on the folder.

    • stopping on error:false. [OPTIONAL] This is so that if someone shuts off one of the target computers that you're installing to, it doesn't halt the install to every other computer in the list.



Line 17 exectues the install package task we just built, and specifies which computer list we want to install it to, in this case "Virex Updates". You can specify any valid computer list here. You can also specify specific computers, or selected computers if you like.

Line 18 is totally optional, and one I don't really use, since it makes this be a background thing only, and that is a dialog with the final result of the task. But it's useful for testing.

 

That's the whole script. There's a lot more you can do with it, error checking, etc., but it's a good little shell. Note that if you have multiple packages in theAddedItems, they all go out as one task installing multiple packages, not multiple tasks each installing a separate package. You can change that by iterating through theAddedItems, and building each task separately if you prefer.

 

I'm going to be doing more of these little "Fun with Apple Remote Desktop 3 Scripting" articles in the coming days, so if you have any specific requests, put them in the comments here, and I'll see if I can help you out.

Vil du følge med i flere artikler som denne?