'
'Program Name: mapprinters.VBS
'
'This Script can be set up to map network printers and Drives
'according to the group membership VLAN the computer IP Address.
'It uses the third octet in the IP address to determine the VLAN.
'
'-----------------
Option Explicit
On Error Resume Next
'-----------------
'global variables
Dim WSHShell, WshProcessEnvironment, EnvTempDir, oNet, oWshNetwork
Dim oGroupDict, IPAddress, IPConfigSet, IPConfig, i, ThrdOctet, VLAN,
filepath
Dim PrinterServer, objShell, objFileSystem, objTextStream, nReturnCode, iPos
Dim objTextFile, sRead, sReadLine, sReadAll, sSkipChar, SskipLine
'-----------------
'Objects
Set oNet = CreateObject("Wscript.Network")
'-----------------
'Function Calls
ThrdOctet = GetPartOfIP(GetIP(), 3)
VLan = GetVLAN(ThrdOctet)
'all mapping is done from'here
'you should not have to change
functions
'unless they do not work for
some'reason
'you can put any combination of code
in
'to process the function calls
PrinterServer="\\Print_Server"
filepath = "C:\printers_names.txt"
MapFromList(VLAN)
'this is the end of the function
calls
'-----------------
'map a printer
Function MapPrinter(PrintServer, Printer)
On Error Resume Next
Dim oPrinter, PrtServer, PrtName, Prt
Prt = PrintServer & Printer 'Strip backslashes from UNC names
PrtServer = Left(Prt,InStr(3,Prt, "\") - 1)
PrtServer = Right(Left(Prt,InStr(3,Prt, "\") - 1), Len(PrtServer) - 2)
PrtName = Right(Prt, Len(Prt) - Len(PrtServer) - 3)
On Error Resume Next 'If printer or server dosen't exist,
code continues execution
Set oPrinter = GetObject("WinNT://" & oNet.UserDomain & "/" & PrtServer
& "/" & PrtName)
If (oPrinter.Name = "" OR oPrinter.Class <> "PrintQueue") Then
Wscript.Echo("Printer " & PrintServer & Printer & " is not a valid
network printer")
Else
oNet.AddWindowsPrinterConnection(PrintServer & Printer)
' Wscript.Echo("Mapping printer to " & PrintServer & Printer)
End If
End Function
'-----------------
'Get IP Address Using IPConfig
Function GetIP()
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshProcessEnvironment = WshShell.Environment("Process")
EnvTempDir = WshProcessEnvironment("Temp")
Const ForReading = 1
If WScript.Arguments.Count Then
If InStr(WScript.Arguments.Item(0), "?") Then
Call Usage()
End If
End If
Set objShell = WScript.CreateObject("WScript.Shell")
nReturnCode = objShell.Run("cmd /C ipconfig.exe >"&
EnvTempDir&"\ipconfig.txt", 0, TRUE)
If Err.Number Then
WScript.Echo "Unable to run ipconfig.exe! Script aborting."
WScript.Quit(1)
End If
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFileSystem.OpenTextFile(EnvTempDir&"\ipconfig.txt",
ForReading)
Do While objTextStream.AtEndOfStream <> True
sReadLine = objTextStream.ReadLine
iPos = instr(sReadLine, "IP Address. . . . . . . . . . . . : ")
if iPos > 0 then
IPAddress = mid(sReadLine, iPos + 36)
end if
Loop
objTextstream.Close
objFileSystem.DeleteFile(EnvTempDir & "\ipconfig.txt")
Set objTextStream = Nothing
Set objFileSystem = Nothing
Set objShell = Nothing
GetIP = IPAddress
End Function
'-----------------
'Get IP Address Using WMI
'Function GetIP()
'set IPConfigSet =
GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
' ("select IPAddress from Win32_NetworkAdapterConfiguration where
IPEnabled=TRUE")
'for each IPConfig in IPConfigSet
' if Not IsNull(IPConfig.IPAddress) then
' for i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
'IPAddress = IPConfig.IPAddress(i)
'GetIP = IPAddress
' next
' end if
'next
'End Function
'-----------------
'Remove Printer maping
Function RemovePrinter(PrintServer, Printer)
'On Error Resume Next
Dim oPrinter, PrtServer, PrtName, Prt
Prt = PrintServer & Printer 'Strip backslashes from UNC names
PrtServer = Left(Prt,InStr(3,Prt, "\") - 1)
PrtServer = Right(Left(Prt,InStr(3,Prt, "\") - 1), Len(PrtServer) - 2)
PrtName = Right(Prt, Len(Prt) - Len(PrtServer) - 3)
' On Error Resume Next 'If printer or server dosen't exist,
code continues execution
Set oPrinter = GetObject("WinNT://" & oNet.UserDomain & "/" & PrtServer
& "/" & PrtName)
If (oPrinter.Name = "" OR oPrinter.Class <> "PrintQueue") Then
Wscript.Echo("Printer " & PrintServer & oPrinter.Name & " is not a
valid network printer")
Else
oNet.RemovePrinterConnection(PrintServer & Printer)
Wscript.Echo("Removeing printer Mapping to " & PrintServer &
Printer)
End If
End Function
'-----------------
'Get Value of any of the four IP Address octets
Function GetPartOfIP(ByVal strIP , ByVal intPos)
Dim tmp
On Error Resume Next
If Len(strIP) > 0 Then
tmp = Split(strIP, ".")
If IsArray(tmp) Then
If IsNumeric(tmp(intPos - 1)) Then
GetPartOfIP = (tmp(intPos - 1))
End If
Else
If IsNumeric(strIP) Then
GetPartOfIP = (strIP)
End If
End If
End If
End Function
'-----------------
'find out what groups the user
belongs too
Function IsMember(sGroup)
On Error Resume Next
Dim sAdsPath, oUser, oGroup
If IsEmpty(oGroupDict) Then
Set oGroupDict = CreateObject("Scripting.Dictionary")
oGroupDict.CompareMode = vbTextCompare
sAdsPath = oNet.UserDomain & "/" & oNet.UserName
Set oUser = GetObject("WinNT://" & sAdsPath & ",user")
For Each oGroup In oUser.Groups
oGroupDict.Add oGroup.Name, "-"
Next
Set oUser = Nothing
End If
IsMember = CBool(oGroupDict.Exists(sGroup))
End Function
'-----------------
'Map printers Using Specified List
Function MapFromList(intVLAN)
Const ForReading = 1
Dim fso, theFile, retstring
Dim strList
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filepath, ForReading, False)
If Err.Number = 0 Then
Do While theFile.AtEndOfStream <> True
retstring = theFile.ReadLine
strList = split(retstring, vbTab)
If IsArray(strList) Then
If GetVLAN(GetPartOfIP(strList(0), 3)) = intVLAN Then
MapPrinter PrinterServer, "\" & strList(1)
End If
End If
if err then exit do
Loop
theFile.Close
End If
Set fso = Nothing
End Function
'-----------------
'Determine VLAN ID :Numbers are put as example
Function GetVLAN(ThrdOctet)
dim VLan
select case true
case ThrdOctet >=1 And ThrdOctet <=50
VLan = 10
case ThrdOctet >=51 And ThrdOctet <=150
VLan = 20
case ThrdOctet >=151 And ThrdOctet <=200
VLan = 30
case ThrdOctet >=201 And ThrdOctet <=254
VLan = 40
end select
GetVLAN = VLan
End Function
'-----------------
Regards,
Antony
____________________________________________________________
Antony Samuel Naiim
M.I.T. -Nottingham Univ. - U.K.
Information systems Dept.
Apache Egypt companies
Web:
www.apachecorp.com
____________________________________________________________
Does anyone know where I can get some sample code on how
to map printers via a log in script.
Thanks in advance!
Shayne