Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...it was ingeniously designed and all those clicks were for my own good... and that was even before I got my speedy and useful answer to my tekkie question that I eventually posted..."

Geography

Where in the world do Tek-Tips members come from?

Deploy Printers based on Subnet

Gracious666 (Programmer)
25 Jul 12 9:40
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)
' http://en.wikipedia.org/wiki/Subnetwork
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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close