Home All Groups Group Topic Archive Search About

input file for changing printer config?



Author
13 Sep 2007 4:54 PM
Wanderer
Hello,

I am new to scripting and have a question.

I have a list of printers that I would like to change the printer ports they
use. I have a txt file with the list of printers.

I downloaded the 2003ResKit to use these 2 scripts.

For the portmgr.vbs, how can I use an input file for port names/IP addresses?

For the prnmgr.vbs,  how can I use an input file to map port names to the
existing printers.


Both these scripts run from the command line wit switches as you can see.
I have done this via the command line but just one to one.

I split the post into 2 because of length restrictions.

Thanks for the help!



portmgr.vbs

************
'----------------------------------------------------------------------
'
' Copyright (c) Microsoft Corporation 1998-2003
' All Rights Reserved
'
' Abstract:
'
' PortMgr.vbs - Port operation script for Windows .NET Server 2003
'
' Usage:
'
' Usage: portmgr [-adl?] [-p port] [-c server][-n port number]
'                        [-t raw|lpr|local] [-e device name]"
'
' Examples
' PortMgr -d -c \\server -p c:\temp\foo.prn
' PortMgr -a -c \\server -p IP_1.2.3.4 -e 1.2.3.4 -t raw -n 9100
'
'----------------------------------------------------------------------

option explicit

'
' Debugging trace flags, to disable debug output trace message
' change gDebugFlag to true.
'
dim   gDebugFlag
const kDebugTrace = 1
const kDebugError = 2

gDebugFlag = false

'
' Messages to be displayed if the scripting host is not cscript
'
const kMessage1 = "Please run this script using CScript."
const kMessage2 = "This can be achieved by"
const kMessage3 = "1. Using ""CScript script.vbs arguments"" or"
const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
const kMessage5 = "   using ""CScript //H:CScript //S"" and running the
script "
const kMessage6 = "   ""script.vbs arguments""."

'
' Operation action values.
'
const kActionAdd        = 0
const kActionDelete     = 1
const kActionList       = 2
const kActionUnknown    = 3
const kActionGet        = 4
const kActionSet        = 5

'
' Port Types
'
const kTcpRaw           = 1
const kTcpLPr           = 2
const kLocal            = 3
const kLocalDownLevel   = 4
const kLprMon           = 5
const kHPdlc            = 7
const kUnknown          = 8

const kStdTCPIP = "Standard TCP/IP Port"

const kErrorSuccess = 0
const KErrorFailure = 1

main

'
' Main execution starts here
'
sub main

    on error resume next

    dim iAction
    dim iRetval
    dim oParamDict

    '
    ' Abort if the host is not cscript
    '
    if not IsHostCscript() then

        call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
                          kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
                          kMessage5 & vbCRLF & kMessage6 & vbCRLF)

        wscript.quit

    end if

    set oParamDict = CreateObject("Scripting.Dictionary")

    iRetval = ParseCommandLine(iAction, oParamDict)

    if iRetval = 0 then

        select case iAction

            case kActionAdd
                iRetval = AddPort(oParamDict)

            case kActionDelete
                iRetval = DeletePort(oParamDict.Item("ServerName"),
oParamDict.Item("PortName"))

            case kActionList
                iRetval = ListPorts(oParamDict.Item("ServerName"))

            case kActionGet
                iRetVal = GetPort(oParamDict.Item("ServerName"),
oParamDict.Item("PortName"))

            case kActionSet
                iRetVal = SetPort(oParamDict)

            case else
                Usage(true)
                exit sub

        end select

    end if

end sub

'
' Delete a port
'
function DeletePort(strServer, strPort)

    on error resume next

    dim oMaster, oPort
    dim iResult

    DebugPrint kDebugTrace, "In DeletePort "
    DebugPrint kDebugTrace, "Server = " & strServer
    DebugPrint kDebugTrace, "Port   = " & strPort

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")
    set oPort   = CreateObject("Port.Port.1")

    oPort.ServerName = strServer

    oPort.PortName   = strPort

    oMaster.PortDel oPort

    if Err.Number = 0 then

        wscript.echo "Success: Deleting port """ & strPort & """ "

        iResult = kErrorSuccess

    else

        wscript.echo "Unable to deletie port """ & strPort & """, error: 0x" _
                     & Hex(Err.Number) & ". " & Err.Description

        iResult = kErrorFailure

    end if

    DeletePort = iResult

end function

'
' Add a port
'
function AddPort(oParamDict)

    on error resume next

    dim oPort
    dim oMaster
    dim iResult
    dim PortType

    DebugPrint kDebugTrace, "In AddPort "
    DebugPrint kDebugTrace, "ServerName  = " & oParamDict.Item("ServerName")
    DebugPrint kDebugTrace, "PortName    = " & oParamDict.Item("PortName")
    DebugPrint kDebugTrace, "PortType    = " & oParamDict.Item("PortType")
    DebugPrint kDebugTrace, "PortNumber  = " & oParamDict.Item("PortNumber")
    DebugPrint kDebugTrace, "QueueName   = " & oParamDict.Item("QueueName")
    DebugPrint kDebugTrace, "Index       = " & oParamDict.Item("Index")
    DebugPrint kDebugTrace, "Community   = " & oParamDict.Item("CName")
    DebugPrint kDebugTrace, "HostAddress = " & oParamDict.Item("HostAddress")

    set oPort   = CreateObject("Port.Port.1")
    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    oPort.ServerName = oParamDict.Item("ServerName")
    oPort.PortName   = oParamDict.Item("PortName")
    PortType         = oParamDict.Item("PortType")

    '
    ' Update the port object with the settings corresponding
    ' to the port type of the port to be added
    '
    select case lcase(PortType)

            case "raw"
                 oPort.PortType    = kTcpRaw
                 oPort.HostAddress = oParamDict.Item("HostAddress")
                 oPort.PortNumber  = oParamDict.Item("PortNumber")

                 if oParamDict.Exists("SNMP") then

                     oPort.SNMP = oParamDict("SNMP")

                 end if

                 if oParamDict.Exists("SNMPDeviceIndex") then

                     oPort.SNMPDeviceIndex =
oParamDict.Item("SNMPDeviceIndex")

                 end if

                 if oParamDict.Exists("CommunityName") then

                         oPort.CommunityName =
oParamDict.Item("CommunityName")

                 end if

            case "lpr"
                 oPort.PortType    = kTcpLpr
                 oPort.HostAddress = oParamDict.Item("HostAddress")
                 oPort.QueueName   = oParamDict.Item("QueueName")

                 if oParamDict.Exists("SNMP") then

                     oPort.SNMP = oParamDict("SNMP")

                 end if

                 if oParamDict.Exists("SNMPDeviceIndex") then

                     oPort.SNMPDeviceIndex =
oParamDict.Item("SNMPDeviceIndex")

                 end if

                 if oParamDict.Exists("CommunityName") then

                         oPort.CommunityName =
oParamDict.Item("CommunityName")

                 end if

                 oPort.DoubleSpool = oParamDict.Item("DoubleSpool")

            case "local"
                 oPort.PortType = kLocal

            case "localdownlevel"
                 oPort.PortType = kLocalDownLevel

            case else
                 wscript.echo "Unable to add port, error: invalid port type
""" & PortType & """ "
                 exit function
    end select

    '
    ' Try adding the port
    '
    oMaster.PortAdd oPort

    if Err.Number = kErrorSuccess then

        wscript.echo "Success: Adding port """ & oPort.PortName & """ "

        iResult = kErrorSuccess

    else

        wscript.echo "Error: Adding port """ & oPort.PortName & """, error:
0x" _
                     & Hex(Err.Number) & ". " & Err.Description

        iResult = kErrorFailure

    end if

    AddPort = iResult

end function

'
' List ports on a machine.
'
function ListPorts(strServer)

    on error resume next

    DebugPrint kDebugTrace, "In ListPorts"

    dim oMaster
    dim oPort
    dim oError
    dim iResult

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    for each oPort in oMaster.Ports(strServer)

        if Err = kErrorSuccess then

            wscript.echo ""

            wscript.echo "ServerName   " & oPort.ServerName

            wscript.echo "PortName     " & oPort.PortName

            '
            ' HPdlc and lpr mon ports don't have a MonitorName or
Description set
            '
            if oPort.MonitorName <> "" then

                wscript.echo "MonitorName  " & oPort.MonitorName

            end if

            if oPort.Description <> "" then

                wscript.echo "Description  " & oPort.Description

            end if

            if oPort.PortType = kLprMon or oPort.PortType = kHPdlc then

                wscript.echo "HostAddress  " & oPort.HostAddress

                wscript.echo "Queue        " & oPort.QueueName

                wscript.echo "PortType     " & Description(oPort.PortType)

            end if

            if oPort.Description = kStdTCPIP then

                wscript.echo "Getting extended information about the TCP port"

                iResult = GetPort(strServer, oPort.PortName)

            end if

        else

            wscript.echo "Unable to list ports, error: 0x" & Hex(Err.Number) _
                         & ". " & Err.Description

            ListPorts = kErrorFailure

            exit function

        end if

    next

    wscript.echo "Success: Lising ports"

    ListPorts = kErrorSuccess

end function

'
' Gets the configuration of a port
'
function GetPort(strServerName, strPortName)

    on error resume next

    DebugPrint kDebugTrace, "In GetPort"

    dim oPort
    dim oMaster
    dim iResult

    set oPort   = CreateObject("Port.Port.1")
    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    '
    ' Get the configuration
    '
    oMaster.PortGet strServerName, strPortName, oPort

    if Err.Number = kErrorSuccess then

        wscript.echo "PortName     " & strPortName

        wscript.echo "PortType     " & Description(oPort.PortType)

        wscript.echo "Host         " & oPort.HostAddress

        if oPort.PortType = kTcpLpr or oPort.PortType = kLprMon then

            wscript.echo "QueueName    " & oPort.QueueName

        end if

        if oPort.PortType=kTcpRaw then

            wscript.echo "PortNumber   " & CStr(oPort.PortNumber)

        end if

        if oPort.PortType = kTcpRaw or oPort.PortType = kTcpLpr then

            if oPort.SNMP then

                wscript.echo "SNMP         Enabled"

                wscript.echo "SNMP Index   " & CStr(oPort.SNMPDeviceIndex)

                wscript.echo "Community    " & oPort.CommunityName

            else

                wscript.echo "SNMP         Disabled"

            end if

        end if

        if oPort.PortType = kTcpLpr then

            if oPort.DoubleSpool then

                wscript.echo     "Byte count   Enabled"

            else

                wscript.echo     "Byte count   Disabled"

            end if

        end if

        iResult = kErrorSuccess

    else

        wscript.echo "Unable to get port configuration, error: 0x" & _
                     Hex(Err.Number) & ". " & Err.Description

        Err.Clear

        iResult = kErrorFailure

    end if

    GetPort = iResult

end function

'
' Set the configuration of a port
'
function SetPort(oParamDict)

    on error resume next

    dim oPort
    dim oMaster
    dim iResult

    set oPort   = CreateObject("Port.Port.1")
    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    '
    ' Get the configuration of the port
    '
    oMaster.PortGet oParamDict.Item("ServerName"),
oParamDict.Item("PortName"), oPort

    if Err.Number <> kErrorSuccess then

        wscript.echo "Unable to get port configuration, error: 0x" &
Hex(Err.Number) _
                     & " " & Err.Description

        iResult = kErrorFailure

        exit function

    end if

    '
    ' Update the oPort object with the settings specified by the user
    '
    if oParamDict.Item("PortType") = "raw" then

        oPort.PortType = kTcpRaw

    elseif oParamDict.Item("PortType") = "lpr" then

        oPort.PortType = kTcpLpr

    end if

    oPort.HostAddress = oParamDict.Item("HostAddress")

    oPort.PortNumber  = oParamDict.Item("PortNumber")

    if oParamDict.Exists("QueueName") then

        oPort.QueueName = oParamDict.Item("QueueName")

    end if

    if oParamDict.Exists("SNMP") then

        oPort.SNMP = oParamDict.Item("SNMP")

    end if

    oPort.SNMPDeviceIndex = oParamDict.Item("SNMPDeviceIndex")

    if oParamDict.Exists("CommunityName") then

        oPort.CommunityName = oParamDict.Item("CommunityName")

    end if

    if oParamDict.Exists("DoubleSpool") then

        oPort.DoubleSpool = oParamDict.Item("DoubleSpool")

    end if

    '
    ' Set the port
    '
    oMaster.PortSet oPort

    if Err.Number = kErrorSuccess then

        wscript.echo "Success: Updating port " & oPort.PortName

        iResult = kErrorSuccess

    else

        wscript.echo "Unable to update port settings , error: 0x" & _
                     Hex(Err.Number) & ". " & Err.Description

        iResult = kErrorFailure

    end if

    SetPort = iResult

end function

'
' Get a string description for a port type
'
function Description(value)

    on error resume next

    select case value

           case kTcpRaw

                Description = "TCP RAW"

           case kTcpLpr

                Description = "TCP LPR"

           case kLocal

                Description = "Standard Local"

           case kLocalDownLevel

                Description = "Standard Local Down Level"

           case kLprMon

                Description = "LPR Mon"

           case kHPdlc

                Description = "HP DLC"

           case kUnknown

                Description = "Unknown Port"

           case Else

                Description = "Invalid PortType"

    end select

end function

'
' Debug display helper function
'
sub DebugPrint(uFlags, strString)

    if gDebugFlag = true then

        if uFlags = kDebugTrace then

            wscript.echo "Debug: " & strString

        end if

        if uFlags = kDebugError then

            if Err <> 0 then

                wscript.echo "Debug: " & strString & " Failed with " &
Hex(Err)

            end if

        end if

    end if

end sub

'
' Parse the command line into its components
'
function ParseCommandLine(iAction, oParamDict)

    on error resume next

    DebugPrint kDebugTrace, "In ParseCommandLine"

    dim oArgs
    dim iIndex

    iAction = kActionUnknown

    set oArgs = Wscript.Arguments

    while iIndex < oArgs.Count

        select case oArgs(iIndex)

            case "-g"
                iAction = kActionGet

            case "-s"
                iAction = kActionSet

            case "-a"
                iAction = kActionAdd

            case "-d"
                iAction = kActionDelete

            case "-l"
                iAction = kActionList

            case "-2e"
                oParamDict.Add "DoubleSpool", true

            case "-2d"
                oParamDict.Add "DoubleSpool", false

            case "-c"
                iIndex = iIndex + 1
                oParamDict.Add "ServerName", oArgs(iIndex)

            case "-n"
                iIndex = iIndex + 1
                oParamDict.Add "PortNumber", oArgs(iIndex)

            case "-p"
                iIndex = iIndex + 1
                oParamDict.Add "PortName", oArgs(iIndex)

            case "-t"
                iIndex = iIndex + 1
                oParamDict.Add "PortType", oArgs(iIndex)

            case "-h"
                iIndex = iIndex + 1
                oParamDict.Add "HostAddress", oArgs(iIndex)

            case "-q"
                iIndex = iIndex + 1
                oParamDict.Add "QueueName", oArgs(iIndex)

            case "-i"
                iIndex = iIndex + 1
                oParamDict.Add "SNMPDeviceIndex", oArgs(iIndex)

            case "-y"
                iIndex = iIndex + 1
                oParamDict.Add "CommunityName", oArgs(iIndex)

            case "-me"
                oParamDict.Add "SNMP", true

            case "-md"
                oParamDict.Add "SNMP", false

            case "-?"
                Usage(True)
                exit function

            case else
                Usage(True)
                exit function

        end select

        iIndex = iIndex + 1

    wend

    if Err = kErrorSuccess then

        ParseCommandLine = kErrorSuccess

    else

        wscript.echo "Unable to parse command line, error 0x" _
                     & Hex(Err.Number) & ". " & Err.Description

        ParseCommandLine = kErrorFailure

    end if

end  function

'
' Display command usage.
'
sub Usage(bExit)

    wscript.echo "Usage: portmgr [-adlgs?] [-p port] [-c server] [-n number]"
    wscript.echo "               [-t raw|lpr|local] [-h host address] [-q
queue]"
    wscript.echo "               [-me | -md ] [-i SNMP index] [-y community]
[-2e | -2d]"
    wscript.echo "Arguments:"
    wscript.echo "-a     - add a port"
    wscript.echo "-d     - delete the specified port"
    wscript.echo "-l     - list all ports"
    wscript.echo "-g     - get configuration for a TCP port"
    wscript.echo "-s     - set configuration for a TCP port"
    wscript.echo "-p     - port name"
    wscript.echo "-c     - server name"
    wscript.echo "-n     - port number, applies to TCP RAW ports"
    wscript.echo "-t     - port type"
    wscript.echo "-h     - IP address of the device"
    wscript.echo "-q     - queue name, applies to TCP LPR ports"
    wscript.echo "-m     - SNMP type. [e] enalbe, [d] disable"
    wscript.echo "-i     - SNMP index, if SNMP is enabled"
    wscript.echo "-y     - community name, if SNMP is enabled"
    wscript.echo "-2     - double spool, applies to TCP LPR ports.[e]
enalbe, [d] disable"
    wscript.echo "-?     - display command usage"
    wscript.echo ""
    wscript.echo "Examples:"
    wscript.echo "portmgr -l -c \\server"
    wscript.echo "portmgr -d -c \\server -p c:\temp\foo.prn"
    wscript.echo "portmgr -a -c \\server -p test -t local"
    wscript.echo "portmgr -a -c \\server -p IP_1.2.3.4 -h 1.2.3.4 -t raw -n
9100"
    wscript.echo "portmgr -s -c \\server -p IP_1.2.3.4 -me -y public -i 1 -n
9100"

    if bExit then

        wscript.quit(1)

    end if

end sub

'
' Determines which program is used to run this script.
' Returns true if the script host is cscript.exe
'
function IsHostCscript()

    on error resume next

    dim strFullName
    dim strCommand
    dim i, j
    dim bReturn

    bReturn = false

    strFullName = WScript.FullName

    i = InStr(1, strFullName, ".exe", 1)

    if i <> 0 then

        j = InStrRev(strFullName, "\", i, 1)

        if j <> 0 then

            strCommand = Mid(strFullName, j+1, i-j-1)

            if LCase(strCommand) = "cscript" then

                bReturn = true

            end if

        end if

    end if

    if Err <> 0 then

        call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " &
Err.Description _
                          & ". " & vbCRLF & "The scripting host could not be
determined.")

    end if

    IsHostCscript = bReturn

end function

Author
13 Sep 2007 4:58 PM
Wanderer
'----------------------------------------------------------------------
'
' Copyright (c) Microsoft Corporation 1998-2003
' All Rights Reserved
'
' Abstract:
'
' prnmgr.vbs - printer script for Windows .NET Server 2003
'
' Usage:
' prnmgr [-adl?][c] [-c server][-b printer][-m driver]
'                   [-l driver path][-r port][-f file]
' Examples:
' prnmgr -l -c \\server
' prnmgr.vbs -a -b "printer" -m "driver" -r "lpt1:"
' prnmgr.vbs -a -b "printer" -m "driver" -r "lpt1:"
' prnmgr.vbs -a -b "printer" -c \\server
' prnmgr.vbs -ac -b "\\server\printer"
' prnmgr.vbs -dc -b "\\server\printer"
'
'----------------------------------------------------------------------

option explicit

'
' Debugging trace flags, to enable debug output trace message
' change gDebugFlag to true.
'
const kDebugTrace = 1
const kDebugError = 2
dim   gDebugFlag

gDebugFlag = false

'
' Messages to be displayed if the scripting host is not cscript
'
const kMessage1 = "Please run this script using CScript."
const kMessage2 = "This can be achieved by"
const kMessage3 = "1. Using ""CScript script.vbs arguments"" or"
const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
const kMessage5 = "   using ""CScript //H:CScript //S"" and running the
script "
const kMessage6 = "   ""script.vbs arguments""."

'
' Operation action values.
'
const kActionUnknown     = 0
const kActionAdd         = 1
const kActionAddConn     = 2
const kActionDel         = 3
const kActionDelConn     = 4
const kActionList        = 5
const kActionDelAll      = 6
const kActionDelConnAll  = 7

const kErrorSuccess      = 0
const KErrorFailure      = 1

const kPrinterNetwork    = 16
const kLocalPrinterFlag  = 64

main

'
' Main execution starts here
'
sub main

    dim iAction
    dim iRetval
    dim strServer
    dim strPrinter
    dim strDriverPath
    dim strDriver
    dim strPort
    dim strInfFile

    '
    ' Abort if the host is not cscript
    '
    if not IsHostCscript() then

        call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
                          kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
                          kMessage5 & vbCRLF & kMessage6 & vbCRLF)

        wscript.quit

    end if

    iRetval = ParseCommandLine(iAction, strServer, strPrinter, strDriver,
strDriverPath, strPort, strInfFile)

    if iRetval = kErrorSuccess then

        select case iAction

            case kActionAdd
                 iRetval = AddPrinter(strServer, strPrinter, strDriver,
strDriverPath, strPort, strInfFile)

            case kActionAddConn
                 iRetval = AddPrinterConnection(strPrinter)

            case kActionDel
                 iRetval = DelPrinter(strServer, strPrinter)

            case kActionDelConn
                 iRetval = DelPrinterConnection(strPrinter)

            case kActionList
                 iRetval = ListPrinters(strServer)

            case kActionDelAll
                 iRetval = DelPrinterAll(strServer)

            case kActionDelConnAll
                 iRetval = DelPrinterConnectionAll()

            case else
                 Usage(true)
                 exit sub

        end select

    end if

end sub

'
' Add a printer
'
function AddPrinter(strServer, strPrinter, strDriver, strDriverPath,
strPort, strInfFile)

    on error resume next

    DebugPrint kDebugTrace, "In AddPrinter"
    DebugPrint kDebugTrace, "Server      "  & strServer
    DebugPrint kDebugTrace, "Printer     "  & strPrinter
    DebugPrint kDebugTrace, "Driver      "  & strDriver
    DebugPrint kDebugTrace, "DrivatePath "  & strDriverPath
    DebugPrint kDebugTrace, "Port        "  & strPort
    DebugPrint kDebugTrace, "InfFile     "  & strInfFile

    dim oMaster
    dim oPrinter
    dim iRetval

    set oMaster  = CreateObject("PrintMaster.PrintMaster.1")
    set oPrinter = CreateObject("Printer.Printer.1")

    oPrinter.ServerName  = strServer

    oPrinter.DriverPath  = strDriverPath

    oPrinter.DriverName  = strDriver

    oPrinter.PortName    = strPort

    oPrinter.PrinterName = strPrinter

    oPrinter.InfFile     = strInfFile

    oMaster.PrinterAdd oPrinter

    if Err.Number = kErrorSuccess then

        wscript.echo "Printer """ & strPrinter & """ added"

        iRetval = kErrorSuccess

    else

        wscript.echo "Unable to add printer """ & strPrinter & """, error:
0x" _
                     & Hex(Err.Number) & ". " & Err.Description

       iRetval = kErrorFailure

    end if

    AddPrinter = iRetval

end function

'
' Add a printer connection
'
function AddPrinterConnection(strPrinter)

    on error resume next

    DebugPrint kDebugTrace, "In AddPrinterConnection"

    dim oMaster
    dim iRetval

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    oMaster.PrinterConnectionAdd strPrinter

    if Err.Number = kErrorSuccess then

        wscript.echo "Printer connection """ & strPrinter & """ added"

        iRetval = kErrorSuccess

    else

        wscript.echo "Unable to add printer connection, error: 0x" &
Hex(Err.Number) _
                     & ". " & Err.Description

        iRetval = kErrorFailure

    end if

    AddPrinterConnection = iRetval

end function

'
' Delete a printer connection
'
function DelPrinterConnection(strPrinter)

    on error resume next

    DebugPrint kDebugTrace, "In DelPrinterConnection"

    dim oMaster
    dim iRetval

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    oMaster.PrinterConnectionDel strPrinter

    if Err.Number = kErrorSuccess then

        wscript.echo "Deleted printer connection " & strPrinter

        iRetval = kErrorSuccess

    else

        wscript.echo "Unable to delete printer connection " & strPrinter &
", error: 0x"_
                     & Hex(Err.Number) & ". " & Err.Description

        iRetval = kErrorFailure

    end if

    DelPrinterConnection = iRetval

end function

'
' Delete a printer
'
function DelPrinter(strServer, strPrinter)

    on error resume next

    DebugPrint kDebugTrace, "In DelPrinter"

    dim oMaster
    dim oPrinter
    dim iRetval

    set oMaster  = CreateObject("PrintMaster.PrintMaster.1")
    set oPrinter = CreateObject("Printer.Printer.1")

    oPrinter.ServerName  = strServer
    oPrinter.PrinterName = strPrinter

    oMaster.PrinterDel oPrinter

    if Err.Number = kErrorSuccess then

        wscript.echo "Printer """ & strPrinter & """ deleted"

        iRetval = kErrorSuccess

    else

        wscript.echo "Unable to delete printer """ & strPrinter &  """,
error: 0x" _
                     & Hex(Err.Number) & ". " & Err.Description

        iRetval = kErrorFailure

    end if

    DelPrinter = iRetval

end function

'
' List the printers
'
function ListPrinters(strServer)

    on error resume next

    DebugPrint kDebugTrace, "In ListPrinter"

    dim oMaster
    dim oPrinter
    dim iRetval

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    for each oPrinter in oMaster.Printers(strServer)

        if Err.Number = kErrorSuccess then

            wscript.echo ""
            wscript.echo "ServerName      : " & oPrinter.ServerName
            wscript.echo "PrinterName     : " & oPrinter.PrinterName
            wscript.echo "ShareName       : " & oPrinter.ShareName
            wscript.echo "DriverName      : " & oPrinter.DriverName
            wscript.echo "PortName        : " & oPrinter.PortName
            wscript.echo "Comment         : " & oPrinter.Comment
            wscript.echo "Location        : " & oPrinter.Location
            wscript.echo "SepFile         : " & oPrinter.SepFile
            wscript.echo "PrintProcesor   : " & oPrinter.PrintProcessor
            wscript.echo "DataType        : " & oPrinter.DataType
            wscript.echo "Parameters      : " & oPrinter.Parameters
            wscript.echo "Attributes      : " & CSTR(oPrinter.Attributes)
            wscript.echo "Priority        : " & CSTR(oPrinter.Priority)
            wscript.echo "DefaultPriority : " & CStr(oPrinter.DefaultPriority)
            wscript.echo "StartTime       : " & CStr(oPrinter.StartTime)
            wscript.echo "UntilTime       : " & CStr(oPrinter.UntilTime)
            wscript.echo "Status          : " & CStr(oPrinter.Status)
            wscript.echo "Jobc Count      : " & CStr(oPrinter.Jobs)
            wscript.echo "AveragePPM      : " & CStr(oPrinter.AveragePPM)

            Err.Clear

        else

            wscript.echo "Unable to list printers, error: 0x" & _
                         Hex(Err.Number) & ". " & Err.Description

            ListPrinters = kErrorFailure

            exit function

        end if

    next

    wscript.echo "Success listing printers"

    ListPrinters = kErrorSuccess

end function

'
' Delete all local printers
'
function DelPrinterAll(strServer)

    on error resume next

    DebugPrint kDebugTrace, "In DelPrinterAll"

    dim oMaster
    dim oPrinter
    dim iTotal
    dim iCount

    '
    ' Number of local printers found
    '
    iTotal = 0

    '
    ' Number of local printes deleted
    '
    iCount = 0

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    for each oPrinter in oMaster.Printers(strServer)

        if Err.Number = kErrorSuccess then

            '
            ' If strServer is not empty, the enumeration will contain
            ' only local printers on that server. The reason is that
            ' connections are per user resources
            '
            if strServer = "" then

                '
                ' Connections are enumerated in this case
                '
                if (oPrinter.Attributes and kLocalPrinterFlag) =
kLocalPrinterFlag then

                    iTotal = iTotal + 1

                    oMaster.PrinterPurge strServer, oPrinter.PrinterName

                    '
                    ' In some cases, we can delete a printer, but not purge it
                    ' We need to clear the error
                    '
                    Err.Clear

                    if DelPrinter(strServer, oPrinter.PrinterName) =
kErrorSuccess then

                        iCount = iCount + 1

                    end if

                end if

            else

                '
                ' Only local printers on the server are enumerated
                '
                iTotal = iTotal + 1

                oMaster.PrinterPurge strServer, oPrinter.PrinterName

                '
                ' In some cases, we can delete a printer, but not purge it
                ' We need to clear the error
                '
                Err.Clear

                if DelPrinter(strServer, oPrinter.PrinterName) =
kErrorSuccess then

                    iCount = iCount + 1

                end if

            end if

            Err.Clear

        else

            wscript.echo "Unable to enumerate printers on server, error: 0x" _
                         & Hex(Err.Number) & ". " & Err.Description

            DelPrinterAll = kErrorFailure

            exit function

        end if

    next

    wscript.echo "Number of local printers found " & iTotal & ". Printers
deleted " & iCount

    DelPrinterAll = kErrorSuccess

end function

'
' Delete all printer connections
'
function DelPrinterConnectionAll()

    on error resume next

    DebugPrint kDebugTrace, "In DelPrinterConnectionAll"

    dim oMaster
    dim oPrinter
    dim iTotal
    dim iCount

    '
    ' Total number of connections found
    '
    iTotal = 0

    '
    ' Total number of connections deleted
    '
    iCount = 0

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")

    for each oPrinter in oMaster.Printers

        if Err.Number = kErrorSuccess then

            '
            ' Test if the printer is not local
            '
            if (oPrinter.Attributes and kLocalPrinterFlag) <>
kLocalPrinterFlag then

                iTotal = iTotal + 1

                if DelPrinterConnection(oPrinter.PrinterName) =
kErrorSuccess then

                    iCount = iCount + 1

                end if

                Err.Clear

            end if

        else

            wscript.echo "Unable to enumerate printers on server, error: 0x" _
                         & Hex(Err.Number) & ". " & Err.Description

            DelPrinterConnectionAll = kErrorFailure

            exit function

        end if

    next

    wscript.echo "Number of connections found " & iTotal & ". Connections
deleted " & iCount

    DelPrinterConnectionAll = kErrorSuccess

end function

'
' Debug display helper function
'
sub DebugPrint(uFlags, strString)

    if gDebugFlag = true then

        if uFlags = kDebugTrace then

            wscript.echo "Debug: " & strString

        end if

        if uFlags = kDebugError then

            if Err <> 0 then

                wscript.echo "Debug: " & strString & " Failed with " &
Hex(Err)

            end if

        end if

    end if

end sub

'
' Parse the command line into it's components
'
function ParseCommandLine(iAction, strServer, strPrinter, strDriver,
strDriverPath, strPort, strInfFile)

    on error resume next

    DebugPrint kDebugTrace, "In the ParseCommandLine"

    dim oArgs
    dim iIndex

    iAction = kActionUnknown
    iIndex  = 0

    set oArgs = wscript.Arguments

    while iIndex < oArgs.Count

        select case oArgs(iIndex)

            case "-a"
                iAction = kActionAdd

            case "-ac"
                iAction = kActionAddConn

            case "-d"
                iAction = kActionDel

            case "-dc"
                iAction = kActionDelConn

            case "-l"
                iAction = kActionList

            case "-x"
                iAction = kActionDelAll

            case "-xc"
                iAction = kActionDelConnAll

            case "-c"
                iIndex = iIndex + 1
                strServer = oArgs(iIndex)

            case "-b"
                iIndex = iIndex + 1
                strPrinter = oArgs(iIndex)

            case "-f"
                iIndex = iIndex + 1
                strInfFile = oArgs(iIndex)

            case "-m"
                iIndex = iIndex + 1
                strDriver = oArgs(iIndex)

            case "-r"
                iIndex = iIndex + 1
                strPort = oArgs(iIndex)

            case "-p"
                iIndex = iIndex + 1
                strDriverPath = oArgs(iIndex)

            case "-?"
                Usage(true)
                exit function

            case else
                Usage(true)
                exit function

        end select

        iIndex = iIndex + 1

    wend

    if Err = kErrorSuccess then

        ParseCommandLine = kErrorSuccess

    else

        wscript.echo "Unable to parse command line, error 0x" & _
                     Hex(Err.Number) & ". " & Err.Description

        ParseCommandLine = kErrorFailure

    end if

end  function

'
' Display command usage.
'
sub Usage(bExit)

    wscript.echo "Usage: prnmgr [-adl?][c] [-c server][-b printer][-m driver
model]"
    wscript.echo "              [-p driver path][-r port][-f file]"
    wscript.echo "Arguments:"
    wscript.echo "-a     - add local printer"
    wscript.echo "-ac    - add printer connection"
    wscript.echo "-d     - delete local printer"
    wscript.echo "-dc    - delete printer connection"
    wscript.echo "-f     - inf file"
    wscript.echo "-l     - list printers"
    wscript.echo "-c     - server name"
    wscript.echo "-b     - printer name"
    wscript.echo "-m     - driver model"
    wscript.echo "-r     - port name"
    wscript.echo "-p     - driver path can be local or network path i.e. a:\
or \\server\share"
    wscript.echo "-x     - delete all local printers"
    wscript.echo "-xc    - delete all printer connections, cannot be used
with the -c option"
    wscript.echo "-?     - display command usage"
    wscript.echo ""
    wscript.echo "Examples:"
    wscript.echo "prnmgr -l -c \\server"
    wscript.echo "prnmgr -a -b ""printer"" -m ""driver"" -r ""lpt1:"""
    wscript.echo "prnmgr -d -b ""printer"" -c \\server"
    wscript.echo "prnmgr -ac -b ""\\server\printer"""
    wscript.echo "prnmgr -dc -b ""\\server\printer"""
    wscript.echo "prnmgr -x"
    wscript.echo "prnmgr -x -c \\server"
    wscript.echo "prnmgr -xc"

    if bExit then

        wscript.quit(1)

    end if

end sub

'
' Determines which program is used to run this script.
' Returns true if the script host is cscript.exe
'
function IsHostCscript()

    on error resume next

    dim strFullName
    dim strCommand
    dim i, j
    dim bReturn

    bReturn = false

    strFullName = WScript.FullName

    i = InStr(1, strFullName, ".exe", 1)

    if i <> 0 then

        j = InStrRev(strFullName, "\", i, 1)

        if j <> 0 then

            strCommand = Mid(strFullName, j+1, i-j-1)

            if LCase(strCommand) = "cscript" then

                bReturn = true

            end if

        end if

    end if

    if Err <> 0 then

        call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " &
Err.Description _
                          & ". " & vbCRLF & "The scripting host could not be
determined.")

    end if

    IsHostCscript = bReturn

end function



Show quote
"Wanderer" wrote:

> Hello,
>
> I am new to scripting and have a question.
>
> I have a list of printers that I would like to change the printer ports they
> use. I have a txt file with the list of printers.
>
> I downloaded the 2003ResKit to use these 2 scripts.
>
> For the portmgr.vbs, how can I use an input file for port names/IP addresses?
>
> For the prnmgr.vbs,  how can I use an input file to map port names to the
> existing printers.
>
>
> Both these scripts run from the command line wit switches as you can see.
> I have done this via the command line but just one to one.
>
> I split the post into 2 because of length restrictions.
>
> Thanks for the help!
>
>
>
> portmgr.vbs
>
> ************
> '----------------------------------------------------------------------
> '
> ' Copyright (c) Microsoft Corporation 1998-2003
> ' All Rights Reserved
> '
> ' Abstract:
> '
> ' PortMgr.vbs - Port operation script for Windows .NET Server 2003
> '
> ' Usage:
> '
> ' Usage: portmgr [-adl?] [-p port] [-c server][-n port number]
> '                        [-t raw|lpr|local] [-e device name]"
> '
> ' Examples
> ' PortMgr -d -c \\server -p c:\temp\foo.prn
> ' PortMgr -a -c \\server -p IP_1.2.3.4 -e 1.2.3.4 -t raw -n 9100
> '
> '----------------------------------------------------------------------
>
> option explicit
>
> '
> ' Debugging trace flags, to disable debug output trace message
> ' change gDebugFlag to true.
> '
> dim   gDebugFlag
> const kDebugTrace = 1
> const kDebugError = 2
>
> gDebugFlag = false
>
> '
> ' Messages to be displayed if the scripting host is not cscript
> '
> const kMessage1 = "Please run this script using CScript."
> const kMessage2 = "This can be achieved by"
> const kMessage3 = "1. Using ""CScript script.vbs arguments"" or"
> const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
> const kMessage5 = "   using ""CScript //H:CScript //S"" and running the
> script "
> const kMessage6 = "   ""script.vbs arguments""."
>
> '
> ' Operation action values.
> '
> const kActionAdd        = 0
> const kActionDelete     = 1
> const kActionList       = 2
> const kActionUnknown    = 3
> const kActionGet        = 4
> const kActionSet        = 5
>
> '
> ' Port Types
> '
> const kTcpRaw           = 1
> const kTcpLPr           = 2
> const kLocal            = 3
> const kLocalDownLevel   = 4
> const kLprMon           = 5
> const kHPdlc            = 7
> const kUnknown          = 8
>
> const kStdTCPIP = "Standard TCP/IP Port"
>
> const kErrorSuccess = 0
> const KErrorFailure = 1
>
> main
>
> '
> ' Main execution starts here
> '
> sub main
>
>     on error resume next
>
>     dim iAction
>     dim iRetval
>     dim oParamDict
>
>     '
>     ' Abort if the host is not cscript
>     '
>     if not IsHostCscript() then
>
>         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
>                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
>                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
>
>         wscript.quit
>
>     end if
>
>     set oParamDict = CreateObject("Scripting.Dictionary")
>
>     iRetval = ParseCommandLine(iAction, oParamDict)
>
>     if iRetval = 0 then
>
>         select case iAction
>
>             case kActionAdd
>                 iRetval = AddPort(oParamDict)
>
>             case kActionDelete
>                 iRetval = DeletePort(oParamDict.Item("ServerName"),
> oParamDict.Item("PortName"))
>
>             case kActionList
>                 iRetval = ListPorts(oParamDict.Item("ServerName"))
>
>             case kActionGet
>                 iRetVal = GetPort(oParamDict.Item("ServerName"),
> oParamDict.Item("PortName"))
>
>             case kActionSet
>                 iRetVal = SetPort(oParamDict)
>
>             case else
>                 Usage(true)
>                 exit sub
>
>         end select
>
>     end if
>
> end sub
>
> '
> ' Delete a port
> '
> function DeletePort(strServer, strPort)
>
>     on error resume next
>
>     dim oMaster, oPort
>     dim iResult
>
>     DebugPrint kDebugTrace, "In DeletePort "
>     DebugPrint kDebugTrace, "Server = " & strServer
>     DebugPrint kDebugTrace, "Port   = " & strPort
>
>     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
>     set oPort   = CreateObject("Port.Port.1")
>
>     oPort.ServerName = strServer
>
>     oPort.PortName   = strPort
>
>     oMaster.PortDel oPort
>
>     if Err.Number = 0 then
>
>         wscript.echo "Success: Deleting port """ & strPort & """ "
>
>         iResult = kErrorSuccess
>
>     else
>
>         wscript.echo "Unable to deletie port """ & strPort & """, error: 0x" _
>                      & Hex(Err.Number) & ". " & Err.Description
>
>         iResult = kErrorFailure
>
>     end if
>
>     DeletePort = iResult
>
> end function
>
> '
> ' Add a port
> '
> function AddPort(oParamDict)
>
>     on error resume next
>
>     dim oPort
>     dim oMaster
>     dim iResult
>     dim PortType
>
>     DebugPrint kDebugTrace, "In AddPort "
>     DebugPrint kDebugTrace, "ServerName  = " & oParamDict.Item("ServerName")
>     DebugPrint kDebugTrace, "PortName    = " & oParamDict.Item("PortName")
>     DebugPrint kDebugTrace, "PortType    = " & oParamDict.Item("PortType")
>     DebugPrint kDebugTrace, "PortNumber  = " & oParamDict.Item("PortNumber")
>     DebugPrint kDebugTrace, "QueueName   = " & oParamDict.Item("QueueName")
>     DebugPrint kDebugTrace, "Index       = " & oParamDict.Item("Index")
>     DebugPrint kDebugTrace, "Community   = " & oParamDict.Item("CName")
>     DebugPrint kDebugTrace, "HostAddress = " & oParamDict.Item("HostAddress")
>
>     set oPort   = CreateObject("Port.Port.1")
>     set oMaster = CreateObject("PrintMaster.PrintMaster.1")
>
>     oPort.ServerName = oParamDict.Item("ServerName")
>     oPort.PortName   = oParamDict.Item("PortName")
>     PortType         = oParamDict.Item("PortType")
>
>     '
>     ' Update the port object with the settings corresponding
>     ' to the port type of the port to be added
>     '
>     select case lcase(PortType)
>
>             case "raw"
>                  oPort.PortType    = kTcpRaw
>                  oPort.HostAddress = oParamDict.Item("HostAddress")
>                  oPort.PortNumber  = oParamDict.Item("PortNumber")
>
>                  if oParamDict.Exists("SNMP") then
>
>                      oPort.SNMP = oParamDict("SNMP")
>
>                  end if
>
>                  if oParamDict.Exists("SNMPDeviceIndex") then
>
>                      oPort.SNMPDeviceIndex =
> oParamDict.Item("SNMPDeviceIndex")
>
>                  end if
>
>                  if oParamDict.Exists("CommunityName") then
>
>                          oPort.CommunityName =
> oParamDict.Item("CommunityName")
>
>                  end if
>
>             case "lpr"
>                  oPort.PortType    = kTcpLpr
>                  oPort.HostAddress = oParamDict.Item("HostAddress")
>                  oPort.QueueName   = oParamDict.Item("QueueName")
>
>                  if oParamDict.Exists("SNMP") then
>
>                      oPort.SNMP = oParamDict("SNMP")
>
>                  end if
>
>                  if oParamDict.Exists("SNMPDeviceIndex") then
>
>                      oPort.SNMPDeviceIndex =
> oParamDict.Item("SNMPDeviceIndex")
>
>                  end if
>
>                  if oParamDict.Exists("CommunityName") then
>
>                          oPort.CommunityName =
> oParamDict.Item("CommunityName")
>
>                  end if
>
>                  oPort.DoubleSpool = oParamDict.Item("DoubleSpool")
>
>             case "local"
>                  oPort.PortType = kLocal
>
>             case "localdownlevel"
>                  oPort.PortType = kLocalDownLevel
>
>             case else
>                  wscript.echo "Unable to add port, error: invalid port type
> """ & PortType & """ "
>                  exit function
>     end select
>
>     '
>     ' Try adding the port

AddThis Social Bookmark Button