Script to detect ip read against text file and display output
I have setup 3 wscript echo in this VBS. the third one does not work at all. I have verified the text file reads correctly and is setup x.x.x carriage return x.x.x carrage return etc...
Any help would be appreciated
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next'wscript.echo strSubnet
Const ForReading = 1Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "strSubnet"Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ip.txt", ForReading)Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
'Wscript.Echo strSearchString
If colMatches.Count = 1 Then
For Each strMatch in colMatches
Wscript.Echo strSearchString
Next
End If
LoopobjFile.Close
Answers
- This line is passing a literal string, rather than the contents of the variable ...
objRegEx.Pattern = "strSubnet"
Take the double quotes off ...
objRegEx.Pattern = strSubnet
The rest cannot be evaluated without knowledge of what is in the file, "C:\ip.txt".
Tom Lavedas- Edited byTom Lavedas Sunday, November 08, 2009 7:56 PMfix trivial typo
- Marked As Answer bytaz081175 Sunday, November 08, 2009 8:58 PM
All Replies
- This line is passing a literal string, rather than the contents of the variable ...
objRegEx.Pattern = "strSubnet"
Take the double quotes off ...
objRegEx.Pattern = strSubnet
The rest cannot be evaluated without knowledge of what is in the file, "C:\ip.txt".
Tom Lavedas- Edited byTom Lavedas Sunday, November 08, 2009 7:56 PMfix trivial typo
- Marked As Answer bytaz081175 Sunday, November 08, 2009 8:58 PM
Thank you. I fell better now. I thought it was the code I was using and banging my head against the wall because of it. Works perfectly now. i did modify another part of it to collect info for multiple network adapters and if the value of the first IP octet is o then in goed to the next network adpter that has an ip.
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) > "0" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next
'wscript.echo strSubnet
Const ForReading = 1Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strSubnetSet objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\ip.txt", ForReading)Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString)
'Wscript.Echo strSearchString
'Wscript.Echo colMatches.Count
If colMatches.Count = 1 Then
For Each strMatch in colMatches
Wscript.Echo strSearchString
Next
End If
LoopobjFile.Close

