Previous Section Table of Contents Next Section

Writing Encoded Scripts

You write encoded scripts the same way you would almost any script, at least to start. Listing 27.1 shows an example.

Listing 27.1. ResetPW.vbs. An unencoded administrative script written in VBScript.

'get user account

varUserID = inputbox ("Reset password for what user ID?")



'bind to domain user

set objUser = GetObject("WinNT://MyDomain/" & varUserID & _

 ",user")



'make up a random password

varPass = DatePart( "y", Date() )

varPass = varPass & left(varUserID, 2)



'set password

objUser.SetPassword varPass



'show password

WScript.echo "New password is " & varPass

You don't need to add anything special to the file; the Script Encoder recognizes the VBS filename extension and deals with the file appropriately. To encode the file, simply run SCRENC /f resetpw.vbs. The Encoder produces a file named ResetPW.vbe, which is an encoded VBScript file. Here's what it will look like.


#@~^pAEAAA==@#@&BL Y,E/ D,Cm1W;xD@#@&-

mDjknD&fP{~rxaED4G6~crIn/ OPalddSWD[~6W.PS4mY~!/ DP&fQE#@#@&@#@&E4rU9PY

K~NK:lbU~Ek+M@#@&/nO,W8L`d+MPx~V+Y68N+^YvEqkUgK=zztXGG:mkUzrP'~7lD`d+Mq

f,'~JBEk+.Jb@#@&@#@&BsC3 P;2,lP.C

      NG:,2m/dSWMN@#@&\m.nm/dP{P9CD+nm.YvPJHESPGlD+c#~b@#@&-

lMKlk/~x,\l.Km/dPL~^+WD`7lD`/ .qG~~ *@#@&@#@&B/ OPal/kAGD9@#@&W8Lid D 

? Onm/dAKDN~-mDKlkd@#@&@#@&BktWSPaC/khGD9@#@&

      UmDb2Yc+m4G~Jg+SP2lddSW.N,r/,J~',\l.Km/d@#@&@#@&2HoAAA==^#~@

You can also run the Script Encoder with the following syntax: SCRENC inputfile outputfile, in which case you can specify an output filename. The Script Encoder also supports the following command-line parameters:

  • /s. Silent operation, with no feedback to the screen.

  • /f. Instructs the Encoder to overwrite the input file with the output file.

  • /l language. Specifies a new default language, either JScript or VBScript. Overrides the filename extension VBS or JS.

If you're writing scriptlets, you need to add some extra code to your scripts. Scriptlets contain <SCRIPT> and </SCRIPT> tags, as shown here.


<SCRIPT>

'get user account

varUserID = inputbox ("Reset password for what user ID?")



'bind to domain user

set objUser = GetObject("WinNT://MyDomain/" & varUserID & _

 ",user")



'make up a random password

varPass = DatePart( "y", Date() )

varPass = varPass & left(varUserID, 2)



'set password

objUser.SetPassword varPass



'show password

WScript.echo "New password is " & varPass

</SCRIPT>

You need to provide the Encoder with some cue as to how to process the file.


<SCRIPT LANGUAGE="VBSCRIPT">

'**Start Encode**

'get user account

varUserID = inputbox ("Reset password for what user ID?")



'bind to domain user

set objUser = GetObject("WinNT://MyDomain/" & varUserID & _

 ",user")



'make up a random password

varPass = DatePart( "y", Date() )

varPass = varPass & left(varUserID, 2)



'set password

objUser.SetPassword varPass

'show password

WScript.echo "New password is " & varPass

</SCRIPT>

These additions, shown in boldface, tell the Encoder which language to use and where to begin the encoding process. Anything before the **Start Encode** comment line won't be encoded, allowing you to preserve copyright statements and other comments. For example:


<SCRIPT LANGUAGE="VBSCRIPT">

'copyright (c)2003 Don Jones

'**Start Encode**

'get user account

varUserID = inputbox ("Reset password for what user ID?")



'bind to domain user

set objUser = GetObject("WinNT://MyDomain/" & varUserID & _

 ",user")



'make up a random password

varPass = DatePart( "y", Date() )

varPass = varPass & left(varUserID, 2)



'set password

objUser.SetPassword varPass



'show password

WScript.echo "New password is " & varPass

</SCRIPT>

The Encoder can also bulk-encode scripts. Simply provide a wildcard as the input file and a folder name as the output: SCRENC *.vbs c:\encoded. The Encoder encodes each script and places the encoded version in the specified folder.

    Previous Section Table of Contents Next Section