Scripting 101 - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Scripting 101

Description:

Allows use of Jscript or VBScript to automate operations. Standard feature of W98/NT/W2000 ... Objects in VBScript. Represented by Variables. General form ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 36
Provided by: clauer5
Category:

less

Transcript and Presenter's Notes

Title: Scripting 101


1
Scripting 101
  • NT Users Group
  • Roger Clauer
  • May 11, 2001

2
Scripting
  • Process to automate repetitive tasks
  • Userid creation
  • System backup
  • Logon Scripts
  • Common Scripting Tools
  • .Bat files
  • Perl
  • Rexx
  • WSH - Vbscript/Jscript

3
WSH - Windows Scripting Host
  • Allows use of Jscript or VBScript to automate
    operations
  • Standard feature of W98/NT/W2000
  • Rich Object Model
  • Shell Object
  • Network Object
  • FileSystem Object

4
WSH V2
  • New Features
  • Include Files
  • XML support for mixed languages in single file
  • Manipulation of Text Files

5
What do you need to get started?
  • A familiarity with Visual Basic
  • VBS is a subset of VB
  • Documentation and differences is on Microsoft
    Scripting web site
  • http//msdn.microsoft.com/scripting
  • Ability to use a basic Text Editor - eg. Notepad

6
VBScript vs. VBA
  • All variables are variants
  • No traditional File I/O
  • Limited Builtin Functions
  • No financial functions
  • No Val, Str, Lset, Rset
  • Reduced Control Flow
  • No GoTo
  • No On Error GoTo

7
Two Applications
  • Wscript
  • Executes in Gui Envirionmnet
  • Cscript
  • Executes in a Console/Command Line Envirionment

8
The Classic
  • Wscript.Echo Hello World

9
Objects in VBScript
  • Represented by Variables
  • General form
  • Set variable object-reference-expression
  • Create object example
  • Dim objShell
  • Set objShell Wscript.CreateObject(Wscript.Shell
    )

10
Accessing Methods and Properties
  • objectref.method arglist
  • Example
  • intRet ojbShell.Run(command ipconfig.exe 0,
    True)
  • Properties (Example2.VBS)
  • dim fso, f, s
  • filespec "c\Program Files"
  • Set fso CreateObject("Scripting.FileSystemObject
    ")
  • Set f fso.GetFolder(filespec)
  • s UCase(f.Name) " uses " f.size " bytes."
  • Wscript.Echo s

11
Wscript Object
  • Methods
  • CreateObject
  • GetObject
  • Echo
  • Quit
  • Sleep

12
Wscript Properties
  • .Fullname
  • .ScriptFullName
  • .Arguments
  • Returns a pointer to the WshArguments Collection

13
Arguments (Example3.VBS)
  • Set oArgs Wscript.Arguments
  • Wscript.Echo "You Entered the following
    Arguments"
  • For i 0 to oArgs.Count-1
  • Wscript.Echo "Argument " i1 " "
    oArgs(i)
  • next

14
WshNetwork Object
  • Exposes the Windows Network allowing Mapping of
    Drives
  • Properties
  • .ComputerName
  • .UserName
  • .UserDomain

15
WshNetwork Methods
  • MapNetworkDrive
  • RemoveNetworkDrive
  • EnumNetworkDrives
  • AddWindowsPrinterConnection
  • RemovePrinterConnection
  • EnumPrinterConnection
  • SetDefaultPrinter

16
WshShell Object
  • Methods
  • Run
  • CreateShortCut Method
  • RegRead, RegWrite, RegDelete
  • ExpandEnvironmentStrings
  • LogEvent
  • AppActivate
  • SendKeys

17
Useability Improvements
  • VBSEdit
  • Debugging
  • Cscript name.vbs //X parms

18
Error Processing
  • Err Object (VBScript)
  • Information about Run Time Errors
  • Properties
  • Number
  • Description
  • On Error Resume Next
  • On Error GoTo 0

19
FileSystem Objects
  • File System Object - Root of the hierarchy
  • Drive - Access to any Drive
  • Folder - Access to file-system Directory
  • File - Access to Files and properties
  • TextStream - I/O to sequential Files

20
Creating FSO
  • Set fso CreateObject(Scripting.FileSystemObject
    )

21
FSO Methods
  • CopyFile
  • CopyFolder
  • CreateFolder
  • DeleteFile
  • DeleteFolder
  • DriveExists
  • FileExists

22
FSO Methods
  • FolderExists
  • MoveFile
  • MoveFolder

23
Accessing Drives
  • ' Example25 Code to Enumerate a Local Machine's
    Drives
  • Dim fso, cDrives, drv, str
  • Set fso CreateObject("Scripting.FileSystemObject
    ")
  • Set cDrives fso.Drives
  • For Each drv In cDrives
  • str str drv.DriveLetter vbCrLf
  • Next
  • MsgBox str

24
Accessing Folders
  • oFolder fso.GetFolder(folderspec)
  • oFolder fso.CreateFolder(folderspec)
  • Where folderspec can be
  • absolute path
  • eg. c\Program Files\Aim95
  • relative path
  • temp\data

25
Folders Properties
  • .Attributes
  • .DateCreated
  • .DateLastAccessed
  • .DateLastModified
  • .Drive
  • .Files
  • .Name
  • .ParentFolder

26
Folder Properties
  • .ShortName
  • .ShortPath
  • .Size
  • .SubFolders
  • .Type

27
Accessing Folders
  • oFolder fso.GetSpecialFolder(folderspec)
  • Where folderspce is a constant
  • Windows Folder 0
  • System Folder 1
  • TemporaryFolder 2

28
File Objects
  • Do not provide access to the content of the files
  • Two methods of Access
  • .Getfile method of FileSystemObject
  • .Files property of Folder Object

29
File Properties
  • .Attributes
  • .DateCreate
  • .DateLastAccessed
  • .DateLastModified
  • .Drive
  • .Name

30
File Properites
  • .ParentFolder
  • .Path
  • .ShortName
  • .ShortPath
  • .Size
  • .Type

31
TextStreamObjects
  • Methods
  • .CreateTextFile of FileSystem Object
  • .OpenTextFile of FileSystem Object
  • .CreateTextFile of Folder Object
  • .OpenAsTextStream of File Object

32
TextStream Objects
  • Methods
  • .ReadLine
  • .WriteLine
  • .Read
  • .Write
  • .SkipLine
  • .Skip

33
TextStream Objects
  • Properties
  • ,AtEndofLine
  • .AtEndofStream
  • .Column
  • .Line

34
Resources
  • Microsoft Scripting Site
  • msdn.microsoft.com/scripting
  • Getting Started with Windows Scripting Host (Tech
    Republic)
  • http//www.techrepublic.com/article.jhtml?idr0022
    0010117jim01.htm
  • Scripting Tutorial (Gunter Born)
  • http//www.borncity.de/WSHBazaar/WSHBazaar.htm

35
Resources
  • Windows 2000 Windows Scrip Host by Tim Hill
    Macmillan Technical Publishing (New Riders)
  • ADSI
  • Msdn.microsoft.com
  • Msdn Library
  • Platform SDK Documentation
  • Network and Directory Services
  • Directory Services
  • Active Direcory Service Interfaces
Write a Comment
User Comments (0)
About PowerShow.com