Page 1 of 1 1
Topic Options
#85391 - 2002-03-25 07:26 PM VBS Translation Please!!!
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I have been converting some VBS scripts to KIX/KixGUI without much difficulty until I tried this one. It seems as though it may not be possible to simply convert the syntax; some portions may need to be rewritten entirely. I am not fluent with VBscript which makes this all the more challenging and my reason for converting; so that I can modify to my liking. First place to start:

Problem 1:
(Line 25 of the original)
I have never seen this ".InstansesOf"? Will this work in Kix? How does it work?
$NICs = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

Problem 2:
I assume this some sort of multidimensional array?
Can it be 'emulated' in kix?

ReDim arrNICs(3,0)

This is the original script:
code:
  

'~~Author~~. Sergei Zelenov

'~~Email_Address~~. sezel@metric.ru

'~~Script_Type~~. vbscript

'~~Sub_Type~~. SystemAdministration

'~~Keywords~~. WMI, TCP/IP, laptop

'~~Comment~~.
'This script is a considerably extended version of the rather primitive Add
'IP script, inspired by a message from someone who tried to use the latter
'for switching between two IP configurations on his laptop. So, this is
'what the script does - switching between two (or more) IPs on the local
'machine, also changing default gateways, DNS servers and WINS servers. It
'will also work on multihomed computers.

'~~Script~~.
'This script will let you switch fast between different IPs on your Win2000-operated PC
'It will also run on any Windows9x or WinNT 4.0 machine with WMI CORE 1.5 installed
'Written by Sergei Zelenov sezel@metric.ru

Set oFso = CreateObject("Scripting.FileSystemObject")
Set NICs = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
'get all network interfaces
ReDim arrNICs(3,0)
n = 0
For Each NIC In NICs 'store NICs' indexes and properties in an array
If NIC.IPEnabled Then
ReDim Preserve arrNICs(3,n)
arrNICs(0,n) = NIC.Index
arrNICs(1,n) = NIC.Description
arrNICs(2,n) = NIC.MACAddress
arrNICs(3,n) = NIC.IPAddress(0)
n = n + 1
End If
Next

If UBound(arrNICs,2) = 0 Then 'if the second dimension has only one value,
If arrNICs(0,0) = "" Then 'this may mean that there's either no or just one adapter
MsgBox "This PC has no IP-enabled adapters to configure!",16,"Switch IPs"
Else
SwitchIPs(arrNICs(0,0))
End If
Else
ChooseNIC(arrNICs) 'if there are multiple values, let's choose which NIC we want to configure
End If

Sub ChooseNIC(arrNICs)
blnValid = False 'initialize a variable that won't let the user to enter incorrect NIC index
strMsg = "The following adapters were detected on your computer: " & String(2,10)
For i = 0 To Ubound(arrNICs,2) 'form a message to be displayed in the input box
strMsg = strMsg & "Index: " & arrNics(0,i) & Chr(10) & _
"Name: " & arrNICs(1,i) & Chr(10) & "MAC Address: " & arrNICs(2,i) & _
Chr(10) & "Primary IP: " & arrNICs(3,i) & String(2,10)
Next
intNICIdx = CLng(InputBox(strMsg & "Please specify the index of the adapter" &_
Chr(10) & "you wish to use:", "Switch IPs"))
For j = 0 To Ubound(arrNICs,2) 'after we got an index from user input, let's check whether it is valid
If arrNICs(0,j) = intNICIdx Then
blnValid = True
Exit For
End If
Next
If blnValid = True Then 'if we have a valid NIC index, then we proceed
SwitchIPs(intNICIdx)
Else
ChooseNIC(arrNICs) 'if not, display the input box again
End If
End Sub

Sub SwitchIPs(idx)
'obtain the appropriate instance of the Win32_NetworkAdapterConfiguration class:
Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & idx)
If Adapter.DHCPEnabled Then 'check whether the IP settings are obtained from a DHCP server
intDynIgn = MsgBox("The adapter you selected gets its IP configuration" & Chr(10) & _
"from a DHCP server. Are you sure you wish to" & Chr(10) & _
"configure this adapter manually?", 36, "Switch IPs")
If intDynIgn = 7 Then
Exit Sub
End If
End If
strNewIp = Trim(InputBox("Specify IP address:","Add IP"))
If strNewIp = "" Then
Exit Sub
End If
arrIp = Adapter.Ipaddress 'obtain an array, containing all the currently configured IPs
For Each ip in arrIp
If ip = strNewIp Then
MsgBox "This address already exists in the TCP/IP configuration of the " & _
"specified adapter!" & Chr(10) & "There is no need to switch to it.",48,"Switch IPs"
Exit Sub
End If
Next
'obtain current path to look for settings file:
strCurPath = oFso.GetParentFolderName(WScript.ScriptFullName)
strFSetName = strCurPath & "\" & Replace(strNewIp,Chr(46),"") & ".cfg"
If oFso.FileExists(strFSetName) Then
'if file exists, get settings from file:
arrSettings = ParseSetFile(strNewIp,strFSetName,Adapter)
Else
'no settings file; prompt user for settings:
arrSettings = GetInputFromUser(strNewIp,strFSetName,Adapter,0)
End If
'in case user pressed cancel when prompted for subnet mask or default gateway:
If Not IsArray(arrSettings) Then
MsgBox "Operation aborted!",16,"Switch IPs"
Exit Sub
End If
'OK, start configuring the adapter:
On Error Resume Next
intRes = Adapter.EnableStatic(Array(strNewIp),Array(arrSettings(0))) 'assign the IP address
If intRes = 0 Then
strLMsg = strLMsg & "IP address changed successfully!" & String(2,10)
Else
strLMsg = strLMsg & "Failed to change IP address." & Chr(10) &_
"Error:" & Err.Number & Chr(10) & "Description:" & Err.Description & String(2,10)
End If
intGate = Adapter.SetGateways(Array(arrSettings(1)),Array(1)) 'assign default gateway
If intGate = 0 Then
strLMsg = strLMsg & "Default gateway changed successfully!" & String(2,10)
Else
strLMsg = strLMsg & "Failed to change gateway configuration." & Chr(10) & _
"Error:" & Err.Number & Chr(10) & "Description:" & Err.Description & String(2,10)
End If
If Not arrSettings(2) = "none" Then 'check if user chose to re-configure DNS settings
ReDim arrDNS(0)
arrDNS(0) = arrSettings(2) 'form an array of DNS servers
If arrSettings(3) <> "" Then
ReDim Preserve arrDNS(1)
arrDNS(1) = arrSettings(3)
End If
'the EnableDNS method is executed against the class itself, not any of its instances,
'so, bind the class:
Set oClass = GetObject("winmgmts:Win32_NetworkAdapterConfiguration")
'execute method:
intDNS = oClass.EnableDNS(oClass.DNSHostName,oClass.DNSDomain,arrDNS,oClass.DNSDomainSuffixSearchOrder)
If intDNS = 0 Then
strLMsg = strLMsg & "DNS servers lookup order changed successfully!" & String(2,10)
Else
strLMsg = strLMsg & "Failed to change DNS servers lookup order." & Chr(10) & _
"Error:" & Err.Number & Chr(10) & "Description:" & Err.Description & String(2,10)
End If
End If
If Not arrSettings(4) = "none" Then 'check if user chose to re-configure WINS servers
intWINS = Adapter.SetWINSServer(arrSettings(4),arrSettings(5))
If intWINS = 0 Then
strLMsg = strLMsg & "WINS server value set successfully!" & String(2,10)
Else
strLMsg = strLMsg & "Failed to set the WINS server value." & Chr(10) & _
"Error:" & Err.Number & Chr(10) & "Description:" & Err.Description & String(2,10)
End If
End If
MsgBox strLMsg,64,"Switch IPs" 'display final message
End Sub

'this function obtains configuration settings from user input
'the 'Skip' parameter is used to check whether welcome message is displayed
'(required when settings file exists but user decides not to use it)
Function GetInputFromUser(strNewIp,strFSetName,Adapter,Skip)
Dim arrInput(5) 'initialize an array that will store the settings
Set oFSet = oFso.OpenTextFile(strFSetName,2,True) 'open new settings file for writing
If Skip = 0 Then
MsgBox "It is the first time you switch to IP-address " & strNewIp & "," & Chr(10) & _
"so you'll be prompted for all the necessary information." & Chr(10) & _
"The settings will be written to a text file, so that next time" & Chr(10) & _
"you specify this adress you will have the option not to specify" & Chr(10) & _
"all the settings manually.", 64, "SwitchIPs"
End If
arrInput(0) = Trim(InputBox("Specify subnet mask:","SwitchIPs",Adapter.IPSubnet(0)))
If arrInput(0) = "" Then 'the first two elements are obligatory, so if they are not
oFSet.Close 'specified, or 'Cancel' is pressed then exit
oFso.DeleteFile(strFSetName)
Exit Function
End If
oFSet.WriteLine "Subnet=" & arrInput(0)
arrInput(1) = Trim(InputBox("Specify default gateway","Switch IPs"))
If arrInput(1) = "" Then
oFSet.Close
oFso.DeleteFile(strFSetName)
Exit Function
End If
oFSet.WriteLine "Gateway=" & arrInput(1)
'check whether user wants to re-configure DNS
intDecDNS = MsgBox("Re-configure DNS?",36,"Switch IPs")
If intDecDNS = 6 Then
arrInput(2) = Trim(InputBox("Specify preferred DNS server","Switch IPs"))
oFSet.WriteLine "PDNS=" & arrInput(2)
arrInput(3) = Trim(InputBox("Specify alternate DNS server","Switch IPs"))
oFSet.WriteLine "ADNS=" & arrInput(3)
Else
arrInput(2) = "none"
oFSet.WriteLine "PDNS=none"
arrInput(3) = "none"
oFSet.WriteLine "ADNS=none"
End If
'check whether user wants to re-configure WINS
intDecWINS = MsgBox("Re-configure WINS?",36,"Switch IPs")
If intDecWINS = 6 Then
arrInput(4) = Trim(InputBox("Specify primary WINS server","Switch IPs"))
oFSet.WriteLine "WINS1=" & arrInput(4)
arrInput(5) = Trim(InputBox("Specify secondary WINS server","Switch IPs"))
oFSet.WriteLine "WINS2=" & arrInput(5)
Else
arrInput(4) = "none"
oFSet.WriteLine "WINS1=none"
arrInput(5) = "none"
oFSet.WriteLine "WINS2=none"
End If
oFSet.Close 'close the settings file
GetInputFromUser = arrInput
End Function

'This function obtains configuration settings from a previosly created text file
Function ParseSetFile(strNewIp,strFSetName,Adapter)
'Ask if user actually wants to use the previously stored settings:
intUseEx = MsgBox("A configuration file with settings for IP-address " & strNewIp & Chr(10) & _
"was found on your harddrive. Do you want to use these settings?" & String(2,10) & _
"If you click 'No' you will be prompted for all the settings again," & Chr(10) & _
"and the settings stored will be lost even if you decide to cancel.", 36, "SwitchIPs")
If intUseEx = 7 Then
'Answer is 'no'; get settings from user input:
ParseSetFile = GetInputFromUser(strNewIp,strFSetName,Adapter,1)
Else
Dim arrRawSet(5) 'initialize an array to store the settings
Set oSetFile = oFso.OpenTextFile(strFSetName,1,False)
'read the first 6 lines of the file and store settings in the array
For i = 0 To 5
retstring = oSetFile.ReadLine
arrNamVal = Split(retstring,"=",2,1)
arrRawSet(i) = arrNamVal(1)
Next
ParseSetFile = arrRawSet
End If
End Function

_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#85392 - 2002-03-25 07:46 PM Re: VBS Translation Please!!!
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
the line you posted.

$NICs = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

works correct with Kix.

This is bit of code is using the WMI COM interface to work with the network cards on a computer.

code:
$NICs = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

for each $nic in $nics
?"-------------------------------------------"
? $nic.index
? $nic.description
? $nic.macaddress
? $nic.ipaddress(0)
next

Bryce

Top
#85393 - 2002-03-26 01:34 PM Re: VBS Translation Please!!!
Alex.H Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 406
Loc: France
jtokach,
As Bryce said, and to be a more explicit, InstancesOf is a method of the GetObject("winmgmts:") object. As this is a COM object, it usual that used syntax is not from kix, but if there's no errors, Kix know how to use it

Also, for the ReDim arrNICs(3,0), if the latest beta version of KiXtart doesn't alreay have it, Ruud has planned to support multidimentionnal arrays. Only a matter of time
_________________________
? getobject(Kixtart.org.Signature)

Top
#85394 - 2002-03-29 07:43 PM Re: VBS Translation Please!!!
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Very helpful!

Thank you both! [Smile]
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#85395 - 2002-04-01 12:27 AM Re: VBS Translation Please!!!
Anonymous
Unregistered


Kixtart can simulate multi dimension arrays. See the following UDF's:

MArray()

Array()

Syntax would be $arrNICs[3][0]

Top
#85396 - 2002-04-02 10:27 PM Re: VBS Translation Please!!!
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Excellent! Thank you!
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2175 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.057 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org