Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

Recommended Posts

Hi Guys,

I've written a batch file that should delete all old printers on a computer, then import a 'ports.reg' registry file to add the correct IP ports to that machine, then it is meant to install the new printers.

The problem is that after it has added the correct ports, it tries to add the new printers but I get the very :unsure: helpful error message "Operation Could not be completed"....

here is my script, and i've included the ports.reg file too:


rem delete old printers

wmic PRINTER delete

rem stop printer spooler

Net stop Spooler
rem reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" /f /va
%windir%\regedit /S ports.reg
net start spooler

pause

rem add printer
RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b "HP Officejet Pro K5400 Series" /f "\\juniorfs\NETLOGON\Printers\HP-K5400\hpwk540a.inf" /r "\\juniorfs\JSchoolVillach" /m "Villach Room"

Ports.Reg:


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports]
"COM1:"="9600,n,8,1"
"COM2:"="9600,n,8,1"
"COM3:"="9600,n,8,1"
"COM4:"="9600,n,8,1"
"FILE:"=""
"LPT1:"=""
"LPT2:"=""
"LPT3:"=""
@=""
"Ne00:"=""
"Ne01:"=""
"Ne02:"=""
"Ne03:"=""
"\\\\juniorfs\\JSchoolFL1"=""
"\\\\juniorfs\\JSchoolVillach"=""

Any help would be gratefully received!

Thanks a lot,

-Jon

Link to post
Share on other sites

I would think that the most likely problem is with your installation inf file.

Likely problems:
  • You cannot use a network path for
    /f

  • The network location specified in
    /f
    cannot be found.

  • There is an error generated from the inf file.

As far as the batch file goes, I'd personally prefer to see you keep everything in there instead of using a reg file merge, and possibly if using WMIC, sticking with it instead of using NET.EXE as well, e.g:

@ECHO OFF

REM Delete old printers except for Microsofts XPS Document Printer
WMIC PRINTER WHERE "PortName <> 'XPSPort:'" DELETE

REM Stop printer spooler if started
WMIC SERVICE WHERE "Name='Spooler' AND State='Running'" CALL StopService

REM Registry data changes
SETLOCAL
SET "KEY=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"

REM Delete old registry data
REM REG DELETE "%KEY%" /VA /F

REM Add new registry data
REG ADD "%KEY%" /VE /F
REG ADD "%KEY%" /V COM1: /D "9600,n,8,1" /F
REG ADD "%KEY%" /V COM2: /D "9600,n,8,1" /F
REG ADD "%KEY%" /V COM3: /D "9600,n,8,1" /F
REG ADD "%KEY%" /V COM4: /D "9600,n,8,1" /F
REG ADD "%KEY%" /V FILE: /F
REG ADD "%KEY%" /V LPT1: /F
REG ADD "%KEY%" /V LPT2: /F
REG ADD "%KEY%" /V LPT3: /F
REG ADD "%KEY%" /V Ne00: /F
REG ADD "%KEY%" /V Ne01: /F
REG ADD "%KEY%" /V Ne02: /F
REG ADD "%KEY%" /V Ne03: /F
REG ADD "%KEY%" /V \\juniorfs\JSchoolFL1 /F
REG ADD "%KEY%" /V \\juniorfs\JSchoolVillach /F

ENDLOCAL

REM Start printer spooler
WMIC SERVICE WHERE "Name='Spooler'" CALL StartService

PAUSE...

Link to post
Share on other sites
  • 2 weeks later...

CMD is a bit clunky for managing printers with imo...

probably better if you switch to vbs.

My machines are in OU's on AD, so say:

SALES

- salesprn1

- salesprn2

- salesprn3

and so on. if a user logs onto a machine and its in the SALES\salesprn1\ OU on AD, then salesprn1 printer would be their default printer, i add the other printers in the same department as well just so they have the option of printing to another one if they choose. Users then get to move around and the nearest printer to them is always their default printer.



'IMPORTANT. DO NOT user Share names to install printers.
'Use the full printer name as it appears on the servers printer menu.
'Enter "none" into printers array to install no printers.
'Do not leave array empty

'IT COMPUTERS
If isMemberOu("OU=IT,OU=Admin workstations") Then
printers = Array("\\GAM012\IT_COLOUR","\\GAM012\IT","\\GAM012\MARKS","\\GAM012\UNDERW2","\\GAM012\UNDERW3","\\GAM012\LOANS1","\\GAM012\LOANS2","\\GAM012\LOANS3","\\GAM012\FAXLESS1","\\GAM012\COLLECTIONS1","\\GAM012\COLLECTIONS2")
defaultPrinter = "\\GAM012\IT"
installPrinters = True

'LOANS1 COMPUTERS
elseIf isMemberOu("OU=LOANS1,OU=Loans,OU=Workstations") Then
printers = Array("\\CYC010\LOANS1","\\CYC010\LOANS2","\\CYC011\LOANS3","\\CYC010\FAXLESS1")
defaultPrinter = "\\CYC010\LOANS1"
installPrinters = True

'LOANS2 COMPUTERS
elseIf isMemberOu("OU=LOANS2,OU=Loans,OU=Workstations") Then
printers = Array("\\CYC010\LOANS1","\\CYC010\LOANS2","\\CYC011\LOANS3","\\CYC010\FAXLESS1")
defaultPrinter = "\\CYC010\LOANS2"
installPrinters = True


'ADMIN OFFICE COMPUTERS
elseIf isMemberOu("OU=ADMIN,OU=Managerial,OU=workstations") Then
printers = Array("\\GAM012\IT_COLOUR","\\GAM013\IT","\\CYC010\DALES","\\CYC010\MARKS")
defaultPrinter = "\\CYC010\MARKS"
installPrinters = True

'Define printers for specific machines ( we use if for laptops)
ELSEIf isMemberOu("ou=laptops") Then
' get computer name
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName

' if computer name matches string then install some printers
If lcase(strComputer) = lcase("laptop1") Then
printers = Array("\\server\printer 1", "\\server\printer 2")
defaultPrinter = "\\pabserv\Room 37" ' Define the default printer
installPrinters = True ' Allows the install routines to run

ElseIf lcase(strComputer) = lcase("laptop2") Then
printers = Array("\\server\printer 1", "\\server\printer 2")
defaultPrinter = "\\pabserv\Room 37" ' Define the default printer
installPrinters = True ' Allows the install routines to run
End If
END IF


' **** DO NOT EDIT BELOW THIS LINE ***
On Error Resume Next

IF installPrinters Then
'install printers
Set objWSHNetwork = CreateObject("WScript.Network")
Set WS_PRINTERS = objWSHNetwork.EnumPrinterConnections
Set WshShell = WScript.CreateObject("WScript.Shell")
FOR EACH printer in printers
if LCase(printer) <> "none" Then
objWSHNetwork.AddWindowsPrinterConnection(printer)
END IF
NEXT

'set Default Printer
if LCase(defaultPrinter) <> "none" Then
objWSHNetwork.SetDefaultPrinter defaultPrinter
END IF

Set WS_PRINTERS = Nothing
Set WS_NET = Nothing
Set WshShell = Nothing
END IF


' Determine OU Membership of computer
Private Function isMemberOu(checkOu)
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerDN = objSysInfo.ComputerName
intIndexStart = InStr(LCase(strComputerDN), "ou=")
intIndexLength = InStr(LCase(strComputerDN), ",dc=") - intIndexStart
isMemberOu = false
If intIndexStart > 0 Then
compOu = LCase(Mid(strComputerDN, intIndexStart, intIndexLength))
End If
If InStr(1, compOu, checkOu, 1) Then
isMemberOu = true
END IF
Set objSysInfo = nothing
End Function

As usual, theres probably a better way of doing it - but it works.

I trimmed the script example down to make it easier to follow, obviously you would need to change it to suit your own AD structure and printer paths etc.

Edited by FthrJACK
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...