Hi,

I would like to know how to call a vbs from another vbs, the calling vbs
will pass the argument to the called vbs, the called vbs will then return a
value back to the calling vbs.

Can I see some samples?


Thanks
Frank

Re: call vbs from within vbs by Al

Al
Sun Jul 24 11:55:55 CDT 2005


"stdfrank" <stdfrank@gmail.com> wrote in message
news:%23XoKbKGkFHA.3300@TK2MSFTNGP15.phx.gbl...
> Hi,
>
> I would like to know how to call a vbs from another vbs, the calling vbs
> will pass the argument to the called vbs, the called vbs will then return
a
> value back to the calling vbs.
>
> Can I see some samples?

There have been a couple of similar threads lately, look for one entitled
"how do I include/reference functions in another file" for starters:

http://tinyurl.com/danlp

The short answer is that vbscript code cannot "call" a file as a function,
it can only call a function (or sub) that has been defined.

Of course, one vbscript file can certainly execute another, passing it
command-line parameters. That other script runs in a different process, and
returns only a numeric errorlevel code. You could, of course, code it to
place more detailed results into a file which your "calling" script will
subsequently open and read.

The approach that suits your situation the best depends on a number of
factors you have not yet provided, so it will be counterproductive to get
more detailed until we know a bit more of your problem.

/Al




Re: call vbs from within vbs by Std

Std
Tue Jul 26 21:18:07 CDT 2005

Thank you Dunbar,

We actually want to transform the following function to be a standalone vbs
so that we can have other vbs just to call it to test the group membership.
Otherwise, I have to include this function in all the logon scripts.

Looks like not easy to achieve.

Thanks
Frank



' this is how current we call th function within the vbs.

If IsMember(objUser, "HR USERS") Then
map a drive here,
End If


'===========================================================
' Function to test for group membership, including nested group.
' objGroupList is a dictionary object with global scope.
'===========================================================
Function IsMember(objADobject, strGroup)
If IsEmpty(objGroupList) Then
Set objGroupList = Createobject("Scripting.Dictionary")
End If
If Not objGroupList.Exists(objADobject.sAMAccountName & "\") Then
Call LoadGroups(objADobject, objADobject)
objGroupList(objADobject.sAMAccountName & "\") = True
End If
IsMember = objGroupList.Exists(objADobject.sAMAccountName & "\" _
& strGroup)
End Function

Sub LoadGroups(objPriobject, objADSubobject)
' Recursive subroutine to populate dictionary object objGroupList.
Dim colstrGroups, objGroup, j
objGroupList.CompareMode = vbTextCompare
colstrGroups = objADSubobject.memberOf
If IsEmpty(colstrGroups) Then
Exit Sub
End If
If TypeName(colstrGroups) = "String" Then
Set objGroup = Getobject("LDAP://" & colstrGroups)
If Not objGroupList.Exists(objPriobject.sAMAccountName & "\" _
& objGroup.sAMAccountName) Then
objGroupList(objPriobject.sAMAccountName & "\" _
& objGroup.sAMAccountName) = True
Call LoadGroups(objPriobject, objGroup)
End If
Set objGroup = Nothing
Exit Sub
End If
For j = 0 To UBound(colstrGroups)
Set objGroup = Getobject("LDAP://" & colstrGroups(j))
If Not objGroupList.Exists(objPriobject.sAMAccountName & "\" _
& objGroup.sAMAccountName) Then
objGroupList(objPriobject.sAMAccountName & "\" _
& objGroup.sAMAccountName) = True
Call LoadGroups(objPriobject, objGroup)
End If
Next
Set objGroup = Nothing
End Sub



Re: call vbs from within vbs by Al

Al
Tue Jul 26 21:59:44 CDT 2005

Contrariwise, that is precisely what our logon scripts do, and same for a
number of related administrative scripts.

Our scripts are in .wsf format. There could be any number of .wsf script
files each with its own code embedded in <script> tags. Shared routines such
as yours below would be stored at a common location in a .vbs file, and
included statically in each of the .wsf scripts with a line such as:

<script language="VBScript" src="..\libfile.vbs">

The "library files" would contain only subs and functions. The main program
in the .wsf would not "call" these files, but simply "call" the functions
and subs defined in them in exactly the same manner as if all the code were
in the same file, like in your example below.

/Al

"Std Frank" <stdfrank@gmail.com> wrote in message
news:eGhAwFlkFHA.3336@tk2msftngp13.phx.gbl...
> Thank you Dunbar,
>
> We actually want to transform the following function to be a standalone
vbs
> so that we can have other vbs just to call it to test the group
membership.
> Otherwise, I have to include this function in all the logon scripts.
>
> Looks like not easy to achieve.
>
> Thanks
> Frank
>
>
>
> ' this is how current we call th function within the vbs.
>
> If IsMember(objUser, "HR USERS") Then
> map a drive here,
> End If
>
>
> '===========================================================
> ' Function to test for group membership, including nested group.
> ' objGroupList is a dictionary object with global scope.
> '===========================================================
> Function IsMember(objADobject, strGroup)
> If IsEmpty(objGroupList) Then
> Set objGroupList = Createobject("Scripting.Dictionary")
> End If
> If Not objGroupList.Exists(objADobject.sAMAccountName & "\") Then
> Call LoadGroups(objADobject, objADobject)
> objGroupList(objADobject.sAMAccountName & "\") = True
> End If
> IsMember = objGroupList.Exists(objADobject.sAMAccountName & "\" _
> & strGroup)
> End Function
>
> Sub LoadGroups(objPriobject, objADSubobject)
> ' Recursive subroutine to populate dictionary object objGroupList.
> Dim colstrGroups, objGroup, j
> objGroupList.CompareMode = vbTextCompare
> colstrGroups = objADSubobject.memberOf
> If IsEmpty(colstrGroups) Then
> Exit Sub
> End If
> If TypeName(colstrGroups) = "String" Then
> Set objGroup = Getobject("LDAP://" & colstrGroups)
> If Not objGroupList.Exists(objPriobject.sAMAccountName & "\" _
> & objGroup.sAMAccountName) Then
> objGroupList(objPriobject.sAMAccountName & "\" _
> & objGroup.sAMAccountName) = True
> Call LoadGroups(objPriobject, objGroup)
> End If
> Set objGroup = Nothing
> Exit Sub
> End If
> For j = 0 To UBound(colstrGroups)
> Set objGroup = Getobject("LDAP://" & colstrGroups(j))
> If Not objGroupList.Exists(objPriobject.sAMAccountName & "\" _
> & objGroup.sAMAccountName) Then
> objGroupList(objPriobject.sAMAccountName & "\" _
> & objGroup.sAMAccountName) = True
> Call LoadGroups(objPriobject, objGroup)
> End If
> Next
> Set objGroup = Nothing
> End Sub
>
>



Re: call vbs from within vbs by Csaba

Csaba
Wed Jul 27 04:14:16 CDT 2005

An idea to call another .vbs program and pick up its output appears in
Post 8 of a thread started March 27, 2003 titled:
Can .Exec be called hidden
http://groups-beta.google.com/group/microsoft.public.scripting.vbscript/msg/b6119a027f408a9c
This method uses volatile environment variables to return the output of
a foreign program without flicker

There are additional thoughts from Michael Harris on the matter from an
April 24, 2005 post titled:
Command line input to CScript/WScript?
http://groups-beta.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/b2004813fb2e6eba/


A second idea is to take the file with the code you want to execute,
read it in, and pass its contents through a
CreateObject("MSScriptControl.ScriptControl")
A very primitive example follows, but it shows that it is possible to
pass even objects. The problem in how I have set it up is that it is
not generic enough and each use must be customized. As shown, a fixed
number of arguments must be passed (an array of them would be better),
and their types are assumed. Objects (passed in with .AddObject), in
particular, are a problem for passing in as both of the execution forms
.Eval (returns a value) and .ExecuteStatement take strings so they must
be passed in ahead of time. Nevertheless, this would seem to be a
simple and viable approach in many situations.


Subroutine.vbs:
Function AddToDictionary (key, value)
'arg1 is a dictionary object, defined in caller.vbs
arg1.add key, value
AddToDictionary = true
End function


Caller.vbs
' --- Test section ---
Dim oDict
Set oDict = CreateObject("Scripting.Dictionary")
callForeignFunc3 "AddToDictionary", "subroutine.vbs", _
oDict, "item1", "value1"
'Next line proves the subroutine executed successfully
MsgBox oDict.item("item1")
' --- End Test section ---

Function callForeignFunc3(functionName, fileItLivesIn, _
arg1, arg2, arg3)
Dim fileContent, oScript
callForeignFunc3 = null 'Default return value
fileContent = slurpFile (fileItLivesIn)
If fileContent="" Then Exit Function
Set oScript=CreateObject("MSScriptControl.ScriptControl")
oScript.Language = "VBScript"
oScript.AddCode fileContent

'just to show objects can be passed
oScript.AddObject "arg1", arg1
'Next line assumes arg2 and arg3 are strings,
'but does not properly escape them for double quotes, vbCrLF, etc.
callForeignFunc3 = oScript.Eval _
(functionName & "(""" & arg2 & """,""" & arg3 & """)")
End Function


Function slurpFile(fileName)
'returns the contents of the path fileName as a string
Dim FSO, oFile
const ForReading=1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(fileName, ForReading)
slurpFile = oFile.ReadAll
oFile.Close
End Function


Csaba Gabor from Vienna