Basic but urgent need: script guidance

(imported topic written by JWMurray)

Hello,

I’m a new forum member and hope I can find some quick help here.

In my distant past I was a developer (assembly, PL/1 (!), VB) and thus have some grasp of programming. However, beyond essential DOS .bat files, I’m wholly out of water with respect to scripting.

My environment is Win7 (a rare XP machine exists).

I’ve been assigned responsibility for managing ~ 200 workstations with Endpoint Manger.

I’ve experiment some with basic distributed updates (e.g., applying relevant updates from Microsoft) to a few test machines; was easy enough.

However after reading through a considerable amount of reference material (IBM and others) I’m at a loss how to do the following trivial task, for which I’ve been asked to write a script (ASAP):

  1. Determine correct path to desktop (e.g., c:\Users\username1\Desktop)

  2. Determine if a given folder exists, if not create it off the root, e.g. c:\ArchiveData

  3. Move (or copy) all folders existing on the desktop to c:\Archive data

I can’t think of a simpler assignment, yet after two days of trying to figure it out I’m stuck, and frustrated. And did I mention stressed?

Two main issues:

  • Do I need to worry about “Prefectch” (if so, what?)

  • Can I pull this off with DOS as the script type, or is there another scripting language to use? (If so, can someone provide quick reference to appropriate commands/syntax?

I am in urgent need of a solution and pointers etc. ANY help is greatly appreciated.

(imported comment written by gearoid)

Hi

It’s pretty simple, use this actionscript in your task

// create archive data if it doesn't exist
if {not exists folder "c:\archivedata"}
  folder create "c:\archivedata"
endif
// copy from data dir to archivedata dir, copy subdirectories and suppress prompt
dos xcopy /E /Y c:\data c:\archivedata

The action script
guide
describes all these and have a look at the relevance
guide
too.

I don’t know what you mean by determine correct path to desktop.

Garret

(imported comment written by JWMurray)

Hi Garret –

First, thank you so much for the reply!

Clarifying my dilemma regarding the correct path to the desktop:

On many of the workstations I support there exists more than one user account. Consequently there could be several desktops, one per user “profile”, for e.g.,

c:\users\Jack

c:\users\Jill

c:\users\JHoffa

Thus, the path to the desktops would be c:\users<useracct>\Desktop.

I’m looking for a way to determine what user name(s) exist, and then create c:<useracct>\archivedata for each account in one execution of the script. Thus looping through path names, extracting the relevant ones and storing them in a variable which I then would (ideally) substitute in the snipped you kindly provided me:

if {not exists folder “c:<useracct>\archivedata”}

folder create "c:\<useracct>\archivedata" . . .

[etc.]

(imported comment written by VitaliLukyanau)

profile folders of users

will show it

(imported comment written by gearoid)

You can do this by using createfile command to create a dos .bat file on the computer and then running the bat.

Here’s a simple example

// use createfile to create a bat file on the computer
delete __createfile
createfile until _EOF_
xcopy /E /Y c:\data c:\archivedata
_EOF_

delete c:\temp\backup.bat
move __createfile c:\temp\backup.bat

// exectue the bat file
waithidden cmd.exe c:\temp\backup.bat
delete c:\temp\backup.bat

You can use relveance in the createfile section.

This is substitutued by the agent when it’s creating the file. So somthing like this would create a bat file with a line for each Desktop folder

createfile until _EOF_
{("xcopy /E /Y " & it as string & " c:\archivedata\" & substring before "\Desktop" of substring after "users\" of pathname of it) of folders whose (name of it is "Desktop") of folders of folder "c:\users"}
_EOF_

(imported comment written by JasonWalker)

My answer is similar to Gearoid’s but I do it a little bit differently. I think the best way to learn is to see several different ways of approaching the problem. This post is not to imply that Gearoid’s is wrong - just to show a couple of different examples.

ActionScript and Relevance are going to take some getting used to. With your programming background, you’ll almost need to “un-learn” some of your habits like include files, using loops, variables, etc.

If you haven’t read the Relevance Guides and ActionScript guides, I’d highly recommend them, as well as browsing www.bigfix.me for lots of examples and downloads.

In ActionScript, you can use Relevance Substitutions by enclosing the relevance in {} tags. A common technique, as demonstrated in the earlier posts, is to use ActionScript to build a Windows CMD file on the fly, using Relevance substitution for portions of it, and to then execute the batch file.

For what you’re doing, you should not need the “prefetch” command. Prefetch indicates to download a file (from your server, or from an external website). It’s the same as a Download command, except that Prefetch downloads the file before your action begins (so you can schedule an Action to occur later, but it will begin downloading the files it needs before the action starts).

For what you’re doing, you’d need a Task rather than a Fixlet. Both are similar, but a Fixlet is only considered “successful” if you have changed some condition that the Fixlet uses to determine its Relevance; so it can be found “Not Relevant” after it executes. A Task on the other hand is considered Successful if all of the ActionScript lines execute successfully.

For this Task, the relevance would probably be something simple like “windows of operating system” - which makes it relevant to all Windows machines.

The ActionScript could be something along the lines of

delete "__appendfile"

if {not exists folder "c:\ArchiveData"}

appendfile mkdir c:\ArchiveData

endif

// The following line builds a series of xcopy strings, one for each Profile Folder of User found.  These are concatenated with

// %0d%0a  -- the DOS/WIndows "Carriage Return / Line Feed" pair - so they end up as multiple lines in __appendfile

// The Paths are wrapped in %22 - the doublequote character.

// Note that since BigFix is doing the copying, the target files will be owned by SYSTEM.  If you want the source permissions preserved,

// adjust the XCOPY parameters appropriately.

appendfile {concatenation "%0d%0a" of ("xcopy /E /I /Y /D %22" & it & "%22 %22c:\ArchiveData\" & following text of last "\" of it & "%22") of profile folders of users}

// move / copy ActionScript will fail if the target file already exists - so make sure it doesn't

delete "BackupDesktops.cmd"

// move our generated __appendfile to a .CMD file

move __appendfile BackupDesktops.cmd

// Execute the .CMD file, without displaying it, and wait for execution to complete

waithidden cmd /c BackupDesktops.cmd

My version doesn’t relay on C:\Users being the path - it should work the same for C:\Users, C:\Documents and Settings, or C:\Profiles, as have been used in different versions of Windows. It also won’t copy the Default User or All User desktops, and it won’t try to copy obsolete profiles for users who have been deleted.