Tuesday 24 September 2013

How to read webpage in vbscript


Here is an example in which I am using xmlhttp object to get the url source  code.
We can get data from any website using below code.


url="http://www.makaninmumbai.com"
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", url, FALSE)
objHTTP.Send
msgbox  (objHTTP.ResponseText)

How to terminate the process in vbscript

Here is the vbscript  that will terminate or close all the processes with given name

In below script I am closing internet explorer process.

'Create a WMI object
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


'get all processes with name iexplore.exe
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe'")

'terminate each process
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

How to create a word document using vbscript

' Create and Save a Word Document

'Create the Word object
Set objWord = CreateObject("Word.Application")

'Set the caption of the Word
objWord.Caption = "Welcome to Automated Word Script"
objWord.Visible = True

'Add new document
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.Font.Name = "Arial"
objSelection.Font.Size = "18"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()
objSelection.TypeParagraph()

objSelection.Font.Size = "10"



'Start writing to the word document

For i=0 to 10

    objSelection.Font.Bold = True
    objSelection.TypeText "Square of   " & i & " is " & i*i
    objSelection.Font.Bold = False
    objSelection.TypeParagraph()
    objSelection.TypeParagraph()

Next

'finally save the document.
objDoc.SaveAs("C:\mydoc.doc")

'Quit the application
objWord.Quit




Monday 23 September 2013

How to get the IP Address of System in vbscript.

Here is a program that can be run to get the IP address of system.

'Get the WMI object for local computer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'get the Ip address configuration setting using Win32_NetworkAdapterConfiguration Class
'Filter IP configuration records using where condition - Where IPEnabled = True

Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

 'Enumerate Each IP Address configuration

For Each IP_Configuration_Record in IPConfigSet

If Not IsNull(IP_Configuration_Record.IPAddress) Then

For i=0 to UBound(IP_Configuration_Record.IPAddress)
  str = str &  IP_Configuration_Record.IPAddress(i)
Next

    End If

Next

'Display IP Address of the system
msgbox str



How to shutdown a computer in vbscript?


'Warning - if you run this script, your computer will shut down

'Here you can specify name of computer you want to  shutdown.....
'. means local computer
strComputer = "."

'create WMI object

Set objWMIService = GetObject_
    ("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
        strComputer & "\root\cimv2")

'Get the reference of all operating systems in your machine using Win32_OperatingSystem


Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")


'Enumerate each OS and shut down .
For Each objOperatingSystem in colOperatingSystems
    objOperatingSystem.Win32Shutdown(1)
Next

How to Restart a machine in vbscript?


'Warning - if you run this script, your computer will reboot/restart

'Here you can specify name of computer you want to restart.....
'. means local computer
strComputer = "."

'create WMI object
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
        strComputer & "\root\cimv2")

'Get the reference of all operating systems in your machine using Win32_OperatingSystem
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

'Enumerate each OS and restart.
For Each Os in colOperatingSystems
    Os.Reboot()
Next

How to read the environment variables in your system in vbscript?


You can read the system variables using below program


       'Create the WMI object
strComputer = "."
Set objWMIService = GetObject("winmgmts:"  _
           & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

      'get all variables using Win32_Environment Class.
Set colItems = objWMIService.ExecQuery("Select * from Win32_Environment")

       'Enumerate each variable
For Each objItem in colItems
str = str & objItem.Name &  " ----------------->  " & objItem.VariableValue & vbcrlf
Next

Msgbox str


How to list all files in cookies in vbscript?

Cookie is a file stored by the web server on web client machine.
If you want to find out the cookies in your machine you can use below program.

Const COOKIES = &H21&

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(COOKIES)
Set objFolderItem = objFolder.Self

Msgbox "The path of cookies folder is ->  " &  objFolderItem.Path

Set colItems = objFolder.Items
For Each cookie in colItems
   Msgbox  cookie.Name
Next

What is WMI?

WMI stands for Windows Management Instrumentation. This is used to access system hardware and software features. It provides very powerful API to perform tasks like starting process, terminating processes etc.

What is Windows Script Host?

WSH is a scripting host for Windows Script compatible scripting engines.
WSH supports various scripting languages like vbscript, javascript, python etc.

You can do below operations using WSH.

  1. Map network printer/drive
  2. Execute remote script
  3. Send keys to application
  4. Run other applications
  5. Read/Write registry
  6. Access special folders, shortcuts etc

Loops in Vbscript

Here is the list of different looping statements in vbscript.

  1. For ....Next
  2. For Each ...Next
  3. Do While...Loop
  4. Do Until....Loop
  5. While...Wend



'You can exit from For Loop with Exit For statement
'For Loop
For i=1 to 3
  Msgbox i*i
Next

'For Each loop
a  = array(22,33,5,3)
For each e in a
 Msgbox a
Next


'You can exit from Do Loop with Exit Do statement
'do while ...loop
i=1
Do while (i<3)
 Msgbox i*i
 i=i+1
Loop

'do until ....loop
i=1
Do until i>3
 Msgbox i*i
 i=i+1
Loop

'You can not exit from while loop using exit statement.
'while........wend
i=1
While i<=3
 Msgbox i*i
 i=i+1
Wend

What are different conditional statements in vbscript?

In Vbscript there are 2 types of conditional statements.

  1. if ...else
  2. Select Case
Example of if else - 


a = 10

If a > 10 Then
  Msgbox "a is greater than 10"
ElseIf a>0 then
  Msgbox "a is between 0 and 11"
Else
  Msgbox "a is not greater than 0"
End If




Example of select case - 


a = Inputbox("enter the number")


Select Case cint(a)

  Case 1  

     msgbox "You entered 1"

  Case 2
     msgbox "You entered 2"

  Case 3
     msgbox "You entered 3"

  Case else
     msgbox "You entered invalid input"

End Select




What are the different operators in vbscript?

At broad level there are 3 kinds of operators as mentioned below.

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators


Arithmetic Operators in the order of precedence and their usage.
Exponentiation ^
Unary negation -
Multiplication *
Floating point Division /
Integer division \
Modulus arithmetic Mod  -gets the remainder
Addition +
Subtraction -
String concatenation & - concatenates 2 strings

Comparison Operators and their usage.
Equality =
Inequality <>
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Object equivalence Is

Logical Operators in the order of precedence and their usage.
Logical negation Not
Logical conjunction And
Logical disjunction Or
Logical exclusion Xor
Logical equivalence Eqv
Logical implication Imp


Example - 

if a>10 and b<99 then 
    msgbox "a is greater than 10 and b is less than 99"
end if

msgbox "Remainder after dividing 10 by 3 is -> " & (10 mod 3)




































Total Pageviews