Innovative Application Packaging Cloud

Innovative Application Packaging Cloud
Automated Packaging Self-Service (APS) is an innovative cloud solution that provides smart application packaging, testing and documentation.

Friday, October 21, 2011

what is VBscript(WSH and WMI), its objects and uses in MSI

All of you must be aware of various scripting language but i would like to focus on Vbscripting which is widely used and flexible.i will discuss some basics here on my blog.

What is VBScript?
  1. VBScript is a scripting language
  2. A scripting language is a lightweight programming language
  3. VBScript is a light version of Microsoft's programming language Visual Basic
  4. VBScript Statements and Comments
  •   Multiple lines are specified using ":"
  •   For continued lines use "_"
  •   Comments using “'”

Running VBScript
Method-1
Write VBScript in notepad
Save the VBScript and make sure that the extension of the script is .VBS
Now If you double click on this VBScript it will start running.
Method-2
Go on command prompt
Type Command Wscript followed by the path of your script. This will execute your script.
Example : Wscript c:\HelloWorld.vbs(send output to message box)
                cscript c:\HelloWorld.vbs(send output to command window) 

WSH  (Windows Script Host )
  1. To check version is installed Run “CScript.exe”
  2. A script host is a program that provides an environment in which users can execute scripts in a variety of languages. Examples are Internet Explorer, Command Prompt
Benefits
       To carry out administration tasks
       To handle command line arguments

Components of WSH
 Built-In object :
  •  WScript
 Three COM objects:                
  •  WshShell (Shell Object)
  •  WshNetwork (Network Object)
  •  WshController (Controler Object)

Built-In object  :WScript
Capabilities Provided by the WScript Object
  •  Using COM objects
  •  Handling input and output
  •  Working with command-line arguments
  •   Controlling script execution
  •   Obtaining WSH environment info
Creating a New Instance of a COM Object
    Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
To attach an existing Instance of a COM object
     Set objWMIService = Wscript.GetObject(“winmgmts”)

COM Object : Wshshell
 

WshShell object gives your scripts the ability to work with Windows shell
Capabilities provided by WshShell Object
  • Running programs
  • Working with shortcuts
  • Working with Environment variables
  • Working with Registry
  • Sending keystrokes to an application
To create an Instance of the WshShell Object
    set ObjShell=Wscript.CreateObject(“WScript.Shell”)
COM Objects : WshNetwork Object – Working with Network Drives

The WshNetwork Object provides your scripts with the ability to work with network drives, printers and provides information about the currently logged on user
Example :
Mapping to a network drive
       Set objNetwork = Wscript.CreateObject("WScript.Network")
       objNetwork.MapNetworkDrive "H:", "\ShareXYZ\Sales"  
   Drive G is mapped to the sales share on the file server  named “atl-fs-01”
 Unmapping a network drive
       Set objNetwork = Wscript.CreateObject("WScript.Network")
       objNetwork.RemoveNetworkDrive  "H:", "\\ShareXYZ\Sales"  


COM Objects : WshController Object


  1. One limitation of WSH - scripts could only be run locally .They could not be run against remote computers.
  2. The WshController object, introduced in WSH 5.6, provides that capability.
  3. The WshController object allows you to create a controller script that can run worker scripts against remote computers. The controller script initiates, monitors and the worker script carries out the task
  4. The script that runs on the remote computer is never saved to disk on the remote computer. Instead, the WshController object starts the script within a WSH process in the memory of the remote computer
  5. WshController Object - Setup Required
Both the local and target remote computers must be running WSH version 5.6.

Add a string-valued entry (REG_SZ) named Remote to the registry subkey HKEY_LOCAL_ MACHINE\SOFTWARE\Microsoft\Windows Script Host\Settings and set its value to 1 on all target remote computers.
Example :
    For modifying registry entry in remote machine
        Const HKEY_LOCAL_MACHINE = &H80000002
         strComputer = "RemoteComputerName“
    Set objRegProv =      GetObject("winmgmts:{impersonationLevel=Impersonate}" & _ "!\\" & strComputer & "\root\default:StdRegProv")
         strKeyPath = "SOFTWARE\Microsoft\Windows Script Host\Settings"
     objRegProv.SetStringValue              HKEY_LOCAL_MACHINE,strKeyPath,"Remote","1"


WMI(windows management instrumentation)

Overview
  1. WMI is the management enabling technology built into windows 2000,XP & 2003 family of operating systems. Through WMI all the windows resources can be Accessed, Configured, Managed and Monitored.
  2. There are three steps used in the scripts to retrieve information about a WMI managed resource.
  • To establish a connection to the windows management service on the target computer
  • To retrieve the instance of WMI managed resource
  • To retrieve the properties or set the properties

WMI – Task Categories

Accounts & Domains    (Win32_ComputerSystem)
Computer Hardware     (Win32_OperatingSystem)
Computer Software       (Win32_Product)
Connecting to a remote WMI service   (Win32_Process)
Dates & Times (Win32_OperatingSystem)
Desktop Management    (Win32_ComputerSystem)
Disks & File Systems  (Win32_DiskQuota)
Event Logs   (Win32_NTEventlogFile)
Networking   (Win32_NetworkAdapterConfiguration)
Operating Systems  (Win32_OperatingSystem)
Processes (Win32_Process)
Printers  (Win32_Printer)
Registry (Win32_Registry)
Services
Example:
TerminateProcess "jqs.exe"

Sub TerminateProcess(ProcessName)
 set WmiObject =  GetObject("winmgmts:")
 QueryString = "select * from Win32_Process where Name='" & ProcessName & "'"
 set Result = WmiObject.ExecQuery(QueryString)
  
 For Each RunningProcess in Result
  RunningProcess.Terminate()
 Next
  
 Set WmiObject = Nothing
 Set Result = Nothing
End Sub

Additional Important Objects in the vbscript and its uses MSI
  • FileSystemObject
Perform operations on files(open,close,read,append data),folders and exsisting drives

FileSystemObject objects
  • Dictionary Object
Object that stores data key, item pairs.
  • Drive Object
Provides access to the properties of a particular disk drive or network share.
  • File Object
Provides access to all the properties of a file.
  • FileSystemObject Object
Provides access to a computer's file system.
  • Folder Object
Provides access to all the properties of a folder.
  • TextStream Object
Facilitates sequential access to file.

Reading MSI Properties Using VBScript

temp = SESSION.PROPERTY("INSTALLDIR")   'Here it can be a SOURCEDIR OR CustomActionData property also.
Here Session.Property is used to read the properties of MSI.

Uses of script in MSI
  1. Copy,delete,modify(append data in text files,remove hardcoding,rename) files
  2. Create,delete,update,restore registries
  3. Terminate process
  4. Mapping and checking exsistence of Network Drives
  5. Resolve Public Property and Environment String
This are the basic examples or usage.if you have any you can add it here.



No comments: