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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return multiple values from function.

Status
Not open for further replies.

Keyster86

IS-IT--Management
Jul 23, 2008
50
US
OK, I have three seperate functions that I would like to combine into one. However, I need to find out the most effective way of using a Function to output three different string vaules.

For example, I have a function to find a MAC address, IP address and Dell Serial Number. How can these be outputed seperately?


Here is code exurpts that contain the three different functions that I want to merge together. Please help.
Code:
FUNCTION FindMAC(strComputer)
	' Continue with script if error occures.
	ON ERROR RESUME NEXT
	
	' When strComputer does not equal blank (null), then strInput is true.
	' Personnally, I have not figured this line of code out yet; therefore,
	' I do not understand its purpose. It causes no harm; therefore, it stays.
	IF strComputer <> "" THEN
		strInput = TRUE
	END IF

	SET objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	IF objWMIservice IS NOTHING THEN
		WRITE_CSV_LINE strComputer & ",Error: " & err.number & " Unable to bind to WMI on " & strComputer & " - DESCRIPTION NOT CHANGED - FINDMAC"
		EXIT FUNCTION
	ELSE
		SET colItems = objWMIService.ExecQuery _
			("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

		' Search for MAC address. Once MAC is found, it is stored into the "Function" as a value.
		FOR EACH objItem IN colItems
			FindMAC = objItem.MACAddress
		NEXT
	END IF
	CleanScriptFromMemory
END FUNCTION 

FUNCTION FindIP(strComputer)
	' Continue with script if error occures.
	ON ERROR RESUME NEXT
	SET objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	IF objWMIservice IS NOTHING THEN
		WRITE_CSV_LINE strComputer & ",Error: " & err.number & " Unable to bind to WMI on " & strComputer & " - DESCRIPTION NOT CHANGED - FINDIP"
		EXIT FUNCTION
	ELSE
		SET colNics = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")
	 
		FOR EACH objNic IN colNics
	    		SET colNicConfigs = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & objNic.DeviceID & "'}" & _
	      		" WHERE AssocClass=Win32_NetworkAdapterSetting")
			FOR EACH objNicConfig IN colNicConfigs
		        	FOR EACH strIPAddress IN objNicConfig.IPAddress
		            		IF LEN(strIPAddress) =< 15 THEN
						FindIP = strIPAddress
					END IF
					
	    	    		NEXT
	    		NEXT
		NEXT
	END IF 
	CleanScriptFromMemory
END FUNCTION 

FUNCTION FindSTN(strComputer)
	ON ERROR RESUME NEXT
	SET objWMIservice = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	IF objWMIservice IS NOTHING THEN
		WRITE_CSV_LINE strComputer & ",Error: " & err.number & " Unable to bind to WMI on " & strComputer & " - DESCRIPTION NOT CHANGED - FINDSTN"
		EXIT FUNCTION
	ELSE
		SET colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
		FOR EACH objitem IN colitems
		      FindSTN =  objitem.serialnumber
		NEXT
	END IF
	CleanScriptFromMemory
END FUNCTION

Something that comes to mind is merging the strings together with ';' delimters as seperators...then using the Split function to seperate them again after out of the function. Is there a more effective way?

V/r,

SPC Key
United States Army
 
I found a work around...

Code:
FUNCTION FindInfo(strComputer)
	' Continue with script if error occures.
	ON ERROR RESUME NEXT
	
	SET objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	IF objWMIservice IS NOTHING THEN
		'WRITE_CSV_LINE strComputer & ",Error: " & err.number & " Unable to bind to WMI on " & strComputer & " - DESCRIPTION NOT CHANGED - FINDMAC"
		wscript.echo "error"
		EXIT FUNCTION
	ELSE
		
		' ----------------------------------------
		' Find MAC
		' ----------------------------------------
		SET colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

		' Search for MAC address. Once MAC is found, it is stored into the "Function" as a value.
		FOR EACH objItem IN colItems
			FindInfo = objItem.MACAddress
		NEXT

		' ----------------------------------------
		' Find IP
		' ----------------------------------------
		SET colNics = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")
	 
		FOR EACH objNic IN colNics
	    		SET colNicConfigs = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & objNic.DeviceID & "'}" & _
	      		" WHERE AssocClass=Win32_NetworkAdapterSetting")
			FOR EACH objNicConfig IN colNicConfigs
		        	FOR EACH strIPAddress IN objNicConfig.IPAddress
		            		IF LEN(strIPAddress) =< 15 THEN
						FindInfo = FindInfo & ";" & strIPAddress
					END IF
					
	    	    		NEXT
	    		NEXT
		NEXT

		' ----------------------------------------
		' Find SN
		' ----------------------------------------
		SET colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)
		FOR EACH objitem IN colitems
		      FindInfo =  FindInfo & ";" & objitem.serialnumber
		NEXT
	END IF
	
	CleanScriptFromMemory
END FUNCTION 


strSplitInfo = Split(FindInfo ("."), ";")

wscript.echo strSplitInfo(0)
wscript.echo strSplitInfo(1)
wscript.echo strSplitInfo(2)

V/r,

SPC Key
United States Army
 
You could also define a class to contain the separate bits, write out the different bits to the members and return the an object of the class.

Another alternative is not to use a function but to use a sub and return them ByRef.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top