Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A few questions for the VBScript gurus...

Status
Not open for further replies.

Ludacris

Technical User
Jun 19, 2004
9
US
Hello All,

I am new to VBScript and was hoping for some pointers. Basically, I created a DOS batch file at work that allows us to install several software packages in order after we tell it which drive to launch the packages from. The DOS batch file works like a dream, however, I would like to use VBScript to take this to the next level. I have figured out how to do certain things like display system specs and stuff like that, but some of the basic concepts are giving me problems. Here are the questions I have:

1. After setting up an Input Box that has the buttons OK and Cancel, how can I program the script to exit the program if the button Cancel is pressed? As it stands right now, regardless if the OK or Cancel button is pressed, the script will carry on to the next line of code.

2. In the DOS batch file, I was easily able to set up labels and tell DOS if a certain condition was met, goto label, If condition wasn't met goto this label. Basically, I was easily able to direct my script to jump to different parts of my code just by using the goto xxxx. Can I do this in VBScript? Right now, it seems like the Select Case and Do Loops just doesn't fit my needs but that is probably because I don't know how to use them properly for what I am trying to do.

3. I am currently using NotePad to develop my scripts and it is ok for the most part. However, if I run the program and receive an error in line 77, I have to count all the way down just to find out the line. Please tell me there is a better way! Right now I have decided to break my code down into sections and run one part at a time in seperate scripts just to see if it works.

4. Is there a way that I can display colored text in a message box? The goal is to have the system specs displayed and if any part of the system specs doesn't meet my software requirements, then I want it to display additional text (that is red) pointing this out.

Any help or advise would be appreciated. Thanks in advance!
 
1) Something like this ?
reply = InputBox(prompt, title, default)
If reply = "" Then WScript.Quit ' Cancel pressed
You may also take a look at the WshShell.Popup method in case the script must be designed to work in unattended mode.
2) No GoTo instruction in VBS except for On Error GoTo 0.
You have to play with the conditional statements (If...Then...Else, Select Case) and the looping statements (Do...Loop, While...Wend, For...Next, For Each...Next).
You may also consider breaking your code in multiple procedures (Sub...End Sub) called only when conditions are meet.
3) Take a look here:
You may also do a keyword search in this forum for editor.
4) WSH is not well designed for GUI stuff.
Do a keyword search in this forum for hta.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Totally agree with PHV.
Just an addition to 2):

Instead of using GOTOs, you might consider using SUBs or Functions, which you call at the specific point. If you need to keep certain variable values, you can pass them as parametre.

[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
andreas.galambos@bowneglobal.de
HP:
 
I'd suggest you get a copy of PrimalScript from Sapien.com for doing your vbScript writing.

You can however "go to" a line quickly in Notepad. Hit <ctrl>+g to be prompted for a line ot go to.

Here is an example of checking the buttons that were pressed as PHV has suggested. You could use vbOkCancel instead of vbYesNo.

Code:
'Reboot Workstation Script
'By Mark MacLachlan
'Creation Date 9/18/2002
'Modification 10/7/2002 Added support for confirmation box
'Usage- Double click and enter a machine name or IP address to reboot machine


On Error Resume Next
mname = InputBox("Enter Machine Name", "Reboot Machine")
If Len(mname) = 0 Then Wscript.Quit

if Msgbox("Are you sure you want to reboot machine " & mname, vbYesNo, "Reboot Machine") = vbYes then

		Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//" & mname).ExecQuery("select * from Win32_OperatingSystem where Primary=true")
		for each OpSys in OpSysSet
			OpSys.Reboot()
 		next
end if

If you want to display colored text, then you might want to consider having your script be in ASP so that you could use HTML to make the text whatever color you want. ASP is a little scary at first if you are not used to it. I am finally getting to the point where I can get most stuff to work but still have a lot to learn. What I like best with ASP solutions is you can set th credentials ont he web site to be elevated and restrict access to the web site at the file level. This lets me give "administrative" authority to non administrators. For example, I have made a page that adds sites to an ISA server destination set. You typically need to log on at the server console to do that, but thanks to my script that is not necessary.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
MakeItSo,for point 2)
exactly how i can modify my script,in stead of "goto"?
i write an example.
-----------------------------------------------------
test1 = "10.16.244."
test2 = "10.16.245."
test3 = "10.16.246."
test4 = "10.16.247."

Set WshNetwork = WScript.CreateObject("WScript.Network")
set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
for each IPConfig in IPConfigSet
dim n
if (left(IPConfig.IPAddress(i),10) = test1) or (left(IPConfig.IPAddress(i),10) = test2) then
n = clng(mid(IPConfig.IPAddress(i),11))
if (n > 1) AND (n < 245) then
'*********group printer1*******
WshNetwork.AddWindowsPrinterConnection "\\Server1\PRINTER032"
WshNetwork.AddWindowsPrinterConnection "\\Server1\PRINTER033"
'*********group printer3*******
WshNetwork.RemovePrinterConnection "\\Server3\PRINTER014", true, true
WshNetwork.RemovePrinterConnection "\\Server3\PRINTER015", true, true
'******************************
end if
elseif (left(IPConfig.IPAddress(i),10) = test3) or (left(IPConfig.IPAddress(i),10) = test4) then
n = clng(mid(IPConfig.IPAddress(i),11))
if (n > 1) AND (n < 245) then
'*********group printer2*******
WshNetwork.AddWindowsPrinterConnection "\\Server2\PRINTER014"
WshNetwork.AddWindowsPrinterConnection "\\Server2\PRINTER015"
'*********group printer3*******
WshNetwork.RemovePrinterConnection "\\Server3\PRINTER014", true, true
WshNetwork.RemovePrinterConnection "\\Server3\PRINTER015", true, true
'******************************
....
..
.


etc...etc...
i must repeat printer group each time.
if i have goto,i call group printer 1 or 2 or 3 or 4 etc. and finish script,but now in this way if i make a change in a printer group i must change every times where group appears and not only 1 times,...

how can i walk around this problem,exactly?
 
Thanks guys! This thread has been very helpful and I have successfully been able to get most of my batch file converted over to VBScript. One last question:

Is it possible you guys can demonstrate how to use a subroutine?

Just some small code revolving around using wscript.echo statements or something. Thanks in advance!

 
name = "Ludacris"

shoutout(name)

sub shoutout(name)

msgbox ("Hi " & name)

end sub

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hi TheAll,
a) your determination of n is always the same:
n = clng(mid(IPConfig.IPAddress(i),11))
(Perhaps you could read it from the right:
n= clng(right(IpConfig.IPAddress(i),3)

b) Your position of Else If:
If...End If..Else If
Does that work? :-?

c) Better to use Select Case:
Code:
Select Case True
Case (left(IPConfig.IPAddress(i),10) = test1) or _
     (left(IPConfig.IPAddress(i),10) = test2)

     GroupPrinter 1,3,"032","033","014","015"

Case (left(IPConfig.IPAddress(i),10) = test3) or _
     (left(IPConfig.IPAddress(i),10) = test4)

     GroupPrinter 2,3,"014","015","014","015"

...
End Select

...

Sub GroupPrinter(srvr1, srvr2, adprt1, adprt2, remprt1, remprt2)

WshNetwork.AddWindowsPrinterConnection "\\Server" & srvr1 & "\PRINTER" & adprt1
WshNetwork.AddWindowsPrinterConnection "\\Server" & srvr1 & "\PRINTER" & adprt2
WshNetwork.RemovePrinterConnection "\\Server" & srvr2 & "\PRINTER" & remprt1, true, true
WshNetwork.RemovePrinterConnection "\\Server" & srvr2 & "\PRINTER" & remprt2, true, true
End Sub

Get the point? ;-)
Hope that helps,
Andy


[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
andreas.galambos@bowneglobal.de
HP:
 
a)you right, i change to 10 or 11 but at the end is better,right read.

b)it work perfectly :) (obviously is only a part of vbscript and printer group :) )

c)thanks for idea, now i work with select.
 
Good stuff guys! I am now efficiently using select case and subroutines in my script. You guys have been a big help!!!!

I now use the (Little known) Microsoft Script Editor v7 to develop my code in and it works like a dream. I had no idea it came with Office XP and Office 2003.

Anyways, thanks again for all your help and I will be back soon for more advice!
 
Yeah, nice tool. You can get to it from Office directly or execute the MSE7.exe file in your office directory to open it without having to go into word first.

Nathan aka: zaz (zaznet)
zaz@zaz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top