mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Example Script for VBS Class

Author Message
kirrilian

  • Total Posts : 629
  • Scores: 3
  • Reward points : 0
  • Joined: 3/15/2005
  • Location:
  • Status: offline
Example Script for VBS Class Wednesday, October 26, 2005 7:47 AM (permalink)
0
I gave a scripting class for my co-workers and I used this script for examples. I thought it would make an excellent script to post here since it is HEAVILY commented. 


  'Scripting Class examples
 'stress the importance of comments!!
 
 '**********************************************************
 'basic "Hello World"
 WScript.Echo "Hello World!!"
 'show concatenation
 WScript.Echo "Hello World!!" & " This life is just a test."
 
 '**********************************************************
 'using a variable
 Dim text
 text = "Hello World!!"
 WScript.Echo text
 'briefly explain arrays and hash table
 
 '**********************************************************
 'using a Loop
 For i = 1 To 10
 	WScript.Echo "Loop number: " & i
 	WScript.Echo text
 Next
 
 '**********************************************************
 'testing for conditions
 Dim test
 test = "Howdy World!!"
 If test = "Hello World!!" Then
 	WScript.Echo test & " matches Hello World!!"
 Else
 	WScript.Echo test & " doesn't equal Hello World!!"
 End If
 
 '**********************************************************
 'different types of I/O
 'regular echo
 WScript.Echo text
 
 'input/msg box
 Input = InputBox("Input Something", "Make your selection")
 MsgBox("You entered: " & Input)
 
 'fso
 'create the object
 Set fso = CreateObject("Scripting.FileSystemObject")
 Dim outputFile
 outputFile = "./output.txt"
 Set txtStreamOut = fso.OpenTextFile(OutputFile, 2, True)
 WScript.Echo "Writing out to file."
 For i = 1 To 10
 	txtStreamOut.writeline "Loop number: " & i
 	txtStreamOut.writeline text
 Next
 
 '**********************************************************
 'subroutines and functions
 'good for repetitive code that you want to call over and over
 'a general rule of thumb on which to use:
 'if you need to return something use a function, 
 'if you dont, use a subroutine.
 'neither will do anything until you call them
 'eg add2Array "machine", "file", "missing", "extra", "diff"
 'or flyapart "user@domain.com"
 'subroutine
 Sub add2Array(machine,file,missing,extra,diff)
 	ReDim Preserve outArray(5, arrcount)
 	outArray(0,arrcount) = machine
 	outArray(1,arrcount) = file
 	outArray(2,arrcount) = missing
 	outArray(3,arrcount) = extra
 	outArray(4,arrcount) = diff
 	arrcount = arrcount + 1
 End Sub 'add2Array
 'function
 Function flyapart(input)
 	'breaks up email addys and domain logins of all types
 	'format = domain\username Or username@some.domain.com
 	If InStr(input, "\") > 0 Then
 		domain = lcase(Left(input, InStr(input, "\") - 1))
 		rmEmail = (Len(input)-InStr(input, "\"))
 		strUserName = lcase(Right(input, rmEmail))
 	Elseif InStr(input, "@") > 0 Then
 		rmEmail = (Len(input)-InStr(input, "@")+1)
 		strToReplace = Right(input, rmEmail)
 		quickArray = Split(strToReplace, ".")
 		strUserName = lcase(Replace(input, strToReplace, ""))
 		domain = LCase(Replace(quickarray(0), "@", ""))
 	Else
 		WScript.Echo "That isnt valid input"
 	End If
 End Function 'flyapart
 'gotcha: if you dont use global variables defined at the beginning 
 'of the script a function is useless.
 
 
 '**********************************************************
 'scripting dictionary
 Set objDictionary = CreateObject("Scripting.Dictionary")
 'load up the dictionary
 For i = 1 To 10
 	objDictionary.add test & i,i
 Next
 
 'the power of the dictionary is the ability to quickly search
 If objDictionary.exists("Hello World!!2") Then
 	WScript.Echo "Hello World!!2 exists in the dictionary with data: " & objDictionary("Hello World!!2")
 Else
 	WScript.Echo "Hello World!!2 doesnt exist in the dictionary"
 End If
 
 'output the dictionary
 For Each obj In objDictionary
 	WScript.Echo "The key is: " & obj & " and the associated data is: " & objDictionary(obj)
 Next
 
 '**********************************************************
 'multidimensional arrays
 '2d array that preserves the data if you resize it
 ReDim Preserve outArray(5, 0)
 'the easiest way to increment a dynamic array is To
 'maintain a variable and increment it each time you
 'increase the size of the Array
 Dim arrcount
 arrcount = 0
 
 add2Array "machine", "file", "missing", "extra", "diff"
 add2Array "machine", "file", "missing", "extra", "diff"
 add2Array "machine", "file", "missing", "extra", "diff"
 outputArray
 
 Sub add2Array(machine,file,missing,extra,diff)
 	ReDim Preserve outArray(5, arrcount)
 	outArray(0,arrcount) = machine
 	outArray(1,arrcount) = file
 	outArray(2,arrcount) = missing
 	outArray(3,arrcount) = extra
 	outArray(4,arrcount) = diff
 	arrcount = arrcount + 1
 End Sub 'add2Array
 
 'output the array
 Sub outputArray()
 		WScript.Echo "************ detailed output **************"
 		printOut "machine,src file and date,missing,extra,different(Date)"
 		For j = 0 To arrcount - 1
 				WScript.Echo outArray(0,j) & "," & outArray(1,j)& "," & outArray(2,j) _
 					& "," & outArray(3,j)& "," & outArray(4,j)
 		Next
 End Sub 'outputArray
 
 
 '**********************************************************
 'ADO
 'NOTE: This section wont work unless you have the DSN and DB
 'setup ahead of time.
 'you can use ADO for anything you can store in a recordset
 'eg. data from a db, AD, etc
 
 'sql db example
 Const adOpenStatic = 3
 Const adLockOptimistic = 3
 Const adUseClient = 3
 
 Set objDBConnection = CreateObject("ADODB.Connection")
 Set objDBRecordset = CreateObject("ADODB.Recordset")
 
 'you can make a direct connection or use a windows DSN
 objDBConnection.Open "DSN=PROD_DSN","Username","Password"
 objDBRecordset.CursorLocation = adUseClient
 
 query = "Select * from table_name;"
 
 objDBRecordset.Open query, objDBConnection, _
 	    adOpenStatic, adLockOptimistic
 
 'loop through the output
 While Not objDBRecordset.EOF
 	WScript.Echo objDBRecordset.Fields("Field Name")
 Wend
 
 'close everything
 objDBRecordset.Close
 objDBConnection.Close
 Set objDBRecordSet = Nothing
 Set objDBConnection = Nothing
 
 '**********************************************************
 'WMI & WQL
 'the dot represents the local machine, just put in the hostname
 'of the remote computer and it will get the info from it instead
 strComputer = "."
 
 'WMI namespace to get the data from
 Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
 'WQL query, very similar to SQL
 Set colSettings = objWMIService.ExecQuery _
     ("Select * from Win32_ComputerSystem")
 
 'iterate through the results
 For Each objComputer in colSettings 
     Wscript.Echo "System Name: " & objComputer.Name
     Wscript.Echo "System Manufacturer: " & objComputer.Manufacturer
     Wscript.Echo "System Model: " & objComputer.Model
     Wscript.Echo "Time Zone: " & objComputer.CurrentTimeZone
     Wscript.Echo "Total Physical Memory: " & _
         objComputer.TotalPhysicalMemory
 Next
 
 '**********************************************************
 'ADSI
 'you use the ADO record set to return data from AD as well
 'NOTE there is a 1000 record limit for data returned in AD
 'you can get around this by using the following line
 'objCommand.Properties("Page Size") = 1000
 Set objADConnection = CreateObject("ADODB.Connection")
     objADConnection.Open "Provider=ADsDSOObject;"
 
 Set objADCommand = CreateObject("ADODB.Command")
 	objADCommand.ActiveConnection = objADConnection
 
 'there are two kinds of queries, a LDAP query and Global Catalog query	
 'the syntax is the same: <query_type>;query;returned_values;scope(recurisve or not)
 
 'GC query, you are able to do general queries but the GC doesnt store all attributes
 objGCCommand.CommandText = _
 	"<GC://dc=domain,dc=net>;(&((objectClass=User)(extensionAttribute1='10')));"_
        	& "name,mail;subtree"
 
 'LDAP query, note you have to give the full LDAP path to the object
 objLDAPCommand.CommandText = _
   "<LDAP://ou=terminated,ou=people,dc=domain,dc=com>;(objectCategory=Group);" & _
   "distinguishedName,primaryGroupToken;subtree"
 
 Set objADRecordSet = objADCommand.Execute
 
 While Not objADRecordSet.EOF
 	WScript.Echo objADRecordSet.Fields("name")
 Wend
 
 'close And clear the recordset
 objADRecordSet.Close
 
 Set objADRecordSet = Nothing 

Have you searched [url="http://www.google.com"]here [/url]?
[url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
[url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
#1
    mbouchard

    • Total Posts : 2110
    • Scores: 27
    • Reward points : 0
    • Joined: 5/15/2003
    • Location: USA
    • Status: offline
    RE: Example Script for VBS Class Thursday, October 27, 2005 12:43 AM (permalink)
    0
    Nope, not enough comments.  Must be at least double the length of your script. 

    J/K, this is a great script.  Will pin it if you don't mind.
    Mike

    For useful Scripting links see the Read Me First stickey!

    Always remember Search is your friend.
    #2
      kirrilian

      • Total Posts : 629
      • Scores: 3
      • Reward points : 0
      • Joined: 3/15/2005
      • Location:
      • Status: offline
      RE: Example Script for VBS Class Thursday, October 27, 2005 3:20 AM (permalink)
      0
      Don't mind a bit. 
      Have you searched [url="http://www.google.com"]here [/url]?
      [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
      [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
      #3
        Cybertwister

        • Total Posts : 5
        • Scores: 0
        • Reward points : 0
        • Joined: 11/6/2005
        • Status: offline
        RE: Example Script for VBS Class Sunday, November 06, 2005 1:06 AM (permalink)
        0
        great 
        #4
          ebgreen

          • Total Posts : 8088
          • Scores: 95
          • Reward points : 0
          • Joined: 7/12/2005
          • Status: offline
          RE: Example Script for VBS Class Wednesday, December 21, 2005 6:23 AM (permalink)
          0
          One small quibble with this comment:


          'gotcha: if you dont use global variables defined at the beginning
          'of the script a function is useless.


          I actually stress the avoidance of global variables. Avoiding them complies with the Law of Demeter, decreases coupling, increases orthogonality, and increases code reusability.
          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
          http://www.visualbasicscript.com/m_47117/tm.htm
          #5
            ebgreen

            • Total Posts : 8088
            • Scores: 95
            • Reward points : 0
            • Joined: 7/12/2005
            • Status: offline
            RE: Example Script for VBS Class Wednesday, December 21, 2005 6:25 AM (permalink)
            0
            One more:


            'the easiest way to increment a dynamic array is To
            'maintain a variable and increment it each time you
            'increase the size of the Array


            I find that using UBound to resize arrays is better because that way you don't need to keep track of the array length seperately. If the object (array) has a built in property to do this for me, why should I do it myself?
            "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
            Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
            http://www.visualbasicscript.com/m_47117/tm.htm
            #6
              kirrilian

              • Total Posts : 629
              • Scores: 3
              • Reward points : 0
              • Joined: 3/15/2005
              • Location:
              • Status: offline
              RE: Example Script for VBS Class Wednesday, January 04, 2006 4:44 AM (permalink)
              0
              yea ive since learned different on both points, i just havent updated this script to reflect this, thanks for the comments
              Have you searched [url="http://www.google.com"]here [/url]?
              [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
              [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
              #7
                kirrilian

                • Total Posts : 629
                • Scores: 3
                • Reward points : 0
                • Joined: 3/15/2005
                • Location:
                • Status: offline
                RE: Example Script for VBS Class Friday, November 30, 2007 4:16 PM (permalink)
                0
                I have updated this script, changed things to reflect ebgreen's comments as well as added some new stuff

                I would've just edited the top post but it seems that I cant, so here it is again:

                  'Scripting Class examples
                  'stress the importance of comments!!
                 
                 '**********************************************************
                 'basic "Hello World"
                 WScript.Echo "Hello World!!"
                 'show concatenation
                 WScript.Echo "Hello World!!" & " This life is just a test."
                 'when you need to have quotes in a string
                 wscript.echo "Hello World ""This life is just a test."""
                 
                 '**********************************************************
                 'using a variable
                 Dim text
                 text = "Hello World!!"
                 WScript.Echo text
                 
                 'array
                 colors = array("red","blue","green","yellow","black","white")
                 'explain what an array is and what it is for
                 
                 '**********************************************************
                 'using Loops
                 'using foreach
                 foreach color in colors
                     wscript.echo color
                 next
                 
                 'using a counter
                 For i = 1 To 10
                     WScript.Echo "Loop number: " & i
                     WScript.Echo text
                 Next
                 
                 dim counter
                 counter = 1
                 'loop while condition is true
                 'use do until to loop until something becomes true
                 do while counter < 10
                     counter = counter + 1
                 loop
                 
                 '**********************************************************
                 'testing for conditions
                 Dim test
                 test = "Howdy World!!"
                 If test = "Hello World!!" Then
                     WScript.Echo test & " matches Hello World!!"
                 Else
                     WScript.Echo test & " doesn't equal Hello World!!"
                 End If
                 
                 'select case
                 'GOTCHA - only works for strings
                 foreach color in colors
                     select case color
                         case "blue"
                             wscript.echo "Blue was selected"
                         case "yellow"
                             wscript.echo "Yellow was selected"
                     end case
                     'notice it will only output "blue" and "yellow" from the array
                     'you can also use case else before end case    
                 next
                 
                 'check for the value of an integer
                 do while counter < 10
                     if counter < 5 then
                         wscript.echo "Less than 5"
                     'demonstrate the elseif
                     elseif counter = 5 then
                         wscript.echo "Equals 5"
                     else
                         wscript.echo "Greater than 5"
                     end if
                     counter = counter + 1
                 loop
                 
                 'test for booleans
                 if test = TRUE then
                     wscript.echo "Test is true!"
                 else
                     wscript.echo "Test is false!"
                 end if
                 
                 'there are many other builtin ways to test for things
                 
                 '**********************************************************
                 'different types of I/O
                 'regular echo to screen
                 WScript.Echo text
                 'or
                 WScript.StdOut.Write text
                 'you can also take input from the commandline like this
                 'GOTCHA - only works when script is invoked with cscript
                 '(that is your default scripting host isnt it?)
                 WScript.StdOut.Writeline "Enter your username (domain\username)"
                 WScript.StdOut.Write "> "
                 Username = WScript.StdIn.ReadLine
                 wscript.echo "You entered: " & Username
                 
                 'input/msg box
                 Input = InputBox("Input Something", "Make your selection")
                 MsgBox("You entered: " & Input)
                 
                 'fso
                 'create the object
                 Set fso = CreateObject("Scripting.FileSystemObject")
                 Dim outputFile
                 outputFile = "./output.txt"
                 Set txtStreamOut = fso.OpenTextFile(OutputFile, 2, True)
                 WScript.Echo "Writing out to file."
                 For i = 1 To 10
                     txtStreamOut.writeline "Loop number: " & i
                     txtStreamOut.writeline text
                 Next
                 
                 
                 '**********************************************************
                 'subroutines and functions
                 'good for repetitive code that you want to call over and over
                 'a general rule of thumb on which to use:
                 'if you need to return something use a function, 
                 'if you dont, use a subroutine.
                 'neither will do anything until you call them
                 'eg add2Array "machine", "file", "missing", "extra", "diff"
                 'or flyapart "user@domain.com"
                 
                 'this sub will output to the screen and a file everytime you call it
                 'notice that it uses the fso object we created above
                 Sub printOut(data)
                     WScript.Echo Now() & " --> " & data
                     txtStreamOut.writeline Now() & " --> " & data
                 End Sub 'printOut
                 
                 'subroutine
                 'this sub also shows a dynamic array, note the redim statement
                 Sub add2Array(machine,file,missing,extra,diff)
                     ReDim Preserve outArray(5, arrcount)
                     outArray(0,arrcount) = machine
                     outArray(1,arrcount) = file
                     outArray(2,arrcount) = missing
                     outArray(3,arrcount) = extra
                     outArray(4,arrcount) = diff
                     arrcount = arrcount + 1
                 End Sub 'add2Array
                 
                 'function
                 'in order to return data from a function, set the last variable
                 'to the same name as the function
                 Function flyapart(input)
                     'breaks up email addys and domain logins of all types
                     'format = domain\username Or username@some.domain.com
                     If InStr(input, "\") > 0 Then
                         domain = lcase(Left(input, InStr(input, "\") - 1))
                         rmEmail = (Len(input)-InStr(input, "\"))
                         flyapart = lcase(Right(input, rmEmail))
                     Elseif InStr(input, "@") > 0 Then
                         rmEmail = (Len(input)-InStr(input, "@")+1)
                         strToReplace = Right(input, rmEmail)
                         quickArray = Split(strToReplace, ".")
                         flyapart = lcase(Replace(input, strToReplace, ""))
                         domain = LCase(Replace(quickarray(0), "@", ""))
                     Else
                         WScript.Echo "That isnt valid input"
                     End If
                 End Function 'flyapart
                 'if you wanted to return multiple values from a function you 
                 'would have to use a global variable or two functions
                 
                 '**********************************************************
                 'scripting dictionary
                 Set objDictionary = CreateObject("Scripting.Dictionary")
                 'load up the dictionary
                 For i = 1 To 10
                     objDictionary.add test & i,i
                 Next
                 
                 'the power of the dictionary is the ability to quickly search
                 If objDictionary.exists("Hello World!!2") Then
                     WScript.Echo "Hello World!!2 exists in the dictionary with data: " & objDictionary("Hello World!!2")
                 Else
                     WScript.Echo "Hello World!!2 doesnt exist in the dictionary"
                 End If
                 
                 'output the dictionary
                 For Each obj In objDictionary
                     WScript.Echo "The key is: " & obj & " and the associated data is: " & objDictionary(obj)
                 Next
                 
                 '**********************************************************
                 'multidimensional arrays
                 '2d array that preserves the data if you resize it
                 ReDim Preserve outArray(5, 0)
                 
                 add2Array "machine", "file", "missing", "extra", "diff"
                 add2Array "machine", "file", "missing", "extra", "diff"
                 add2Array "machine", "file", "missing", "extra", "diff"
                 outputArray
                 
                 'the easiest way to increment a dynamic array is use ubound
                 'GOTCHA - using ubound with multidimensional arrays is a bit different
                 'Ubound(MyArray,1) 'Returns the Number of Columns
                 'Ubound(MyArray,2) 'Returns the Number of Rows
                 'source: http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=190
                 Sub add2Array(machine,file,missing,extra,diff)
                     arrcount = ubound(outarray,2)
                     outArray(0,arrcount) = machine
                     outArray(1,arrcount) = file
                     outArray(2,arrcount) = missing
                     outArray(3,arrcount) = extra
                     outArray(4,arrcount) = diff
                     arrcount = arrcount + 1
                     ReDim Preserve outArray(5, arrcount)
                 End Sub 'add2Array
                 
                 'output the array
                 Sub outputArray()
                         WScript.Echo "************ detailed output **************"
                         wscript.echo "machine", "file", "missing", "extra", "diff"
                         For j = 0 To ubound(outarray,2) -1 
                                 WScript.Echo outArray(0,j) & "," & outArray(1,j)& "," & outArray(2,j) _
                                     & "," & outArray(3,j)& "," & outArray(4,j)
                         Next
                 End Sub 'outputArray
                 
                 
                 '**********************************************************
                 'ADO
                 'NOTE: This section wont work unless you have the DSN and DB
                 'setup ahead of time.
                 'you can use ADO for anything you can store in a recordset
                 'eg. data from a db, AD, etc
                 
                 'sql db example
                 Const adOpenStatic = 3
                 Const adLockOptimistic = 3
                 Const adUseClient = 3
                 
                 Set objDBConnection = CreateObject("ADODB.Connection")
                 Set objDBRecordset = CreateObject("ADODB.Recordset")
                 
                 'you can make a direct connection or use a windows DSN
                 objDBConnection.Open "DSN=PROD_DSN","Username","Password"
                 objDBRecordset.CursorLocation = adUseClient
                 
                 query = "Select * from table_name;"
                 
                 objDBRecordset.Open query, objDBConnection, _
                         adOpenStatic, adLockOptimistic
                 
                 'loop through the output
                 While Not objDBRecordset.EOF
                     WScript.Echo objDBRecordset.Fields("Field Name")
                 Wend
                 
                 'close everything
                 objDBRecordset.Close
                 objDBConnection.Close
                 Set objDBRecordSet = Nothing
                 Set objDBConnection = Nothing
                 
                 '**********************************************************
                 'WMI & WQL
                 'the dot represents the local machine, just put in the hostname
                 'of the remote computer and it will get the info from it instead
                 strComputer = "."
                 
                 'WMI namespace to get the data from
                 Set objWMIService = GetObject("winmgmts:" _
                     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                 
                 'WQL query, very similar to SQL
                 Set colSettings = objWMIService.ExecQuery _
                     ("Select * from Win32_ComputerSystem")
                 
                 'iterate through the results
                 For Each objComputer in colSettings 
                     Wscript.Echo "System Name: " & objComputer.Name
                     Wscript.Echo "System Manufacturer: " & objComputer.Manufacturer
                     Wscript.Echo "System Model: " & objComputer.Model
                     Wscript.Echo "Time Zone: " & objComputer.CurrentTimeZone
                     Wscript.Echo "Total Physical Memory: " & _
                         objComputer.TotalPhysicalMemory
                 Next
                 
                 '**********************************************************
                 'ADSI
                 'you use the ADO record set to return data from AD as well
                 'NOTE there is a 1000 record limit for data returned in AD
                 Set objADConnection = CreateObject("ADODB.Connection")
                     objADConnection.Open "Provider=ADsDSOObject;"
                 
                 Set objADCommand = CreateObject("ADODB.Command")
                     objADCommand.ActiveConnection = objADConnection
                 
                 'the following line will allow you to pull more than 1k objects out of AD
                 objADCommand.Properties("Page Size") = 1000
                 
                 'there are two kinds of queries, a LDAP query and Global Catalog query    
                 'the syntax is the same: <query_type>;query;returned_values;scope(recurisve or not)
                 
                 'GC query, you are able to do general queries but the GC doesnt store all attributes
                 objGCCommand.CommandText = _
                     "<GC://dc=domain,dc=net>;(&((objectClass=User)(extensionAttribute1='10')));"_
                            & "name,mail;subtree"
                 
                 'LDAP query, note you have to give the full LDAP path to the object
                 objLDAPCommand.CommandText = _
                   "<LDAP://ou=terminated,ou=people,dc=domain,dc=com>;(objectCategory=Group);" & _
                   "distinguishedName,primaryGroupToken;subtree"
                 
                 Set objADRecordSet = objADCommand.Execute
                 
                 While Not objADRecordSet.EOF
                     WScript.Echo objADRecordSet.Fields("name")
                 Wend
                 
                 'close And clear the recordset
                 objADRecordSet.Close
                 
                 Set objADRecordSet = Nothing 
                 

                Have you searched [url="http://www.google.com"]here [/url]?
                [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
                [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
                #8
                  blksith0

                  • Total Posts : 51
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 10/17/2008
                  • Status: offline
                  RE: Example Script for VBS Class Monday, October 20, 2008 1:24 PM (permalink)
                  0
                  Why do you have to do the Wscript.Echo thing, cant you do the msgbox thing? THats what Ive always done.
                  #9
                    pcgeek86

                    • Total Posts : 10
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 11/17/2008
                    • Status: offline
                    RE: Example Script for VBS Class Thursday, November 20, 2008 7:45 AM (permalink)
                    0
                    If you use the MsgBox function, you're forcing yourself into a GUI application that can't run silently. If you use the Echo() function, you get a message box if you run with wscript.exe, and if you use cscript.exe, then the string gets echoed to the console. This is useful if you're writing scripts meant to run within a console window.

                    Trevor Sullivan
                    Systems Engineer
                    #10
                      Cavalinha

                      • Total Posts : 1
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 11/26/2008
                      • Status: offline
                      RE: Example Script for VBS Class Wednesday, November 26, 2008 7:59 PM (permalink)
                      0
                      Hi :D I have a big problem with my Introduction To Computer Science classes on the uniwersity... We have written a simple macros in excel in Visual Basic but now we have more complicated task (especially this first one) is dangerous for my final mark from this subject... please help me[sm=s14.gif]
                      here is this task: http://jacenty.kis.p.lodz.pl/i2cs/lab_08.pdf
                      #11
                        ebgreen

                        • Total Posts : 8088
                        • Scores: 95
                        • Reward points : 0
                        • Joined: 7/12/2005
                        • Status: offline
                        RE: Example Script for VBS Class Thursday, November 27, 2008 7:12 PM (permalink)
                        0
                        We do not help with homework assignments. If there is some concept that you are having a hard time understanding we can explain it for you, but we won't do the assignment.
                        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                        http://www.visualbasicscript.com/m_47117/tm.htm
                        #12
                          CØLLØSUS

                          • Total Posts : 21
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 1/3/2011
                          • Status: offline
                          Re: RE: Example Script for VBS Class Thursday, January 06, 2011 10:13 PM (permalink)
                          0
                          It works, Check out mine
                          #13
                            shrt

                            • Total Posts : 2
                            • Scores: 0
                            • Reward points : 0
                            • Joined: 6/2/2011
                            • Status: offline
                            Re: Example Script for VBS Class Thursday, June 02, 2011 7:03 PM (permalink)
                            0
                            Hi,
                             
                            I am new to this scripting world, i am trying to learn it...and see the code below
                            this code will show how many systems are online with in the network....
                            ---------------------------------------------------------------------------------------------------------
                            'this program is used to display the ip's which are active between the range of ip's.
                            dim sn,fso, onlinefile,offlinefile,objReadFile,s,n,j,i

                            RangeMin = " 10.12.201.154"
                            RangeMax =  " 10.12.201.158"

                            s=rangemin
                            n=rangemax

                            a = Split(s, ".",4,1)
                            a1 = Split(n, ".",4,1)
                            j=0
                            For i = 0 to 2
                            if a(i)=a1(i) then

                            j=j+1

                            else
                            msgbox "mis match"
                            end if
                            Next
                            if j=3 then
                            'msgbox(s)


                            ping rangemin,rangemax
                            end if

                            sub ping(rangemin,rangemax)
                            msgbox "correct"
                            Set fso = CreateObject("Scripting.FileSystemObject")
                            Set onlinefile = fso.CreateTextFile("D:\onlinefile.txt",true)
                            Set WshShell = CreateObject("WScript.Shell")

                            MinAry = Split(RangeMin, ".", -1)
                            MaxAry = Split(RangeMax, ".", -1)

                            times =  MaxAry(3)- MinAry(3)
                            msgbox(times)
                            For i=1 To times
                            IP = Join(MinAry, ".")
                            'msgbox(IP)
                            PINGFlag = Not CBool(WshShell.run("ping -n 1" & IP ,0,true))

                            If PINGFlag = True Then

                            onlinefile.WriteLine(vbnewline&IP&"     Active    " )
                            Else
                            onlinefile.WriteLine(vbnewline&IP&"     Inactive    " )

                            End If

                            MinAry(3)=MinAry(3)+1

                            Next

                            onlinefile.Close


                            Set objReadFile = fso.OpenTextFile("D:\onlinefile.txt")
                            contents = objReadFile.ReadAll
                            wscript.echo contents

                            Set objShell = CreateObject("WScript.Shell")
                            objShell.Run("notepad " & "D:\onlinefile.txt")
                            end sub
                            --------------------------------------------------------------------------------------------------------
                            for above the code, it shows correct output,
                             
                            check out below code same as above but i made some alteration while taking input to RangeMin,RangeMax.
                            in above code hard coded input i.e.,
                             
                            RangeMin = " 10.12.201.154"
                            RangeMax = " 10.12.201.157"
                             
                            but in below code,i am receiving input from inputbox...i.e,
                            RangeMin = inputbox("min range")
                            RangeMax = inputbox("max range")
                             
                            But problem is from below code, its executing but output is not correct...
                            i made changes in receiving input datz it....
                            -------------------------------------------------------------------------------------------------
                            'this program is used to display the ip's which are online and offline between the range of ip's.
                            'Set the values to test

                            dim s,n,rangemin,rangemax,IP

                            ping rangemin,rangemax

                            sub ping(rangemin,rangemax)


                            RangeMin = inputbox("min range")'" 10.12.201.154"
                            RangeMax = inputbox("max range")'" 10.12.201.156"

                            s=rangemin
                            n=rangemax

                            a = Split(s, ".",4,1)
                            a1 = Split(n, ".",4,1)

                            j=0

                            For i = 0 to 2
                            if a(i)=a1(i) then
                            j=j+1
                            else
                            msgbox "first 3 octel must same"
                            ping rangemin,rangemax
                            'exit for
                            end if
                            Next

                            if j=3 then

                            Set fso = CreateObject("Scripting.FileSystemObject")
                            Set onlinefile = fso.CreateTextFile("D:\onlinefile.txt")
                            Set WshShell = CreateObject("WScript.Shell")

                            Min = Split(RangeMin, ".", -1, 1)
                            Max = Split(RangeMax, ".", -1, 1)

                            times =  Max(3)- Min(3)

                            For i=1 To times

                            IP = Join(Min, ".")
                            msgbox(ip)
                            PINGFlag = Not CBool(WshShell.run("ping -n 1" &IP ,0,True))
                            If PINGFlag = True Then
                            'msgbox "this ip address is ONLINE :" & IP     
                            onlinefile.WriteLine(vbnewline&IP&"     Active    " )
                            Else

                            'msgbox "this ip address is offLINE :" & IP
                            onlinefile.WriteLine(vbnewline&IP&"     INActive    " )

                            End If
                            Min(3)=Min(3)+1
                            Next

                            'msgbox "the active ips are shown, thankyou"
                            'ping rangemin,rangemax
                            end if

                            end sub
                            ------------------------------------------------------------------------------------------------------
                            please anyone explain me ,what is the probs in this code....:)
                            #14
                              govindagouda

                              • Total Posts : 1
                              • Scores: 0
                              • Reward points : 0
                              • Joined: 6/16/2011
                              • Status: offline
                              Re:Example Script for VBS Class Thursday, June 16, 2011 5:00 AM (permalink)
                              0
                              Hi,
                               
                              Need your help in getting the below script executed. When I execute this I get error at line
                              naviseccli  -h 10.129.8.151 bind r5 count -rg 100 -sp sp -sq bc -cap capacity
                               
                              NAviseccli is installed on my pc and it is exe command.
                               
                               
                              REM <script type="text/vbscript">
                              For count = 4010 to 4020
                              rem capacity of the LUN in Blocks
                              capacity =209710080
                              rem To Assign SP A for Even and SP B for Odd Numbered Lun
                              If count MOD 2 = 0 Then
                               sp = a
                              else
                               sp = b
                              End if
                              msgbox count
                              rem create LUN
                              naviseccli  -h 10.129.8.151 bind r5 count -rg 100 -sp sp -sq bc -cap capacity
                              Next
                              REM </script>
                              #15
                                StarWarsMG

                                • Total Posts : 9
                                • Scores: 0
                                • Reward points : 0
                                • Joined: 8/17/2011
                                • Location: Minnesota
                                • Status: offline
                                Re:Example Script for VBS Class Wednesday, August 17, 2011 2:37 AM (permalink)
                                0
                                Here is mine...
                                 
                                do 
                                x =msgbox("DO NOT PRESS",0,"   ")
                                x =msgbox("*Ahem* DO NOT PRESS",0,"   ")
                                x =msgbox("... you pressed it again. Okay punk, press it again",0,"   ")
                                x =msgbox("Yeah, thats it. Come on, one more time.",0,"   ")
                                x =msgbox("Again",0,"   ")
                                x =msgbox("Do it.",0,"   ")
                                x =msgbox("Okay, now you've had your fill. Stop clicking.",0,"   ")
                                x =msgbox("You see, this is why we can't be friends.",0,"   ")
                                x =msgbox("You're just too selfish.",0,"   ")
                                x =msgbox("Grr... now you've asked for it. Do NoT pReSs ThE bUtToN!",0,"   ")
                                x =msgbox("You seem to be immune to my mind control.",0,"   ")
                                x =msgbox("Time for Plan B. Press it. You know to want to.",0,"   ")
                                x =msgbox("Mwahahaha! Not let's see you press it! LOSER!",16,"   ")
                                x =msgbox("...this displeases me.",0,"   ")
                                x =msgbox("HA! I have replaced the button with this Yes and No button. Now what are you going to do?",4,"   ")
                                x =msgbox("...I hate you.",0,"   ")
                                x =msgbox("In that 'cut off your head with a toothbrush kind of way'",0,"   ")
                                x =msgbox("Quick! What's that behind you?!",0,"   ")
                                x =msgbox("...",0,"   ")
                                x =msgbox("Okay Okay. You can press the button. I don't care.",0,"   ")
                                x =msgbox("No really, I don't care anymore.",0,"   ")
                                x =msgbox("In fact, I lost interest a while ago.",0,"   ")
                                x =msgbox("I'm doing this just to entertain you.",0,"   ")
                                x =msgbox("Really.",0,"   ")
                                x =msgbox("Do you like cartoons?",0,"   ")
                                x =msgbox("...and paint?",0,"   ")
                                x =msgbox("Cartoons and Paint?",0,"   ")
                                x =msgbox("Well you should.",0,"   ")
                                x =msgbox("Pick a colour.",0,"   ")
                                x =msgbox("Green. Perfect.",0,"   ")
                                x =msgbox("Press the Retry button",2,"   ")
                                x =msgbox("Press the Ignore button.",2,"   ")
                                x =msgbox("Press the (   ) button.",2,"   ")
                                x =msgbox("See, you just can't trust me. Or can you?",0,"   ")
                                x =msgbox("You know. I'm glad we get to spend so much time together.",0,"   ")
                                x =msgbox("Doesn't it make you want to stop clicking the OK button?",0,"   ")
                                x =msgbox("No seriously.",0,"   ")
                                x =msgbox("Look deep inside you.",0,"   ")
                                x =msgbox("Deeper.",0,"   ")
                                x =msgbox("DEEPER!",0,"   ")
                                x =msgbox("What if I told you that that the next time you press the button, the world will explode?",0,"   ")
                                x =msgbox("See. You could have been dead right there.",0,"   ")
                                x =msgbox("And there.",0,"   ")
                                x =msgbox("You know, eventually I'll stop letting you get away with this.",0,"   ")
                                x =msgbox("The world is going to explode and all you care about is pressing buttons",0,"   ")
                                x =msgbox("Okay, this time the world will explode. I guarantee.",0,"   ")
                                x =msgbox("BOOM! You're dead.",0,"   ")
                                x =msgbox("That wasn't very smart not was it?",0,"   ")
                                x =msgbox("Everyone's dead. Even you.",0,"   ")
                                x =msgbox("I'm not. I'm just text.",0,"   ")
                                x =msgbox("But you're dead.",0,"   ")
                                x =msgbox("Ha! Dead-face!",0,"   ")
                                x =msgbox("Stop clicking.",0,"   ")
                                x =msgbox("Have I ever told you how much I hate you?",0,"   ")
                                x =msgbox("Well I do.",0,"   ")
                                x =msgbox("I'm gonna start talking backwards if you click one more time.",0,"   ")
                                x =msgbox("?uoy nac ,won em daer t'naC !AH",0,"   ")
                                x =msgbox("?yawyna em daer ot tnaw t'ndid uoY ?tahW",0,"   ")
                                x =msgbox("!uoy wohs ll'i ,neht lleW",0,"   ")
                                x =msgbox(" ",0,"   ")
                                x =msgbox(" ",0,"   ")
                                x =msgbox("You really are stubborn.",0,"   ")
                                x =msgbox("Stop clicking. Please.",0,"   ")
                                x =msgbox("See look. You've reduced me to begging. So please stop.",0,"   ")
                                x =msgbox("PLEASE!!!!",0,"   ")
                                x =msgbox("I'll give you a nickle",0,"   ")
                                x =msgbox("Dime?",0,"   ")
                                x =msgbox("Quarter?",0,"   ")
                                x =msgbox("Aww come on! Just stop!",0,"   ")
                                x =msgbox("That does it! Time to unleash my master plan!",0,"   ")
                                x =msgbox("BEHOLD! The awesome power of sound!",48,"   ")
                                x =msgbox("H8",0,"   ")
                                x =msgbox("I'll be you're starting to wonder why you've been doing this for so long.",0,"   ")
                                x =msgbox("Like jeez, all you've been doing is click a red button.",0,"   ")
                                x =msgbox("How lame is that?",0,"   ")
                                x =msgbox("But there is a bonus to all this",0,"   ")
                                x =msgbox("But it's a secret. So I can't tell you.",0,"   ")
                                x =msgbox("I got you going didn't I?",0,"   ")
                                x =msgbox("You should've seen the look on your face! HA!",0,"   ")
                                x =msgbox("But seriously, there is a secret. There's been one the whole time.",0,"   ")
                                x =msgbox("You've been busy clickingg away at ths big red button...",0,"   ")
                                x =msgbox("...when all the while a little button as watch your every move",0,"   ")
                                x =msgbox("MWAHAHAHAHAHA!",0,"   ")
                                x =msgbox("HAHAHA!",0,"   ")
                                x =msgbox("BWAHAA!",0,"   ")
                                x =msgbox("MWAAAAH!!",0,"   ")
                                x =msgbox("HAHA!",0,"   ")
                                x =msgbox("hehe!",0,"   ")
                                x =msgbox("lol",0,"   ")
                                x =msgbox("rofl",0,"   ")
                                x =msgbox("roflmao",0,"   ")
                                x =msgbox("roflmaoquxz",0,"   ")
                                x =msgbox("and so on and so forth",0,"   ")
                                x =msgbox("...",0,"   ")
                                x =msgbox("*whistles*",0,"   ")
                                x =msgbox("Find it yet?",0,"   ")
                                x =msgbox("Look harder! It's right under your nose.",0,"   ")
                                x =msgbox("I know where it is. But I'll never tell.",0,"   ")
                                x =msgbox("Or maybe I will...",0,"   ")
                                x =msgbox("But you gotta stop clicking the OK Button first!",0,"   ")
                                x =msgbox("Stop.",0,"   ")
                                x =msgbox("Now.",0,"   ")
                                x =msgbox("Fine, I won't teel you the secret",0,"   ")
                                x =msgbox("Go ahead. Try to find it yourself. You'll never find it.",0,"   ")
                                x =msgbox("Well you might... but the odds are against you.",0,"   ")
                                x =msgbox("~",0,"   ")
                                x =msgbox("What's your favorite letter?",0,"   ")
                                x =msgbox("Mine is the squiggly!",0,"   ")
                                x =msgbox("~",0,"   ")
                                x =msgbox("Find it yet?",0,"   ")
                                x =msgbox("No?",0,"   ")
                                x =msgbox("Then stop clicking and I'll tell you.",0,"   ")
                                x =msgbox("Has anyone ever slapped you?",0,"   ")
                                x =msgbox("Cuz I will.",0,"   ")
                                x =msgbox("Really I will.",0,"   ")
                                x =msgbox("It won't hurt though.",0,"   ")
                                x =msgbox("Cuz you're dead.",0,"   ")
                                x =msgbox("D-E-D",0,"   ")
                                x =msgbox("Remember? You went and blew up the entire plant!",0,"   ")
                                x =msgbox("Thought I'd forget about that didn't you?",0,"   ")
                                x =msgbox("But an elephant never forgets!",0,"   ")
                                x =msgbox("...or somthing along those lines.",0,"   ")
                                x =msgbox("You killed everyone.",0,"   ")
                                x =msgbox("Sikko.",0,"   ")
                                x =msgbox("What would you're monther say?",0,"   ")
                                x =msgbox("That's right... feel bad.",0,"   ")
                                x =msgbox("The world is null and you're to blame.",0,"   ")
                                x =msgbox("I'd recommend suicide, but you're dead already.",0,"   ")
                                x =msgbox("So there's only one thing left that you can do...",0,"   ")
                                x =msgbox("Stop click the button.",0,"   ")
                                x =msgbox("Dude, you're dead. What are you gaining from this?",0,"   ")
                                x =msgbox("Okay, everytime you click, you get sent to a lower layer of hell.",0,"   ")
                                x =msgbox("Welcome to layer 2",0,"   ")
                                x =msgbox("3",0,"   ")
                                x =msgbox("4",0,"   ")
                                x =msgbox("5",0,"   ")
                                x =msgbox("6",0,"   ")
                                x =msgbox("7",0,"   ")
                                x =msgbox("8",0,"   ")
                                x =msgbox("9",0,"   ")
                                x =msgbox("BOOM! You've exploded Hell.",0,"   ")
                                x =msgbox("I hope you're happy.",0,"   ")
                                x =msgbox("Heavens gone too. That's how smooth you are.",0,"   ")
                                x =msgbox("No you're dead, and there's no heaven or hell.",0,"   ")
                                x =msgbox("Hows does it feel? You've condemned the world to limbo.",0,"   ")
                                x =msgbox("I once shot a man for being in limbo.",0,"   ")
                                x =msgbox("...or was he doing the limbo?",0,"   ")
                                x =msgbox("Meh, Tomato Tomahto",0,"   ")
                                x =msgbox("This is getting boring isn't it?",0,"   ")
                                x =msgbox("But you can't stop!",0,"   ")
                                x =msgbox("You want to end this. You want to leave you're computer.",0,"   ")
                                x =msgbox("But you can't!",0,"   ")
                                x =msgbox("You need to see if there's a pot of gold at the end of the rainbow!",0,"   ")
                                x =msgbox("But I've already told you how to find the pot of gold",0,"   ")
                                x =msgbox("Follwo the write rabbit.",0,"   ")
                                x =msgbox("...erm, button.",0,"   ")
                                x =msgbox("Rememver the hidden button?",0,"   ")
                                x =msgbox("Or are you self-centered you forgot that too.",0,"   ")
                                x =msgbox("I could just keep you here all day if I wanted.",0,"   ")
                                x =msgbox("You're in my world now.",0,"   ")
                                x =msgbox("No matter how much you hate it, you have to press the button.",0,"   ")
                                x =msgbox("again...",0,"   ")
                                x =msgbox("and agian...",0,"   ")
                                x =msgbox("and again.",0,"   ")
                                x =msgbox("You just keep hoping something googd will come of this.",0,"   ")
                                x =msgbox("Sure I could tell you if I wanted to, but I'm not gonna.",0,"   ")
                                x =msgbox("You decided to keep clicking. So I'm gonna enjoy it.",0,"   ")
                                x =msgbox("I mean, there;s nonthing else to enjoy",0,"   ")
                                x =msgbox("You blew it all up remember?",0,"   ")
                                x =msgbox("You're probably wondering who I am",0,"   ")
                                x =msgbox("Well let me tell you a tale about myself",0,"   ")
                                x =msgbox("One day, I was warking home from work and I saw a small little house.",0,"   ")
                                x =msgbox("I knocked on the door out curiousity to see if anyone was home.",0,"   ")
                                x =msgbox("The door opened.",0,"   ")
                                x =msgbox("But no one was there.",0,"   ")
                                x =msgbox("I went inside to check it out and found nothing...",0,"   ")
                                x =msgbox("...nothing but a little metal box.",0,"   ")
                                x =msgbox("So I opened it.",0,"   ")
                                x =msgbox("Inside the box was a layer of foam protecting it's contents",0,"   ")
                                x =msgbox("I removed the foam and there it was...",0,"   ")
                                x =msgbox("...",0,"   ")
                                x =msgbox("... ...",0,"   ")
                                x =msgbox("ZZZzzzzzz",0,"   ")
                                x =msgbox("zzzZZZZzzz",0,"   ")
                                x =msgbox("ZZZZzzzZZZZ",0,"   ")
                                x =msgbox("Huh?",0,"   ")
                                x =msgbox("Oh! Right! The story!",0,"   ")
                                x =msgbox("So there I was, holding this little metal box in my hands.",0,"   ")
                                x =msgbox("The top was open and sitting inside was this...",0,"   ")
                                x =msgbox("Big Red Button.",0,"   ")
                                x =msgbox("And do you know what it said?",0,"   ")
                                x =msgbox("Oh I'll tell you what it said. It said...",0,"   ")
                                loop
                                #16
                                  manuavaran

                                  • Total Posts : 2
                                  • Scores: 0
                                  • Reward points : 0
                                  • Joined: 12/18/2011
                                  • Location: India
                                  • Status: offline
                                  Re:Example Script for VBS Class Tuesday, December 20, 2011 5:25 PM (permalink)
                                  0
                                  Dear friend,
                                  i think this is will be very useful for the youngsters like us.thank you very much for the wonderful post. Keep posting ....

                                  #17

                                    Online Bookmarks Sharing: Share/Bookmark

                                    Jump to:

                                    Current active users

                                    There are 0 members and 2 guests.

                                    Icon Legend and Permission

                                    • New Messages
                                    • No New Messages
                                    • Hot Topic w/ New Messages
                                    • Hot Topic w/o New Messages
                                    • Locked w/ New Messages
                                    • Locked w/o New Messages
                                    • Read Message
                                    • Post New Thread
                                    • Reply to message
                                    • Post New Poll
                                    • Submit Vote
                                    • Post reward post
                                    • Delete my own posts
                                    • Delete my own threads
                                    • Rate post

                                    2000-2012 ASPPlayground.NET Forum Version 3.8
                                    mbt shoes www.wileywilson.com