Thursday, January 31, 2013

determining correct MS Print Server based on IP

For this request.  We wanted a way to determine, based on the workstations IP, what print server a user should be pointed to. It first reads in the contents of the CSV file, then the IP of the workstation, then compares it against a list of IP Ranges in the CSV file until it finds a match.  Finally it launches the, with IE, the Printer selection page on that Print Server.

Idea's for enhancement, it would be great to build the Site list off AD Site boundaries.  Then pull in the assigned print server (not sure how) for that site.  Basically, eliminate the static CSV file, and make it dynamic.  Else, someone will need to maintain the list of sites ans servers in the CSV file forever (although they shouldn't change all that often)...




'----------------------------------------------------------------
' Purpose: Scans against a CSV file containing Subnet information
' the local Workstations IP address, then if it finds a match
' it Launches a Web Page with IE for the Local Print Server
' Version: 1.0
' NOTE:  in the CSV file the format must be: (IP Addresses must be in Quotes)
' "172.0.1.1","172.255.255.254",{AD SITE NAME}, {PRINT SERVER}
' Created by: Corey A Sines
'----------------------------------------------------------------
On Error Resume Next ' If this isn't set, any incorrect entry in the CSV file will cause the script to exit.

forceCScriptExecution

VERBOSE = 1 ' Sets script to display debug info
sCSVFile = "printservers.csv" ' File must be in the same container as the VBS file, you can add pathing / cmd line args if you want...

' Commands used to open the CSV file and select all of the records
set oConnection = createobject("adodb.connection")
set oRecordSet = createobject("adodb.recordset")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = Wscript.CreateObject("Wscript.Shell")
iRecCount = 1 ' record counter

sWorkstationIP = GetIPAddress() ' calls function to get IP Address
'comment out the above variable for sWorkstationIP, and use the below variable to set to a random IP for testing
'sWorkstationIP = "172.0.0.23"

Wscript.Echo "---------------------------------------------------------"
Wscript.Echo "Running Find Local Print Server Script on " & Date & " " & Time
If FSO.FileExists(sCSVFile) Then 'Opens the CSV file with ADO Objects
If VERBOSE = 1 Then Wscript.echo "Atttemping to Open CSV File :" & sCSVFile End If
oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & WshShell.CurrentDirectory &_
";Extended Properties=" & Chr(34) & "text;HDR=NO;FMT=Delimited" & Chr(34)
oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection ' Selecting all

If Err.Number <> 0 Then ' Error occured in the above code
    DisplayError "An ADODB Error Occured opening the CSV file!   Script Exiting!"
    Wscript.Quit(1) ' Script exits
End If  
Else
WScript.Echo "CSV file: " & sCSVFile & "Not found!" & vbCrLf & "Script Exiting..."
WScript.Quit(1) ' Script exits
End If

Wscript.Echo "Attempting to Determine what Subnet this Workstation at IP:" & sWorkstationIP & " Belongs to..."

While oRecordSet.EOF = False
' Read variable information from the CSV file
' and determines what the is Appropriate Print Server
sIPRangeStart = oRecordSet.Fields.Item(0).value ' Starting IP Address
sIPRangeEnd = oRecordSet.Fields.Item(1).value ' Ending IP Address
sADSiteName = oRecordSet.Fields.Item(2).value ' AD Site value for the IP Range
sPrintServer = oRecordSet.Fields.Item(3).value ' Print Server for the Specific Site

If VERBOSE = 1 Then
Dim strLDAP
Wscript.Echo "---------------------------------------------------------"
Wscript.Echo "Record Count:" & iRecCount
WScript.Echo "Attempting IP Range: " &  sIPRangeStart & " - " & sIPRangeEnd

tmpArraySt = Split(sIPRangeStart, ".", -1,0)
iClassASt = int(tmpArraySt(0))
iClassBSt = int(tmpArraySt(1))
iClassCSt = int(tmpArraySt(2))

'Wscript.echo iClassASt & " " & iClassBSt & " " & iClassCSt

tmpArrayEnd = Split(sIPRangeEnd, ".", -1,0)
iClassAEnd = int(tmpArrayEnd(0))
iClassBEnd = int(tmpArrayEnd(1))
iClassCEnd = int(tmpArrayEnd(2))

'Wscript.echo iClassAEnd & " " & iClassBEnd & " " & iClassCEnd

tmpArrayIP = Split(sWorkstationIP, ".", -1,0)
iWorkStIP_A = int(tmpArrayIP(0))
iWorkStIP_B = int(tmpArrayIP(1))
iWorkStIP_C = int(tmpArrayIP(2))

'Wscript.echo iWorkStIP_A & " " & iWorkStIP_B & " " & iWorkStIP_C

If iWorkStIP_A >= iClassASt and iWorkStIP_A <= iClassAEnd Then
'Wscript.echo "Class A matched, trying Class B"
If iWorkStIP_B >= iClassBSt and iWorkStIP_B <= iClassBEnd Then
'Wscript.echo "Class B matched, trying Class C"
If iWorkStIP_C >= iClassCSt and iWorkStIP_C <= iClassCEnd Then
wscript.echo "This workstation is part of this site: " & sADSiteName
wscript.echo "Local Print Server will be set to: " & sPrintServer
Return = WshShell.Run("iexplore.exe http://" & sPrintServer & "/printers/", 1)
Wscript.quit(0)
Else
Wscript.echo "No Network Match for Subnet:" & sIPRangeStart & " - " & sIPRangeEnd
End If
Else
Wscript.echo "No Network Match for Subnet:" & sIPRangeStart & " - " & sIPRangeEnd
End If
Else
Wscript.echo "No Network Match for Subnet:" & sIPRangeStart & " - " & sIPRangeEnd
End If

End if


iRecCount = iRecCount + 1
oRecordSet.MoveNext

Wend

oRecordSet.Close
oConnection.Close

Set WshShell = Nothing
Set FSO = Nothing
set oConnection = nothing
set oRecordSet = nothing

'----------------------------------------Subs and Functions-----------------------------------

Function GetIPAddress() 'Function that Returns the IPAdress of a workstation, note in machines
' with multiple NIC adapters, or multiple bound IPs(shouldn't be many)
' it will return only the first NIC's IP address.

Dim strComputer, odjWMIService, IPConfigSet, IPAdress, IPConfig
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.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)
'WScript.Echo IPConfig.IPAddress(i)
GetIPAddress = IPConfig.IPAddress(0)
Next
End If
Next
End Function
'-------------
Sub forceCScriptExecution ' Script designed to run under cscript only!
    Dim Arg, Str
    If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
        For Each Arg In WScript.Arguments
            If InStr( Arg, " " ) Then Arg = """" & Arg & """"
            Str = Str & " " & Arg
        Next
        CreateObject( "WScript.Shell" ).Run "cscript //nologo """ & WScript.ScriptFullName & """" & Str
        WScript.Quit
    End If
End Sub

No comments:

Post a Comment