Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Call .vbs from a .vbs

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2354
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Call .vbs from a .vbs
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Call .vbs from a .vbs - 3/15/2005 4:32:37 PM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
Please tell me if I have an error in syntax

set net = CreateObject("WScript.Network")

set shell = CreateObject("WScript.Shell")

workstation=net.computername

location=left(workstation,4)

select case location

case "mypc"

shell.run("\\2k3svr\netlogon\share.vbs")
shell.run("\\2k3svr\netlogon\printer.vbs")

case "mypc01"

shell.run("\\2k3svr\netlogon\share01.vbs")
shell.run("\\2k3svr\netlogon\printer01.vbs")

End Select


I suspect that I have an error in case select syntax

Here's the contents of share.vbs and printer.vbs

share.vbs

set shell = WScript.CreateObject("WScript.Shell")
strDesktop = shell.SpecialFolders("Desktop")
set oShellLink = shell.CreateShortcut(strDesktop & "\Share.lnk")
oShellLink.TargetPath = "\\2k3svr\Netlogon"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "SHELL32.dll, 18"
oShellLink.Description = "Share"
oShellLink.WorkingDirectory = "\\2k3svr\Netlogon"
oShellLink.Save


printer.vbs

set net = wscript.CreateObject("WScript.Network")

printer = "\\2k3svr\AD-Printer"

printername="Printer for all users"

net.AddWindowsPrinterConnection printer

net.SetDefaultPrinter(printername)

Please help. Thanks
 
 
Post #: 1
 
 Re: Call .vbs from a .vbs - 3/15/2005 5:37:05 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
The conditional statement, case "mypc01", will never execute because it is 6 characters in length, and you specifically limits the location variable to be 4 characters in length.

If the script didn't execute the way intended, you might want to tell us exactly what the error message was and at what line.

(in reply to azmantek)
 
 
Post #: 2
 
 Re: Call .vbs from a .vbs - 3/15/2005 5:51:08 PM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
Thanks Token,

mypc01 is just a case that I put there and I understand it'll never reach that part.

My computername is "mypc" and the script doesn't run either.

If I set location="mypc" it runs.

It didn't give me any error msg at all.

(in reply to azmantek)
 
 
Post #: 3
 
 Re: Call .vbs from a .vbs - 3/16/2005 12:13:19 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
2 things,

1) select case is case sensitive, so change this workstation=net.computername to this workstation=lcase(net.computername)

2) You might want to add a wait on return to the calls for the other scripts. also, you do not need () around the run unless you were looking for a return.
so, this shell.run("\\2k3svr\netlogon\share.vbs") becomes this shell.run "\\2k3svr\netlogon\share.vbs",1,TRUE

(in reply to azmantek)
 
 
Post #: 4
 
 Re: Call .vbs from a .vbs - 3/16/2005 1:11:38 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
Hi mbouchard,
Thanks alot. It's working now. The keyword is "lcase" :). When users log on, it runs well but it pops up two DOS windows. How do I prevent it from popping up like that? Thanks

(in reply to azmantek)
 
 
Post #: 5
 
 Re: Call .vbs from a .vbs - 3/16/2005 4:04:49 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Why does your script need to call the other 2 scripts? Why not just put the code into the first script?

(in reply to azmantek)
 
 
Post #: 6
 
 Re: Call .vbs from a .vbs - 3/16/2005 5:35:13 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
I agreed with mbouchard. Unless you have a VERY good reason for having them in separate files, you are better off having them in a single file and replace the run with a subroutine or function. If you do that, you don't have to worry about the popup DOS window.

(in reply to azmantek)
 
 
Post #: 7
 
 Re: Call .vbs from a .vbs - 3/16/2005 9:31:42 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
The reasons that I have to separate them into smaller scripts because we have more than 100 computer labs :). I think it's easier to break down and keep track. We have naming convention here. Building#-Room#-Computer#. That's why I use the left function to cut the computer # and map printer, share drive base on bldg#-room#. So there's no way that I can prevent the DOS window from popping up?
Thanks

(in reply to azmantek)
 
 
Post #: 8
 
 Re: Call .vbs from a .vbs - 3/16/2005 9:41:57 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
hm... I don't understand why your naming convention has anything to do with the way your scripts are broken up. Generally speaking, unless the the scripts are divided among different people where different people are resopnsible for different parts of the code, the script itself should reside in a single file instead of across multiple files.

I have the feeling that you have the wrong reasons for breaking it into different files. Again, I would suggest you to put them into a single file.

However, if you insist (which I believe you will change your mind when the complexity grows in the future =) ), you can do this:

shell.run "\\2k3svr\netlogon\share.vbs",0,TRUE

(in reply to azmantek)
 
 
Post #: 9
 
 Re: Call .vbs from a .vbs - 3/16/2005 10:05:15 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
Thanks token,
I have read your post regarding routine and function. And you're 100% right on that. I can pass printer as variable to the function so it can map the printer. I'm still new to VBScript. Please help me with
forming the function

This is my code for mapping the printer


set net = wscript.CreateObject("WScript.Network")

printer = "\\2k3svr\AD-Printer"

net.AddWindowsPrinterConnection printer

net.SetDefaultPrinter printer


so "\\2k3svr\AD-Printer" is the variable.

And this is my code for creating a shorcut that mapped to a network share

set shell = WScript.CreateObject("WScript.Shell")
strDesktop = shell.SpecialFolders("Desktop")
set oShellLink = shell.CreateShortcut(strDesktop & "\Share.lnk")
oShellLink.TargetPath = "\\2k3svr\Netlogon"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "SHELL32.dll, 18"
oShellLink.Description = "Share"
oShellLink.WorkingDirectory = "\\2k3svr\Netlogon"
oShellLink.Save


\\2k3svr\Netlogon is the variable.

Please help me by putting them into two functions: one for mapping printer and one for the shortcut.

And also, I would like to know how you use variable for the whole script. Thank you very much.

(in reply to azmantek)
 
 
Post #: 10
 
 Re: Call .vbs from a .vbs - 3/16/2005 10:16:51 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
To use the mapPrinter subroutine, you can do the following.

call mapPrinter("\\2k3svr\AD-Printer")

======================================================================
sub mapPrinter(printer)
set net = wscript.CreateObject("WScript.Network")
net.AddWindowsPrinterConnection printer
net.SetDefaultPrinter printer
end sub
======================================================================


To use the mapShare subroutine, you could do the following:

call mapShare("\\2k3svr\Netlogon")

======================================================================
sub mapShare(share)
set shell = WScript.CreateObject("WScript.Shell")
strDesktop = shell.SpecialFolders("Desktop")
set oShellLink = shell.CreateShortcut(strDesktop & "\Share.lnk")
oShellLink.TargetPath = share
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "SHELL32.dll, 18"
oShellLink.Description = "Share"
oShellLink.WorkingDirectory = share
oShellLink.Save
end sub

(in reply to azmantek)
 
 
Post #: 11
 
 Re: Call .vbs from a .vbs - 3/16/2005 11:44:04 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
Hi token,

Here's my code :). It works perfectly. Thanks alot

set net = CreateObject("WScript.Network")
set shell = WScript.CreateObject("WScript.Shell")

workstation=net.computername

location = left(workstation, 4)

select case location

case "Lab1"

call mapPrinter("\\2k3svr\AD-Printer")
call mapShare("\\2k3svr\Netlogon")


End Select


sub mapPrinter(var)

net.AddWindowsPrinterConnection var
net.SetDefaultPrinter var

end sub


sub mapShare(var)

strDesktop = shell.SpecialFolders("Desktop")
set oShellLink = shell.CreateShortcut(strDesktop & "\Share.lnk")
oShellLink.TargetPath = var
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "SHELL32.dll, 18"
oShellLink.Description = "Share"
oShellLink.WorkingDirectory = var
oShellLink.Save

end sub


So the printer mapping is cool now. But I have some rooms with 2 printers and the instructor wants side A ( computer 1 - 10 ) print to printer A and side B ( computer 11 - 20 ) print to printer B

Suggestion?

Thank you very much

(in reply to azmantek)
 
 
Post #: 12
 
 Re: Call .vbs from a .vbs - 3/16/2005 1:36:42 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
You are welcome =)

If these 20 computers follow some naming conventions, you can then use IF statement to determine which printer to map. Suppose comp01 to comp10 on Side A and comp11 to comp20 on Side B, you could do something listed below:

if cint(right(workstation,2)) <= 10 then
mapPrinter("\\2k3svr\Side_A_Printer")
else
mapPrinter("\\2k3svr\Side_B_Printer")
end if

(in reply to azmantek)
 
 
Post #: 13
 
 Re: Call .vbs from a .vbs - 3/19/2005 8:26:09 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
hi token,
This is the sub that creates a shortcut on the desktop to map to a share.

sub mapShare(var)

strDesktop = shell.SpecialFolders("Desktop")
set oShellLink = shell.CreateShortcut(strDesktop & "\Share.lnk")
oShellLink.TargetPath = var
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "SHELL32.dll, 18"
oShellLink.Description = "Share"
oShellLink.WorkingDirectory = var
oShellLink.Save

end sub


The share UNC always has \\domain\data as a prefix. For example room A will have UNC as \\domain\data\roomA

I want to use roomA as the variable. I've been trying with " & var & to separate the variable and the string but it doesn't work. My problem is i don't know how to separte "" that is required and "" of the text string and the variable. What should I do? Thanks

(in reply to azmantek)
 
 
Post #: 14
 
 Re: Call .vbs from a .vbs - 3/19/2005 8:45:33 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
I'm not exactly sure if this is what you wanted. But if you wanted to map the share based on the room number inside the "mapShare" subroutine, you can do the following.

Suppose "\\domain\data" is the variable passed into the subroutine,

eg: call mapShare("\\domain\data")

then you can have the following in the subroutine:

if room = "roomA" then
var = var & "\roomA"
else
var = var & "\roomB"
end if

If this is not what you wanted, you have to give me an exact example on what you had trouble with.

(in reply to azmantek)
 
 
Post #: 15
 
 Re: Call .vbs from a .vbs - 3/19/2005 8:54:47 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
hi token,
I would like to use the room # as the variable. So mapshare(RoomA)


var = "\\domain\data" & var

So I want var to be "\\domain\data\roomA"

I have problem concatenating between the text "" and the variable.

(in reply to azmantek)
 
 
Post #: 16
 
 Re: Call .vbs from a .vbs - 3/19/2005 8:57:46 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Is "roomA" in a variable name or a literal string, if it is a string, you will need to quote it, eg: mapshare("RoomA")

then

var = "\\domain\data\" & var

(in reply to azmantek)
 
 
Post #: 17
 
 Re: Call .vbs from a .vbs - 3/19/2005 9:05:24 AM   
  azmantek

 

Posts: 14
Score: 0
Joined: 3/15/2005
From:
Status: offline
hi token

if I use mapshare("roomA")

var = "\\domain\data\" & var

then var becomes "\\domain\data\roomA" right?

I need to have " " in the syntax to work.

Here's the original code


set shell = WScript.CreateObject("WScript.Shell")
strDesktop = shell.SpecialFolders("Desktop")
set oShellLink = shell.CreateShortcut(strDesktop & "\Share.lnk")
oShellLink.TargetPath = "\\2k3svr\Netlogon"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "SHELL32.dll, 18"
oShellLink.Description = "Share"
oShellLink.WorkingDirectory = "\\2k3svr\Netlogon"
oShellLink.Save


That's why I need " " :)

Thanks

(in reply to azmantek)
 
 
Post #: 18
 
 Re: Call .vbs from a .vbs - 3/19/2005 10:02:32 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Correct, if you do that, var will contain the string: \\domain\data\roomA

I don't believe that you need to use double quotes to enclose the value in either the TargetPath or WorkingDirectory methods though. However, if the path contains space, you can do the following:

var = """\\domain\data\" & var & """"

(in reply to azmantek)
 
 
Post #: 19
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Call .vbs from a .vbs Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts