none
i need help, i need to change the print port from USB001 a USB002 using vscript RRS feed

  • Question

  • 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"
    strService="MpsSvc"
    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
         objPrinter.Put_
         msg = Err.Description
         On Error GoTo 0
         If msg <> "" Then
            MsgBox "Cannot share " & objPrinter.Name & ": " & msg
         End If
     End If
    Next
    
    Set objShell = CreateObject("WScript.Shell")
    strCmd = "net use lpt1 \\"& strComputerName & "\"& PrtName & " /persistent:yes /YES"
    set objExec = objShell.Exec(strCmd)
    
    Else
    
    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
        Next
    End Function

    • Edited by ARGJ Thursday, December 20, 2018 2:36 PM
    Thursday, December 20, 2018 2:31 PM

All replies

  • Try this, replace this line:

      strCmd = "net use lpt1 \\"& strComputerName & "\"& PrtName & " /persistent:yes /YES"

    with

      strCmd = "net use USB02PORT lpt1 \\"& strComputerName & "\"& PrtName & " /persistent:yes /YES"

    see where I added the usb port?

    and or try USB002PORT

    Or is this what you want?

      strCmd = "net use USB002PORT USB001PORT \\"& strComputerName & "\"& PrtName & " /persistent:yes /YES"


    Thursday, December 20, 2018 3:47 PM
  • thanks for your help i will try you recommendations.
    Friday, December 21, 2018 12:03 PM
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 1:36 AM