i have this script for share and map printers but i need to change the print port! RRS feed

  • General discussion

  • hi, some body knows how to change print port using vbscript i want to change the print port from USB001 to USB002 when users performance logig, i have sevarals day looking this information on internet but i can't find it.

    This is my script for sharing the print and map to lpt1 this for a old system

    Dim objWMIService, objService
    Dim strService,strComputer, PrintPortType, PrtName
    PrtName = "TMUCASH"
    PrintPortType = "USB"
    strComputer = "."
    Set wshShell = CreateObject( "WScript.Shell" )
    strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    IF InStr(strComputerName,"CASH") THEN
    IF printerExists("Generic / Text Only") THEN
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer Where Name = 'Generic / Text Only' AND PortName LIKE '%USB%'")
    For Each objPrinter in colInstalledPrinters
     If objPrinter.Shared = False Then
         objPrinter.Shared = True
         objPrinter.ShareName = PrtName 
         On Error Resume Next
         msg = Err.Description
         On Error GoTo 0
         If msg <> "" Then
            MsgBox "Cannot share " & objPrinter.Name & ": " & msg
         End If
     End If
    Set objShell = CreateObject("WScript.Shell")
    strCmd = "net use lpt1 \\"& strComputerName & "\"& PrtName & " /persistent:yes /YES"
    set objExec = objShell.Exec(strCmd)
    END IF
    END IF
    Function printerExists(str)
        printerExists = False
        Dim objWMIService
        Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Dim colPrinters
        Set colPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where Name = 'Generic / Text Only' AND PortName LIKE '%USB%'")
        Dim objPrinter
        For Each objPrinter In colPrinters
            If objPrinter.Name = str Then
                printerExists = True
                Exit For
            End If
    End Function

    • Edited by ARGJ Thursday, December 20, 2018 2:36 PM
    • Changed type Bill_Stewart Friday, March 15, 2019 4:49 PM
    • Moved by Bill_Stewart Friday, March 15, 2019 4:50 PM This is not "fix/debug/rewrite my script for me" forum
    Thursday, December 20, 2018 2:35 PM

All replies

  • The script you posted has nothing to do with your question.

    Please read the following post first (from right at the top of this forum):

    This forum is for scripting questions rather than script requests

    -- Bill Stewart [Bill_Stewart]

    Thursday, December 20, 2018 4:31 PM
  • The forum is a physical or virtual place (through the Internet, or through a talk) that is used to meet, exchange ideas and opinions on various topics of common interest. They can be classified in public forums, private forums and protected forums. The important thing about participating in them is that you can listen to the different perspectives on a topic, while at the same time we can intervene enriching the debate with our point of view and reaching an agreement. It is essentially an oral technique performed in groups.

    in a few words they should close this forum :)
    Thursday, December 20, 2018 6:12 PM
  • Unfortunately the script is very old and was not designed to do what you ask.  Since you do not know scripting I suggest looking in the Gallery for scripts that do what you ask.  You can also use PowerShell to find and change printers.

    Writing custom scripts is beyond the scope of technical forums.  We can point you in the correct direction but you must write your script.

    for PowerShell just type the following at a prompt"

    help printer

    Get the full help for any help topic:

    help Get-Printer -Full


    Thursday, December 20, 2018 10:08 PM