Windows: 根据分组的本地TCP/IP打印机的安装(2)

序:

 

Printer Driver Tested:

1. HP Universal Printer Driver 6.*

    This version of driver is a Windows signed driver, please download it from HP offical website.

    Driver Model: HP Universal Printing PCL 6

2. Ricoh Universal Printer Driver:

    This version of driver is a Windows signed driver, please download it from Ricoh offical website.

    Driver Model: PCL6 Driver for Universal Print

3. KyoceraUniversal Printer Driver:

    This version of driver is a Windows signed driver, please download it from Kyocera offical website.

    Driver Model: KX DRIVER for Universal Printing

 

 

Codes:

'-----------------------------------------------------------------------------------
' Name: Local TCP/IP Printer Setup
' History:
'    Created: May 14, 2008
'    Updates: Dec 3, 2010
'             Dec 7, 2010, adding support command line arguments.
'             Dec 13, 2010, deal with multiple printer share one port
' Description:
'    This script set up TCP/IP printer on a local PC for users who can access it.
'    It is used on the following 3 cases:
'       School has no server.
'       School has a Mac server.
'       School has a PC server, but some AD user groups have to use local
'          TCP/IP printer.
'
On Error Resume Next

' ================================================
' The following could be changed by user.
' -----------------------------------------
' Global Definition of Settings.ini file.
' -----------------------------------------
Const  LOCATION_FILE = "Locations.ini"
Const  PRINTERS_INFO = "PrinterConfig.ini"
Const  PRINTR_LIST_PST = ".txt"
Const  DEFAULT_LOCATION = "Default"

' ===================================================================================
' Usually user don't need to change the following values.
' -----------------------------------------------
' Global Variables and constants.
' -----------------------------------------------
Const  sVer = "0.5.3"

'------------------------
' Public Env preparation
'------------------------
' Text File operation code
Const FOR_READING = 1
Const FOR_WRITING = 2
Const For_APPENDING = 8

'------------------------
' Log Level Define
'------------------------
Const LOGLVL_None = 0
Const LOGLVL_SYSTEM = 0
Const LOGLVL_Error = 1
Const LOGLVL_Warning = 2
Const LOGLVL_ALL = 3
Const LOGLVL_Message = 3
Const LOGLVL_Debug = 9

Public piLogLevel
Public piTatalWarnings
Public piTatalErrors
Public pLogFileName

' ================================================
' ***User do NOT change the follwoing values.***
' ================================================
Const IDX_LOCATION = 0
Const IDX_COMPUTER = 1

'-----------------------------------
' Printer Info index in array
'-----------------------------------
Const IDX_Staff = 0
Const IDX_PrinterName = 1
Const IDX_IP = 2
Const IDX_InfFile = 3
Const IDX_DriverModel = 4
Const IDX_PLocation = 5
Const IDX_PComment = 6

'-----------------------------------
' Command line options
'-----------------------------------
Const SETTING_ITEM_NAME = 0
Const SETTING_ITEM_VALUE = 1
Const SCHOOL_CODE = "/c"
Const CONFIG_PATH = "/p"
Const DELETE_ALL = "/d"
Const LOCATION_FROM = "/ad"
Const LOG_LEVEL = "/l"
Const DELETE_SELF = "/s"
Const REMOTE_COMPUTER = "/r"
Const HELP_ARGS = "/h"
Const HELPS_ARGS = "/?"
Const QUIET_MODE = "/q"
Const CMD_DELETEALL = "CMD /C del /F /S /Q *.*"

'-----------------------------------
' Global Setting variables
'-----------------------------------
Public pstrSchoolCode
Public pstrConfig_Path
Public pstrDelete_All
Public pLOCATIONCTRL
Public pbDeleteSelf
Public piQuietMode
Public pstrLocationFile_Pre
'Public pstrComputer

'-----------------------------------
' Printer Related variables
'-----------------------------------
Public paPrinterList            ' Printer Configuration Info List
Public piPrinterListCount        ' All printer count

Public paPrinters(510)    ' Computer's local Printer setup List
Public piPrinterNum        '

'-----------------------------------
' Other Public variables
'-----------------------------------
Public pstrComputer            ' Current Computer pointer
Public pstrComputerName        ' Current Computer Name
Public pobjWMIService        ' Current Computer's WMI object
Public objFSObj                ' File System object.
Public pobjWMISvr            '

'-------------------------------------------
' Main process
'-------------------------------------------
   strLogInfo = "Set up local TCP/IP printer, start running at " & time() & " on " & Date()
   strLogInfo = strLogInfo & vbCrLf & "Version " & sVer & " 2010 Tony Liu (toliu@cbe.ab.ca)"
   myLog LOGLVL_SYSTEM, strLogInfo
' 1. --- Get settings, initial system parameters, and get printer setting List ---
If GetArguments() <> 0 Then WScript.Quit
If SysInit() <> 0 Then WScript.Quit
If GetPrinterConfigList(objFSObj, pstrConfig_Path & pstrLocationFile_Pre & PRINTERS_INFO) <> 0 Then WScript.Quit

' 2. --- Get printers setup list from location ---
If pLOCATIONCTRL = "AD" Then
   strLocation = GetLocation_AD (pstrComputerName)
Else
   strLocation = GetLocation_File (pstrComputerName, pstrConfig_Path & pstrLocationFile_Pre & LOCATION_FILE, objFSObj)
End If
If strLocation = "" Then strLocation = DEFAULT_LOCATION
strLocationFile = pstrConfig_Path & LocationFileName(strLocation)
if ReadLocationPrinters(objFSObj, strLocationFile) <> 0 Then WScript.Quit

' 3. --- Remove Ports and/or Printers ---
If pstrDelete_All = True Then
   iResult = RemoveAllTCPIPPrinterandPorts (pobjWMIService)
Else
   iResult = DeleteUnusedTCPIPProts (pobjWMIService)
End If

' 4. --- Add Printers on list to this computer ---
iReturn = AddAllPrinters(pobjWMIService, paPrinterList, paPrinters, pstrSchoolCode)

' 5. --- Finial info ---
strLogInfo = vbCrLf & "Total Warnings: " & piTatalWarnings & vbCrLf & "Total Errors: " & piTatalErrors
strLogInfo = strLogInfo & vbCrLf & "Program ended successfully at " & time() & "." & vbCrLf & "<<<<<<"
myLog LOGLVL_ALL, strLogInfo

' 6. --- Remove Self ---
If pbDeleteSelf=True Then
   Set WshShell = CreateObject("WScript.Shell")
   WshShell.Run CMD_DELETEALL, 6
End If
WScript.Quit


'-------------------------------------------
' Get all scripts' arguments
'-------------------------------------------
Function GetArguments ()

   Const ARGUMENTS_ID = "<GetArguments>:"
   Err.Clear

   ' Set public variables' default value.
   pstrSchoolCode = ""
   pstrLocationFile_Pre = pstrSchoolCode
   pstrConfig_Path = "./"
   pstrDelete_All = False
   pLOCATIONCTRL = "FILE"
   piLogLevel = LOGLVL_Error
   pbDeleteSelf = False
   pstrComputer = "."
   piQuietMode = 0

   GetArguments = 0
   Set objArgs = WScript.Arguments
   LAST_Item = objArgs.Count -1
   If objArgs.Count < 1 Then
         wscript.echo " /h for help"
      Exit Function
   End If
  
   for args_index = 0 to LAST_ITEM
      if piLogLevel >= LOGLVL_Debug then
         WScript.Echo "Arguments[" & args_index & "]:" & objArgs(args_index)
      End If
     
      strOption = LCase(objArgs(args_index))
      select case strOption
      case SCHOOL_CODE: ' school code
         args_index = args_index + SETTING_ITEM_VALUE
         pstrLocationFile_Pre = Trim(objArgs(args_index)) & "_"
         ' pstrSchoolCode =
      case CONFIG_PATH: ' configuration files path
         args_index = args_index + SETTING_ITEM_VALUE
         pstrConfig_Path = MakeFilePath(objArgs(args_index))
      case DELETE_ALL:  ' Delete all other TCP/IP printers?
         pstrDelete_All = True
      case LOCATION_FROM: ' Location info from AD or file?
         pLOCATIONCTRL = "AD"
      case LOG_LEVEL:   ' Log level
         args_index = args_index + SETTING_ITEM_VALUE
         piLogLevel = Int(objArgs(args_index))
      case DELETE_SELF: ' If delete this script self?
         'args_index = args_index + SETTING_ITEM_VALUE
         'strALL = Ucase(objArgs(args_index))
         'If Left(strALL,1) = "N" Then
         '   pbDeleteSelf = False
         'Else
            pbDeleteSelf = True
         'End If
'      case REMOTE_COMPUTER:
'         args_index = args_index + SETTING_ITEM_VALUE
'         pstrComputer = Ucase(TRIM(objArgs(args_index)))
      case QUIET_MODE:
         piQuietMode = 1
      case HELPS_ARGS:
         ShowHelps
      case HELP_ARGS:
         ShowHelps
      End Select
   Next

   if piLogLevel >= LOGLVL_Debug then
      ListGlobalSettins
   End If
  
   GetArguments = err.number
   If err <> 0 Then
      myLog LOGLVL_Error, ARGUMENTS_ID & "Phasing Arguments: Error:" & err.number & ":" _
         & err.description
   Else
      myLog LOGLVL_Message, ARGUMENTS_ID & "Phasing Arguments passed."
   End If
   Err.Clear
End Function

'-------------------------------------------
' Show help info lines
'-------------------------------------------
Function ShowHelps ()
   if piQuietMode > 0 then
      Exit Function
   End if
   WScript.Echo "Usage: command [option1 ...]"
   WScript.Echo "   " & CONFIG_PATH & " Path:" & vbTab & vbTab & " Full path of configuration file."
   WScript.Echo "   " & SCHOOL_CODE & " SchoolCode:" & vbTab & " School code of configuration files, default is none."
   WScript.Echo "   " & LOG_LEVEL & " [0|1|2]:" & vbTab & vbTab & " 0=none, 1=error, 2=warnings, default=1."
   WScript.Echo "   " & LOCATION_FROM & ":" & vbTab & " Retrieve workstation's location information from AD. default is file."
   WScript.Echo "   " & DELETE_ALL & ":" & vbTab & " Delete all TCP/IP printers and ports first."
   WScript.Echo "   " & DELETE_SELF & ":" & vbTab & " This program kill itself after done."
   WScript.Echo "   " & HELPS_ARGS & ":" & vbTab & " This help."
'   WScript.Echo "   " & REMOTE_COMPUTER &  ":" & vbTab & " Remote computer name."
'   WScript.Echo "   " & QUIET_MODE & ":" & vbTab & " Quiet mode."
   WScript.Quit
End Function

'-------------------------------------------
' System initialize
'-------------------------------------------
Function SysInit ()

   Const SYSINIT_ID = "<SysInit>:"
   piTatalWarnings = 0
   piTatalErrors = 0

   ' Get local machine name
   If pstrComputer = "." then
      Set objComputer = CreateObject("Shell.LocalMachine")
      pstrComputerName = UCase(Trim(objComputer.MachineName))
   Else
      pstrComputerName = Left(pstrComputer,split(pstrComputer, ".", 1)-1)
   End If
   pstrSchoolCode = Mid(strComputerName, 2, 3)

   Set pobjWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & pstrComputer & "/root/cimv2")
   If err <> 0 Then
      myLog LOGLVL_Error, SYSINIT_ID & "Program Initial: 1.Error:" & err.number & ":" & err.description
      SysInit = err.Number
      Exit Function
   End If
  
   Set objFSObj = CreateObject("Scripting.FileSystemObject")
   If err <> 0 Then myLog LOGLVL_Error, SYSINIT_ID & "Program Initial: 2.Error:" & err.number & ":" & err.description
  
   Set paPrinterList = CreateObject("Scripting.Dictionary")
   If err <> 0 Then myLog LOGLVL_Error, SYSINIT_ID & "Program Initial: 3.Error:" & err.number & ":" & err.description

   SysInit = err.Number
   If err <> 0 Then
      myLog LOGLVL_Error, SYSINIT_ID & "Program Initial: 4.Error:" & err.number & ":" & err.description
   Else
      myLog LOGLVL_ALL, SYSINIT_ID & "Program Initialization passed."
   End If
   Err.Clear
End Function

'-------------------------------------------
' Get all printers configuration definitions
'-------------------------------------------
Function GetPrinterConfigList(objFSO, strFileName)
   Const GetPrinterConfigList_LogID = "<GetPrinterConfigList>:"

   strLogInfo = GetPrinterConfigList_LogID & "Starting to read printer info file<" & strFileName & ">"
   myLog LOGLVL_Message, strLogInfo
   GetPrinterConfigList = 0
   piPrinterListCount = 0
   Err.Clear

   Set hFile = objFSO.OpenTextFile(strFileName, FOR_READING)
   if IsEmpty(hFile) OR Err <> 0 Then
      strLogInfo = GetPrinterConfigList_LogID & "Couldn't open Printer_Info file <" & strLocationFile & ">."
      myLog LOGLVL_Error, strLogInfo
      GetPrinterConfigList = err
   Else
      Do Until hFile.AtEndOfStream
         strThisLine = LTRIM(hFile.Readline)
         If Left(strThisLine, 1) <> "#" AND Len(strThisLine)>0 Then
            arrPInfo = Split(strThisLine, ",")
            strPrinterName = UCase(Trim(arrPInfo(IDX_PrinterName)))
            paPrinterList.add strPrinterName, arrPInfo
            piPrinterListCount = piPrinterListCount + 1
         End If
      Loop
   End If
   hFile.Close

   '
   GetPrinterConfigList = err
   If err <> 0 Then
      myLog LOGLVL_Error, GetPrinterConfigList_LogID & "Failed of reading printer definition file <" & err & ">:" & err.descrption
   Else
      myLog LOGLVL_Message, GetPrinterConfigList_LogID & "Read <" & piPrinterListCount & "> record(S) of printer from list <" & strFileName & ">."
   End If
   Err.Clear
End Function


'------------------------------------------------------
' Get computer location from AD
'------------------------------------------------------
Function GetLocation_AD (strComputerName)
   '
   Const GetLocation_AD_LogID = "<GetLocation_AD>:"
   Err.Clear
   GetLocation_AD = ""

   strSchoolCode = Mid(strComputerName, 2, 3)
   Set objThisComputer = GetObject("LDAP://CN=" & strComputerName & ",OU=Workstations,OU=" & strSchoolCode & ",OU=Schools,DC=edu,DC=ca")
   GetLocation_AD = Trim(objThisComputer.Location)

   If err <> 0 Then
      myLog LOGLVL_Error, GetLocation_AD_LogID & "error on fetching Computer:<" & strComputerName & "> location on AD."
   Else
      myLog LOGLVL_Message, GetLocation_AD_LogID & "Computer:<" & strComputerName & "> location on AD is <" & GetLocation_AD & ">"
   End If

   Err.Clear
End Function


'------------------------------------------------------
' Get computer location from location file.
'------------------------------------------------------
Function GetLocation_File (strComputerName, strLocationFile, objFSO)
   '
   Const GetLocation_File_LogID = "<GetLocation_File>:"

   GetLocation_File = ""

   strComputerName = UCase(strComputerName)
'   myLog LOGLVL_Message, GetLocation_File_LogID & "Starting to read Location file <" & strLocationFile & "> on <" & strComputerName & "> computer."

   Set hFile = objFSO.OpenTextFile(strLocationFile, FOR_READING)

   if IsEmpty(hFile) or Err <> 0 Then
      '
      strLogInfo = GetLocation_File_LogID & "Couldn't open location file <" & strLocationFile & ">."
      myLog LOGLVL_Error, strLogInfo
      GetLocation_File = Null
   Else
      Do Until hFile.AtEndOfStream
         strThisLine = Trim(hFile.Readline)
         If Left(strThisLine, 1) <> "#" and Len(strThisLine)>0 Then
            arrLocation = Split(strThisLine, ",")
            strReadComputer = UCase(Trim(arrLocation(IDX_COMPUTER)))
            If strComputerName = strReadComputer Then
               GetLocation_File = Trim(arrLocation(IDX_LOCATION))
               Exit Do
            End If
         End If
      Loop
      hFile.Close
   End If

   If len(GetLocation_File) = 0 Then
      strLogInfo = GetLocation_File_LogID & "Can't find location for computer <" & strComputerName & ">."
      myLog LOGLVL_Warning, strLogInfo
   Else
      strLogInfo = GetLocation_File_LogID & "Location got for computer <" & strComputerName & "> is <" & GetLocation_File & ">."
      myLog LOGLVL_Message, strLogInfo
   End If
End Function


'------------------------------------------------------
' Get printers for crrent location
'------------------------------------------------------
Function ReadLocationPrinters (objFSO, strPrinterListFile)
   '
   Const GLP_Warning_ID = "<ReadLocationPrinters>: "

   err.Clear
   piPrinterNum = 0

   Set hFile = objFSO.OpenTextFile(strPrinterListFile, FOR_READING)
   if IsEmpty(hFile) or Err <> 0 Then
      myLog LOGLVL_Error, GLP_Warning_ID & "Couldn't open the printer list file."
   Else
      Do Until hFile.AtEndOfStream
         strThisLine = Trim(hFile.Readline)
         If Left(strThisLine, 1) <> "#" and Len(strThisLine)>0 Then
            paPrinters(piPrinterNum) = UCase(strThisLine)
            piPrinterNum = piPrinterNum + 1
         End If
      Loop
      myLog LOGLVL_Message, GLP_Warning_ID & "Read <" & piPrinterNum & "> printers to be installed for current location."
      hFile.Close
   End If
  
   ReadLocationPrinters = err
   ListPrinterList
   err.Clear
End Function


'-------------------------------------------
' Remove All jobs, TCP/IP Printer and Ports
'-------------------------------------------
Function RemoveAllTCPIPPrinterandPorts (objWMIService)

   Const RemoveAllTCPIP = "<RemoveAllTCPIPPrinter&Ports>:"
   err.clear
   Set objDict = CreateObject("Scripting.Dictionary")

   ' 1. --- Delete all print jobs ---
   Set colPrintJobs =  objWMIService.ExecQuery("Select * from Win32_PrintJob")
   For Each objPrintJob in colPrintJobs
     objPrintJob.Delete_
   Next

   ' 2. --- Delete all TCP/IP Printer and TCP/IP ports ---
   Set colPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
   Set colPorts = objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort")
   For Each objPort in colPorts
      For Each objPrinter in colPrinters

         If objPrinter.PortName = objPort.Name Then objPort.Delete_
      Next
      ObjPort.Delete_
   Next

   RemoveAllTCPIPPrinterandPorts = err
   If err <> 0 Then
      myLog LOGLVL_Error, RemoveAllTCPIP & "Remove All TCP/IP Printer and Ports error(" & err & "):" & err.description
   Else
      myLog LOGLVL_Message, RemoveAllTCPIP & "All TCP/IP Printer and ports deleted."
   End If

   Err.Clear
End Function


'------------------------------------------------------
' Removed all unused TCp/IP ports
'------------------------------------------------------
Function DeleteUnusedTCPIPProts (objWMIService)
   Const RemoveAllTCPIP = "<DeleteUnusedTCPIPProts>:"
   err.clear
   Set objDict = CreateObject("Scripting.Dictionary")

   ' 1. --- Delete all print jobs ---
'   Set colPrintJobs =  objWMIService.ExecQuery("Select * from Win32_PrintJob")
'   For Each objPrintJob in colPrintJobs
'     objPrintJob.Delete_
'   Next

   ' 1. ---Get all printers in dictionary ---
   Set colPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
   For Each objPrinter in colPrinters
      if Not objDict.Exists(objPrinter.PortName) Then objDict.Add objPrinter.PortName, objPrinter
   Next

   ' 2. --- Delete all Unused TCp/IP Ports ---
   Set colPorts = objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort")
   For Each objPort in colPorts
      If Not objDict.Exists(objPort.Name) Then ObjPort.Delete_
   Next

   DeleteUnusedTCPIPProts = err
   If err <> 0 Then
      strLog = RemoveAllTCPIP & "Remove Unused TCP/IP Ports error(" & err & "):" & err.description
      myLog LOGLVL_Error, strLog
   Else
      myLog LOGLVL_Message, RemoveAllTCPIP & "Unused TCP/IP ports deleted."
   End If

   Err.Clear
End Function


'-------------------------------------------
' Add All Printers belong to this computer
'-------------------------------------------
Function AddAllPrinters(objWMIService, aPrinterList, aPrinterInfo, strSchoolCode)
   Const AddPrinter_LogID = "<AddAllPrinters>:"
   DefaultSet = False
   Err.Clear

   If piPrinterNum <= 0 Then
      myLog LOGLVL_Warning, AddPrinter_LogID & "No Printer is added [printer number=0]."
      Exit Function
   End If

   myLog LOGLVL_Message, AddPrinter_LogID & "---Starting Processing printers---"
   For i=0 to piPrinterNum-1
      ' For each printer on the list, find the printer Info in printinfo array
      curPrinter = paPrinters(i)
      myLog LOGLVL_Message, AddPrinter_LogID & "Processing printer(" & i+1 & ")->" & curPrinter

      bExist = paPrinterList.Exists(UCase(curPrinter))
      If bExist = TRUE Then
         arrPInfo       = paPrinterList.Item(curPrinter)
         strStaff       = UCase(Trim(arrPInfo(IDX_Staff)))
         strPrinterName = Trim(arrPInfo(IDX_PrinterName))
         strIP          = Trim(arrPInfo(IDX_IP))
         strInfFile     = Chr(34) & Trim(arrPInfo(IDX_InfFile)) & Chr(34)
         strDriverModel = Trim(arrPInfo(IDX_DriverModel))
         strPLocation   = Trim(arrPInfo(IDX_PLocation))
         strComments    = Trim(arrPInfo(IDX_PComment))
         ListPrinterInfo strStaff, strPrinterName, strIP, strInfFile, strDriverModel, strPLocation, strComments

         ' 1. --- Add port ---
         if strIp = "" then strIP = strPrinterName
         strPortName = MakeTCPIPPortName(strIP)
         strPortName = AddTCPIPProt (objWMIService, strPortName, strIP)

         ' 2. --- Rename printer if exists ---
         iResule = RenamePrinter(objWMIService, strPrinterName, strPrinterName & Hour(now) & Minute(now))

         ' 3. --- Printer Setup ---
         If Len(strInfFile) > 0 Then
            iResult = InstallPrinterINF (strPrinterName, strPortName, strDriverModel, strInfFile)
            myLog LOGLVL_Message, AddPrinter_LogID & "Printer <" & strPortName & "> set up with INF."
         Else
            myLog LOGLVL_Message, AddPrinter_LogID & "No INF file defined for <" & strPortName & ">, assuming printer driver already exists <" & strDriverModel & ">."
            iResult = InstallPrinter (objWMIService, strPrinterName, strPortName, strDriverModel)
         End If
         iRet = SetPrinterComments (objWMIService, strPrinterName, strPLocation, strComments, i)

         ' 6. --- Deny Student Access ---
         If iResule = 0 AND strStaff = "S" Then
            DenyGroup strPrinterName, strSchoolCode &"-Students_G"
         End If
      Else
         myLog LOGLVL_Error, AddPrinter_LogID & "Printer <" & curPrinter & "> not defined in PrintersList.txt file, is ignored."
      End If
      myLog LOGLVL_Message, AddPrinter_LogID & "Added (" & i+1 & "/" & piPrinterNum & ") printer with success."
   Next
   '
   Err.Clear
   myLog LOGLVL_Message, AddPrinter_LogID & "All of " & piPrinterNum & " printers has been processed."
End Function


'------------------------------------------------------
'
'------------------------------------------------------
Function InstallPrinter (objWMIService, strPrinterName, strPortName, strPrinterModel)
   Const InstallPrinter_LogID = "<InstallPrinter>:"
   myLog LOGLVL_Message, InstallDriver_LogID & "Start setup printer <" & strPrinterName & ">:"
   err.clear

   ' Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2")
   Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
   objPrinter.DriverName = strPrinterModel
   objPrinter.PortName   = strPortName
   objPrinter.DeviceID   = strPrinterName
   objPrinter.Network = True
   objPrinter.Shared = False
   objPrinter.Put_

   InstallPrinter = err
   If err <> 0 Then
      myLog LOGLVL_Error, InstallDriver_LogID & "Install printer <" & strPrinterName & "> on port <" & strPortName & "> with driver <" & strDriverName & "> error <" & err.Description & ">."
   Else
      myLog LOGLVL_Message, InstallPrinter_LogID & "installed printer<" & strPrinterName & "> at port <" & strPortName& "> with driver <" & strDriverName & ">."
   End If

   err.clear
End Function

'------------------------------------------------------
' Set printer Comments information and default
'------------------------------------------------------
Function SetPrinterComments (objWMIService, strPrinter, strLocation, strComment,iDefault)
   err.clear
'   Set colPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where DeviceID = '" & strPrinterName & "'")
'   For Each objPrinter in colPrinters
'      ObjPrinter.Location = strLocation
'      objPrinter.Put_
'      If bDefault = 0 Then
'         objPrinter.SetDefaultPrinter()
'      End If
'   Next
  
   strLocation = Chr(34) & strLocation & Chr(34)
   strComment = Chr(34) & strComment & Chr(34)
   Set WshShell = CreateObject("WScript.Shell")
   strCMD = "rundll32 printui.dll,PrintUIEntry /Xs /n " & strPrinter & " Location " & strLocation & " comment " & strComment
   ' wscript.echo ">>>>>" & strCMD
   WshShell.Run strCMD, 6, true
   if iDefault = 0 Then
      strCMD = "rundll32 printui.dll,PrintUIEntry /y /n " & strPrinter
      WshShell.Run strCMD, 6, true
   End If
End Function

'------------------------------------------------------
'
'------------------------------------------------------
Function InstallPrinterINF (strPrinterName, strPortName, strDriverName, strDriverINF)
   Const InstallPrinter_LogID = "<InstallPrinterINF>:"
   err.clear

   strPrinterName = chr(34) & Trim(strPrinterName) & chr(34)
   strPortName = chr(34) & Trim (strPortName) & chr(34)
   strDriverName = chr(34) & Trim (strDriverName) & chr(34)
   strPrinterLocation = chr(34) & Trim (strPrinterLocation) & chr(34)

   strCMD="rundll32 printui.dll,PrintUIEntry /b " & strPrinterName & " /f " & strDriverINF & " /r " & strPortName & " /m " & strDriverName
   Set WshShell = CreateObject("WScript.Shell")
   WshShell.Run strCMD, 6, true

   InstallPrinterINF = err
   If err <> 0 Then
      myLog LOGLVL_Error, InstallDriver_LogID & "Install printer <" & strPrinterName & "> on port <" & strPortName & "> with driver <" & strDriverName & "> error: <" & err.Description & ">."
   Else
      myLog LOGLVL_Message, InstallPrinter_LogID & "installed printer<" & strPrinterName & "> at port <" & strPortName& "> with driver <" & strDriverName & ">."
   End If
   err.clear
End Function

'------------------------------------------------------
'------------------------------------------------------
Function AddTCPIPProt (objWMIService, strPortName, strPortIP)
   Const AddTCPIPProt_Warning_ID = "<AddTCPIPProt>:"
   err.clear

   strPortIP = Trim(strPortIP)
   strPortName = Ucase(Trim(strPortName))
   bFound = False    'Default no same port found.

      ' There is no IP port exist, then Add one
      Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
      objNewPort.Name = strPortName
      objNewPort.Protocol = 1
      objNewPort.HostAddress = strPortIP
      objNewPort.PortNumber = "9100"
      objNewPort.SNMPEnabled = True
      objNewPort.SNMPCommunity = "public"
      objNewPort.SNMPDevIndex = "1"
      objNewPort.Put_

      strLogInfo = AddTCPIPProt_Warning_ID & "Added a TCP/IP Print Port:" & strPortName & " with IP:" & strPortIP
      myLog LOGLVL_Message, strLogInfo
      ' Set objNewPort = Null

   AddTCPIPProt = strPortName
   err.clear
End Function

'------------------------------------------------------
'------------------------------------------------------
Function RenamePrinter(objWMIService, strPrinterName, strToPrinterName)
   Const RenameP_ID = "<RenamePrinter>:"
   err.clear

   Set colPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where DeviceID = '" & strPrinterName & "'")
   For Each objPrinter in colPrinters
      objPrinter.RenamePrinter(strToPrinterName)
   Next
   myLog LOGLVL_Message, RenameP_ID & "Rename exist printer finished <" & strPrinterName & ">."

   RenamePrinter = err
   err.Clear
End Function


'------------------------------------------------------
'------------------------------------------------------
Function myLog (LogType, strLog)
   Err.Clear

   if piQuietMode > 0 then
      Exit Function
   End if
   '
   ' Ouput Only the LogType is rgeater than system Log Level
   '
   Select Case LogType
      Case LOGLVL_Warning
         piTatalWarnings = piTatalWarnings + 1
      Case LOGLVL_Error
         piTatalErrors = piTatalErrors + 1
   End Select

   If LogType <= piLogLevel Then
      Select Case LogType
         Case LOGLVL_Message
            strLog = "MSG: " & strLog
         Case LOGLVL_Warning
            strLog = "WRN: " & strLog
         Case LOGLVL_Error
            strLog = "ERR: " & strLog
      End Select

'     strLog = vbCrLf & strLog
      WScript.Echo strLog
'     Set objFSObj1 = CreateObject("Scripting.FileSystemObject")
'     Set objLogFile = objFSObj1.OpenTextFile(LOG_FILENAME, For_APPENDING, True)
'     objLogFile.WriteLine(strLog)
'     objLogFile.Close
   End If
   Err.Clear
End Function


'------------------------------------------------------
' Set printer security
'------------------------------------------------------
Function DenyGroup (strPrinter, strGroup)
   '
   ' Const DenyGroup_LogID = "DenyGroup:"
   Const DenyGroup_LogID = "<DenyGroup>:"


   ' Build the command line.
   strCMD="SetACL.exe -on """ & strPrinter & """ -ot prn -actn ace -ace ""n:EDU/" & strGroup & ";p:print;m:deny"""
   'strCMD="c:/Windows/system32/SetACL"

   Set WshShell = CreateObject("WScript.Shell")
   ' WshShell.Exec(strCMD)
   WshShell.Run strCMD, 6
 
   DenyGroup = 0  
   '
   strLogInfo = DenyGroup_LogID & "Group <" & strGroup & "> has been set DENY using printer <" _
      & strPrinter & ">."
   myLog LOGLVL_Message, strLogInfo
End Function

'------------------------------------------------------
' Make up TCp/IP Port Name from IP address.
'------------------------------------------------------
Function MakeTCPIPPortName(strIP)
   MakeTCPIPPortName = Replace("PP_" & Trim(Replace(strIP, ".", "_")), " ", "")
End Function

'------------------------------------------------------
' Build location file name
'------------------------------------------------------
Function LocationFileName(Location)
   LocationFileName = pstrLocationFile_Pre & Location & PRINTR_LIST_PST
End Function

'------------------------------------------------------
'------------------------------------------------------
Function MakeFilePath (sPath)
   MakeFilePath = sPath
   If Right(sPath, 1) <> "/" then
      MakeFilePath = sPath & "/"
   end if
End Function


'------------------------------------------------------
' List one printer Configuration.
'------------------------------------------------------
Sub ListPrinterInfo (strStaff, strPrinterName, strIP, strInfFile, strDriverModel, strPLocation, strComment)
   myLog LOGLVL_Message, "<" & strPrinterName & "> printer's configuration:"
   myLog LOGLVL_Message, vbTab & "Access Bit = <" & strStaff & ">."
   myLog LOGLVL_Message, vbTab & "IP Address = <" & strIP & ">."
   myLog LOGLVL_Message, vbTab & "  INF file = <" & strInfFile & ">."
   myLog LOGLVL_Message, vbTab & "Model Name = <" & strDriverModel & ">."
   myLog LOGLVL_Message, vbTab & "  Location = <" & strPLocation & ">."
   myLog LOGLVL_Message, vbTab & "  Comments = <" & strComment & ">."
End Sub

'------------------------------------------------------
' List all printers will be setup which get from locatoin file.
'------------------------------------------------------
Sub ListPrinterList()
   For i=0 to piPrinterNum-1
      myLog LOGLVL_Message, vbTab & "Printer configuration List(" & i & "): " & paPrinters(i)
   Next
End Sub


Sub ListGlobalSettins()
   myLog LOGLVL_Message, "Command line arguments:"
   myLog LOGLVL_Message, "   " & CONFIG_PATH & " :" & pstrConfig_Path
   myLog LOGLVL_Message, "   " & DELETE_ALL & " :" & pstrDelete_All
   myLog LOGLVL_Message, "   " & LOCATION_FROM & ":" & pLOCATIONCTRL
   myLog LOGLVL_Message, "   " & DELETE_SELF & " :" & pbDeleteSelf
   myLog LOGLVL_Message, "   " & SCHOOL_CODE & " :" & pstrSchoolCode
   myLog LOGLVL_Message, "   " & LOG_LEVEL & " :" & piLogLevel
   myLog LOGLVL_Message, "   " & REMOTE_COMPUTER & " :" & pstrConfig_Path
   myLog LOGLVL_Message, "   " & QUIET_MODE & " :" & piQuietMode
End Sub


原文链接: http://blog.csdn.net/afatgoat/article/details/6074993

转载于:https://my.oschina.net/junwong/blog/46425

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值