Gracious666
Programmer
Hi,
I wonder if anyone can help on the script below. It works fine on XP and deploys printers by the subnet your PC is in.
However in Windows 7, I get the following Windows Script Host Error
Line: 82
Char 10
Error: Type mismatch: 'addressbytes(...)'
Code: 800A000D
Source: Microsoft VBScript runtime error
Dim strSubnet, strComputer, WshNetwork
Dim strPrinter1, strPrinter2, strPrinter3, strPrinter4
Dim objWMIService
Set WshNetwork = WScript.CreateObject("WScript.Network")
strPrinter1 = "\\BEDA\BTH04P21"
strPrinter2 = "\\BEDA\BTH05P01"
strPrinter3 = "\\BEDA\BTH06P01"
strPrinter4 = "\\BEDA\BTH07P01"
On Error Resume Next
WshNetwork.RemovePrinterConnection _
strPrinter1, true, true
WshNetwork.RemovePrinterConnection _
strPrinter2, true, true
WshNetwork.RemovePrinterConnection _
strPrinter3, true, true
WshNetwork.RemovePrinterConnection _
strPrinter4, true, true
On Error GoTo 0
strComputer = WshNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer _
& "\root\cimv2")
strSubnet = GetSubnet(strComputer)
'Assign printers to laptop users
Select Case strSubnet
Case "172.16.72.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter1
Case "172.16.92.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter2
Case "172.16.100.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter3
Case "172.16.76.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter4
End Select
wscript.Quit
Function GetSubnet(sHost)
' Dim arrNetworkAddress, prefix
arrNetworkAddress = GetIpConf(sHost)
Dim ActiveIPAddress, ActiveSubnetMask
ActiveIPAddress = arrNetworkAddress(0)
ActiveSubnetMask = arrNetworkAddress(1)
Dim addressbytes(4), addressbyte
Dim subnetmaskbytes(4), subnetmaskbyte
Dim subnet, i, period, prevperiod
i=0 : period = 1
while period<>len( ActiveIPAddress ) + 2
prevperiod=period
period = instr( period+1, ActiveIPAddress, "." ) + 1
if period = 1 then period = len( ActiveIPAddress ) + 2
addressbyte = mid( ActiveIPAddress, prevperiod, _
period-prevperiod-1 )
addressbytes(i)=addressbyte
i=i+1
wend
i=0 : period = 1
while period<>len( ActiveSubnetMask ) + 2
prevperiod=period
period = instr( period+1, ActiveSubnetMask, "." ) + 1
if period = 1 then period = len( ActiveSubnetMask ) + 2
subnetmaskbyte = mid( ActiveSubnetMask, prevperiod, _
period-prevperiod-1 )
subnetmaskbytes(i)=subnetmaskbyte
i=i+1
wend
subnet=""
for i=0 to 3
subnet = subnet & (addressbytes(i) _
AND subnetmaskbytes(i)) & "."
next
GetSubnet = left( subnet, len(subnet)-1 )
prefix = PrefixLength(ActiveSubnetMask)
GetSubnet = GetSubnet & "/" & prefix
End Function
Function GetIpConf(sHost)
' objWMIService has global scope
If not IsObject(objWMIService) Then
Dim objWMIService
Set objWMIService = GetObject("winmgmts:{impersonationLevel=" _
& "impersonate}!\\" & sHost & "\root\cimv2")
End If
DIM colPings, objPing, sProtocolAddress, colNicConfigs
DIM objNicConfig, i, sIPAddress, IsMatch, sSubnetmask
DIM objNic, strIPAddress, arrsubnetmask
'DETERMINE THE *ACTIVE* IP ADDRESS
Set colPings = objWMIService.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & sHost & "'")
For Each objPing in colPings
If objPing.StatusCode = 0 Then
sProtocolAddress = objPing.ProtocolAddress
Exit For
Else
WScript.Quit
End If
Next
'GET SUBNETMASK FROM THE *ACTIVE* NIC
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration " _
& "WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
IsMatch = False : i = 0
Set objNic = objWMIService.Get _
("Win32_NetworkAdapter.DeviceID=" & objNicConfig.Index)
For Each strIPAddress In objNicConfig.IPAddress
sIPAddress = strIPAddress
If sIPAddress = sProtocolAddress Then
IsMatch = True
sSubnetmask = objNicConfig.IPSubnet(i)
Exit For
End If
i = i + 1
Next
If IsMatch = True Then Exit For
Next
GetIpConf = array(sIPAddress,sSubnetmask)
End Function
Function PrefixLength(subnetmask)
DIM arrsubnetmask, i, bits
arrsubnetmask = split(subnetmask, ".")
PrefixLength = 0
For i = LBound(arrsubnetmask) To 3
Select Case arrsubnetmask(i)
case 0 bits = 0
case 128 bits = 1
case 192 bits = 2
case 224 bits = 3
case 240 bits = 4
case 248 bits = 5
case 252 bits = 6
case 254 bits = 7
case 255 bits = 8
End Select
PrefixLength = PrefixLength + bits
Next
End Function
I wonder if anyone can help on the script below. It works fine on XP and deploys printers by the subnet your PC is in.
However in Windows 7, I get the following Windows Script Host Error
Line: 82
Char 10
Error: Type mismatch: 'addressbytes(...)'
Code: 800A000D
Source: Microsoft VBScript runtime error
Dim strSubnet, strComputer, WshNetwork
Dim strPrinter1, strPrinter2, strPrinter3, strPrinter4
Dim objWMIService
Set WshNetwork = WScript.CreateObject("WScript.Network")
strPrinter1 = "\\BEDA\BTH04P21"
strPrinter2 = "\\BEDA\BTH05P01"
strPrinter3 = "\\BEDA\BTH06P01"
strPrinter4 = "\\BEDA\BTH07P01"
On Error Resume Next
WshNetwork.RemovePrinterConnection _
strPrinter1, true, true
WshNetwork.RemovePrinterConnection _
strPrinter2, true, true
WshNetwork.RemovePrinterConnection _
strPrinter3, true, true
WshNetwork.RemovePrinterConnection _
strPrinter4, true, true
On Error GoTo 0
strComputer = WshNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer _
& "\root\cimv2")
strSubnet = GetSubnet(strComputer)
'Assign printers to laptop users
Select Case strSubnet
Case "172.16.72.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter1
Case "172.16.92.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter2
Case "172.16.100.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter3
Case "172.16.76.0/22"
WSHNetwork.AddWindowsPrinterConnection strPrinter4
End Select
wscript.Quit
Function GetSubnet(sHost)
' Dim arrNetworkAddress, prefix
arrNetworkAddress = GetIpConf(sHost)
Dim ActiveIPAddress, ActiveSubnetMask
ActiveIPAddress = arrNetworkAddress(0)
ActiveSubnetMask = arrNetworkAddress(1)
Dim addressbytes(4), addressbyte
Dim subnetmaskbytes(4), subnetmaskbyte
Dim subnet, i, period, prevperiod
i=0 : period = 1
while period<>len( ActiveIPAddress ) + 2
prevperiod=period
period = instr( period+1, ActiveIPAddress, "." ) + 1
if period = 1 then period = len( ActiveIPAddress ) + 2
addressbyte = mid( ActiveIPAddress, prevperiod, _
period-prevperiod-1 )
addressbytes(i)=addressbyte
i=i+1
wend
i=0 : period = 1
while period<>len( ActiveSubnetMask ) + 2
prevperiod=period
period = instr( period+1, ActiveSubnetMask, "." ) + 1
if period = 1 then period = len( ActiveSubnetMask ) + 2
subnetmaskbyte = mid( ActiveSubnetMask, prevperiod, _
period-prevperiod-1 )
subnetmaskbytes(i)=subnetmaskbyte
i=i+1
wend
subnet=""
for i=0 to 3
subnet = subnet & (addressbytes(i) _
AND subnetmaskbytes(i)) & "."
next
GetSubnet = left( subnet, len(subnet)-1 )
prefix = PrefixLength(ActiveSubnetMask)
GetSubnet = GetSubnet & "/" & prefix
End Function
Function GetIpConf(sHost)
' objWMIService has global scope
If not IsObject(objWMIService) Then
Dim objWMIService
Set objWMIService = GetObject("winmgmts:{impersonationLevel=" _
& "impersonate}!\\" & sHost & "\root\cimv2")
End If
DIM colPings, objPing, sProtocolAddress, colNicConfigs
DIM objNicConfig, i, sIPAddress, IsMatch, sSubnetmask
DIM objNic, strIPAddress, arrsubnetmask
'DETERMINE THE *ACTIVE* IP ADDRESS
Set colPings = objWMIService.ExecQuery _
("Select * From Win32_PingStatus where Address = '" & sHost & "'")
For Each objPing in colPings
If objPing.StatusCode = 0 Then
sProtocolAddress = objPing.ProtocolAddress
Exit For
Else
WScript.Quit
End If
Next
'GET SUBNETMASK FROM THE *ACTIVE* NIC
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration " _
& "WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
IsMatch = False : i = 0
Set objNic = objWMIService.Get _
("Win32_NetworkAdapter.DeviceID=" & objNicConfig.Index)
For Each strIPAddress In objNicConfig.IPAddress
sIPAddress = strIPAddress
If sIPAddress = sProtocolAddress Then
IsMatch = True
sSubnetmask = objNicConfig.IPSubnet(i)
Exit For
End If
i = i + 1
Next
If IsMatch = True Then Exit For
Next
GetIpConf = array(sIPAddress,sSubnetmask)
End Function
Function PrefixLength(subnetmask)
DIM arrsubnetmask, i, bits
arrsubnetmask = split(subnetmask, ".")
PrefixLength = 0
For i = LBound(arrsubnetmask) To 3
Select Case arrsubnetmask(i)
case 0 bits = 0
case 128 bits = 1
case 192 bits = 2
case 224 bits = 3
case 240 bits = 4
case 248 bits = 5
case 252 bits = 6
case 254 bits = 7
case 255 bits = 8
End Select
PrefixLength = PrefixLength + bits
Next
End Function