Login script to detect subnet and run script
I need a script to detect the subnets. Then if in these subnets 10.x.1.x to 10.x.254.x place in this group and run this script. I need about 4 diff groups for diff subnents that we have. I have seen the article from the scripting guys that contains this code. I am dealing with 80 subnets or so.
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
NextSet colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'H:'")If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.1"
objNetwork.MapNetworkDrive "H:", "\\arizona-fs-01\public"
Case "192.168.2"
objNetwork.MapNetworkDrive "H:", "\\newmexico-fs-01\public"
Case "192.168.3"
objNetwork.MapNetworkDrive "H:", "\\texas-fs-01\public"
End Select
End If
Answers
Yes, you can use strSubnet in the Case statement, but you should probably not mix with arrOctets(2). I just suggested that you might be able to simplify. I also wanted to demonstrate that you can combine cases by separating with commas. I would suggest something similar to:
Select Case strSubnet
Case "10.5.1", "10.5.2", "10.5.13"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare1"
Case "10.5.4", "10.5.12", "10.5.26", "10.5.101"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare2"
Case "10.5.32"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare3"
Case "10.5.44", "10.5.24", "10.5.121", "10.5.122", "10.5.134", "10.5.141", "10.5.142", "10.5.144"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare4"
Case "192.168.1"
objNetwork.MapNetworkDrive "H:", "\\arizona-fs-01\public"
End SelectRichard Mueller
MVP ADSI- Marked As Answer bytaz081175 Sunday, November 08, 2009 2:47 AM
All Replies
- The example you post is a good one. It looks to me like you can detect the subnet from just the third octet of the IP address. Maybe you could use code similar to below:
========
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
Select Case arrOctets(2)
Case "1", "2", "13"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
Case "4", "12", "26", "101"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
Case "32"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare3"
Case "44", 24", "121", "122", "134", "141", "142", "144"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare4"
End Select
========
Of course the case statements depend on your subnets, and what you want to do for each. In the example above I map a drive, but you can insert any code desired, even many lines. You can test this script by running it as a normal user at a command prompt after logon to make sure there are no syntax errors and you have sufficient permissions. Does this help?Richard Mueller
MVP ADSI Should you have a Active Directory enviroment then you can do this a lot simpler. By looking at your urls I presume that the servers are at different sites. This is probably also specified in the AD Sites and Services so that each site has it's own AD controller(s) for authentication and such. By using this information you can create a GPO and link that GPO to a specific site. That GPO will then only be processed/run when a users is at the specific location.
This way you let AD do the location processing and you get better control of what is processed in what location. This way you can also define proxy settings per site for instance as a lot of other usefull site specific issues.- I think that will work. I will test it out and report back. Thank you for your help.
- Can I add the original line to use the "Select Case strSubnet" for thos ip's where the third octect may be the same, apparently i have some old 192.x.x.x.
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
Select Case arrOctets(2)
Case "1", "2", "13"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
Case "4", "12", "26", "101"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
Case "32"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare3"
Case "44", 24", "121", "122", "134", "141", "142", "144"
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare4"
End Select
Select Case strSubnet
Case "192.168.1"
objNetwork.MapNetworkDrive "H:", "\\arizona-fs-01\public"
End Select Yes, you can use strSubnet in the Case statement, but you should probably not mix with arrOctets(2). I just suggested that you might be able to simplify. I also wanted to demonstrate that you can combine cases by separating with commas. I would suggest something similar to:
Select Case strSubnet
Case "10.5.1", "10.5.2", "10.5.13"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare1"
Case "10.5.4", "10.5.12", "10.5.26", "10.5.101"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare2"
Case "10.5.32"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare3"
Case "10.5.44", "10.5.24", "10.5.121", "10.5.122", "10.5.134", "10.5.141", "10.5.142", "10.5.144"
objNetwork.MapNetworkDrive "H:", "\\MyServer\MyShare4"
Case "192.168.1"
objNetwork.MapNetworkDrive "H:", "\\arizona-fs-01\public"
End SelectRichard Mueller
MVP ADSI- Marked As Answer bytaz081175 Sunday, November 08, 2009 2:47 AM
- I think if you can tie these scripts to sites (like what VincentK was suggesting) the scenario is much simpler.
Also, I would be sure of the definition of "subnet" (it usually means "network ID"). Whether a host is a part of a particular network depends on the network mask ("subnet mask"). The correct way to do this is to calculate the network ID by ORing the octets of the host's IP address with the corresponding octets of the network mask. Here's an example VBScript function:
Function NetworkID(Address, Mask) Dim AddressOctets, MaskOctets, Result, N AddressOctets = Split(Address, ".") MaskOctets = Split(Mask, ".") ReDim Result(UBound(AddressOctets)) For N = 0 To UBound(AddressOctets) Result(N) = AddressOctets(N) And MaskOctets(N) Next NetworkID = Join(Result, ".") End Function
For example:
WScript.Echo NetworkID("192.168.32.15", "255.255.255.0") ' 192.168.32.0 WScript.Echo NetworkID("192.168.65.76", "255.255.252.0") ' 192.168.64.0 WScript.Echo NetworkID("10.152.32.100", "255.240.0.0") ' 10.144.0.0
HTH,
Bill - Is it possible to have the script call to a txt file that contained all the ip's?
if in x.txt then
if in y.txt then and so on? just trying to keep the script clean and manageable.
I would also like to do the same for server netbios names if possible, but that could be in a separate post. Thanks for the help guys.

