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

Trying to script something but have forgotten the basics

Status
Not open for further replies.

tt300zrcr

IS-IT--Management
Sep 21, 2010
6
US
I have this file generated by WMI output reading the network configuration

Description: :Intel(R) 82577LM Gigabit Network Connection #2:
Physical (MAC) address:

IP address: 1.x.x.x
Subnet: 255.255.255.0
Subnet: 64
Default gateway: 1.x.x.1

DNS
1.x.x.12
1.x.x.13
1..x.x.14
1.x.x.15
DNS domain: x.com

WINS
Primary WINS server: 1.x.x.50
Secondary WINS server: 1.x.x.53


what i am trying to attain is I need to read the file and then out put in a new file with each entry on a new line that i can feed into my WMI updates.
Description(actual value)
MAC Address(actual value)
IP address(actual Value)
Subnet "
Gateway "

i have taken general approach below which copies my whole file over.
Const ForReading = 1
Const ForWriting = 2
inFile = InputBox ("Enter File to read", "Source File")
outFile = InputBox ("Enter File to write:", "Destination File")
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objRead = objFSO.OpenTextFile(inFile, ForReading)
Set objWrite = objFSO.CreateTextFile (outFile, ForWriting)
Do While Not objRead.AtEndOfStream
strLine = objRead.ReadLine
if strLine <> "" then
objWrite.WriteLine strLine
end if
Loop
objRead.Close
objWrite.Close
Set objRead = nothing
Set objWrite = nothing
 
you generated the file in the original format from WMI, just update the file which generates the original file so that it outputs it into the format you want.

dont generate a file using WMI, only to read it back in to then write it back out in a different format,,,only to feed into 'my WMI updates'
 
the issue is i want a file to output hte current settings.

then i have to update the ips for dns wins and ipv4 based on the adpater and mac address.

in unix i would just grep and replace simple and easy
 
there is no need to output to a file the current settings, you are only going to need that information again, read back into memory to make your decision?

'please excuse my use of .Writeline with obvious xml, i was forced to write with TS due to other members of the team

'need to run off and get the nic information
Dim colAdapters, objAdapter, strTemp, dicNoDuplicates, strPNPID, strDeviceId, strSubId, objNIC, strNetConnectionStatus
Set dicNoDuplicates = CreateObject("Scripting.Dictionary")
Set colAdapters = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter")
For Each objAdapter In colAdapters
strTemp = ""
If Not IsNull(objAdapter.AdapterType) Then
If CStr(objAdapter.AdapterType) = "Ethernet 802.3" Then
If Not IsNull(objAdapter.MACAddress) Then
strTemp = UCase(objAdapter.MACAddress)
End If
'manipulate objAdapter.PNPDeviceID
strPNPID = "": strDeviceId = "": strSubId = "": strNetConnectionStatus = ""
If Not IsNull(objAdapter.PNPDeviceID) Then
strPNPID = UCase(objAdapter.PNPDeviceID)
If Left(strPNPID, 8) = "PCI\VEN_" Then
strPNPID = Right(strPNPID, Len(strPNPID) - 8)
strDeviceId = Left(strPNPID, 4)
strDeviceId = strDeviceId & Mid(strPNPID, 10, 4)
strSubId = Mid(strPNPID, 22, 8)
End If
End If
'netconnectionstatus?
If Not IsNull(objAdapter.NetConnectionStatus) Then
strNetConnectionStatus = CStr(objAdapter.NetConnectionStatus)
End If
If strTemp <> "" Then
strTemp = Replace(strTemp, ":", "-")
If Not dicNoDuplicates.Exists(strTemp) Then
Set objNIC = New clsNIC
objNIC.MACAddress = strTemp
objNIC.SubId = strSubId
objNIC.DeviceId = strDeviceId
objNIC.NetConnectionStatus = strNetConnectionStatus
'description?
If Not IsNull(objAdapter.Description) Then
objNIC.Description = objAdapter.Description
End If
dicNoDuplicates.Add strTemp, objNIC
Set objNIC = Nothing
End If
End If
End If
End If
Next
Set colAdapters = Nothing

Dim strDNSServers, i
'now lets update the nics in the dicNoDuplicates with the ipstack information
Set colAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter In colAdapters
'MsgBox objAdapter.MACAddress
If dicNoDuplicates.Exists(Replace(CStr(objAdapter.MACAddress), ":", "-")) Then

Set objNIC = dicNoDuplicates.Item(Replace(CStr(objAdapter.MACAddress), ":", "-"))
'ipaddress
If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)
objNIC.IP = objAdapter.IPAddress(i)
Next
End If
'subnet mask
If Not IsNull(objAdapter.IPSubnet) Then
For i = 0 To UBound(objAdapter.IPSubnet)
'WScript.Echo objAdapter.IPSubnet(i) & " ===="
objNIC.SubNetMask = objAdapter.IPSubnet(i)
Exit For
Next
End If
'default gateway
If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = 0 To UBound(objAdapter.DefaultIPGateway)
objNIC.GateWay = objAdapter.DefaultIPGateway(i)
Next
End If
'dnsservers, they call it the DNSSSO, nice
strDNSServers = ""
If Not IsNull(objAdapter.DNSServerSearchOrder) Then
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
strDNSServers = strDNSServers & "," & objAdapter.DNSServerSearchOrder(i)
Next
End If
If Left(strDNSServers, 1) = "," Then
strDNSServers = Right(strDNSServers, Len(strDNSServers) - 1)
End If
objNIC.DNS = strDNSServers
objNIC.PrimaryWINS = objAdapter.WINSPrimaryServer
objNIC.SecondaryWINS = objAdapter.WINSSecondaryServer
Set objNIC = Nothing
End If
Next
Set colAdapters = Nothing


For Each objAdapter In dicNoDuplicates
Set objNIC = dicNoDuplicates.Item(objAdapter)
objTS.WriteLine vbTab & vbTab & vbTab & "<nic mac=" & Chr(34) & objAdapter & Chr(34) & " disable=" & Chr(34) & "false" & Chr(34) & " description=" & Chr(34) & objNIC.Description & Chr(34) & ">"

'information which is of interest
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<pe_ipaddress>" & objNIC.IP & "</pe_ipaddress>"
'information we might want to keep
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<dns>" & objNIC.DNS & "</dns>"
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<primarywins>" & objNIC.PrimaryWINS & "</primarywins>"
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<secondarywins>" & objNIC.SecondaryWINS & "</secondarywins>"
'information which is of interest
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<gateway>" & objNIC.Gateway & "</gateway>"
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<subnetmask>" & objNIC.SubNetMask & "</subnetmask>"
'other information which might be of interest
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<deviceid>" & objNIC.DeviceId & "</deviceid>"
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<subid>" & objNIC.SubId & "</subid>"
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<description>" & objNIC.Description & "</description>"
objTS.WriteLine vbTab & vbTab & vbTab & vbTab & "<netconnectionstatus>" & objNIC.NetConnectionStatus & "</netconnectionstatus>"
Set objNIC = Nothing
objTS.WriteLine vbTab & vbTab & vbTab & "</nic>"
Next

objTS.WriteLine vbTab & vbTab & "</nics>"


Class clsNIC

Public IP
Public MACAddress
Public SubNetMask
Public PrimaryWINS
Public SecondaryWINS
Public DNS
Public Gateway
Public DeviceId
Public SubId
Public NetConnectionStatus
Public Description

End Class
 
I think i got easier way to do this.

anyways i am going to use netsh create a dump file

to get this

output

# ----------------------------------
# Interface IP Configuration
# ----------------------------------
pushd interface ip


# Interface IP Configuration for "Team (Name)"

set address name="Team (Name)" source=static addr=x.x.x.22 mask=255.255.255.192
add address name="Team (Name)" addr=x.x.x.23 mask=255.255.255.192
add address name="Team (Name)" addr=x.x.x.24 mask=255.255.255.192
add address name="Team (Name)" addr=x.x.x.25 mask=255.255.255.192
set address name="Team (Name)" gateway=x.x.x.7 gwmetric=0
set dns name="Team (Name)" source=static addr=x.x.x.50 register=PRIMARY
add dns name="Team (Name)" addr=x.x.x.51 index=2
set wins name="Team (Name)" source=static addr=x.x.x.22
add wins name="Team (Name)" addr=x.x.x.52 index=2
add wins name="Team (Name)" addr=x.x.x.53 index=3

# Interface IP Configuration for "Slot 2 Port 1 (Name)"

set address name="Slot 2 Port 1 (Name)" source=static addr=x.x.x.185 mask=255.255.240.0
set dns name="Slot 2 Port 1 (Name)" source=static addr=none register=NONE
set wins name="Slot 2 Port 1 (Name)" source=static addr=none


popd
# End of interface IP configuration


but my issue is how do i scrub the x.x.x and the mask i am not a windows person i am familar with grep and it would be easier.

what i want to do is read the ip and subnet from a file tabe delimited and on 1-1 basis replace the ip and subnet in the dump file

then just run a netsh -f filename and be done with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top