Script to check if printer exists locally, and if so, deletes it - printing

I'm writing a windows batch file and need to check whether the print exists on the local computer, and if so, deletes the mapped printer from the computer. Here is the code that I'm using to delete the printer.
RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn
This works fine, but now I need a conditional statement before this so I check if that printer exists first. Then run that line. I'm not sure how to write this.

You could try something like this, just replace the string to find with the printer you want to find.
For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I "560C"') Do If "%%I"==",,ServerName,HP DeskJet 560C" goto :REMOVE
goto :SKIP
:REMOVE
RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn
:SKIP
Or just run the command and if it doesn't exist it will error, if it does it will work?
Hope this helps!

You can just skip the check and run your deletion command anyway.
To suppress the error popup just add the /q option as specified in the rundll32 printui.dll documentation.
this yields:
RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn /q

Related

CUPS Printer added using command prompt is not working with authentication

I have a CUPS server setup in Linux.I want to add this printer to my windows programmatically. Manually I am able to add.By searching various forums I found the following command:
rundll32 printui.dll,PrintUIEntry /if /b "TestPrinter_With_Auth" /f %windir%\inf\ntprint.inf /r "https://username:pasword#ipp.devCups.com/printers/Printer" /m "HP Color LaserJet 2500 PCL6 Class Driver"
I could install Printer using this command. But I couldn't Print to CUPS as it is not recognizing the credentials. I get access denied.
Any suggestion to achieve this programmatically.
Thanks in Advance
Matt

make an lp script into functioning printer

I'm trying to print to separate printers simultaneously in Ubuntu 14.04
From all of my reading the best option I've seen is to write a script
that sends an lp command to the separate printers.
This is the script I've written so far
!/bin/bash
lp -d printer "$#"
lp -d printer2 "$#"
where printer and printer 2 are the actual printers installed on the system
This script works from a terminal, however I would like to be able to send print jobs directly to a "printer" that is actually the script I've written.
How can I make this lp script into a "printer"
Ok I didn't find a way to do this the way I originally intended, however it is possible with tea4CUPS
great cups backend tool with an easy config file
http://www.pykota.com/software/tea4cups/download
The install instructions are on the download page.
As for printing to multiple printers, add this command in the config file for every printer you wish to print to.
prehook_firstprinter: /usr/bin/lp -d Name of Printer -o raw $TEADATAFILE
Here are the simplest instructions I could write
1. download the tea4cups.gz
Extract it to the home folder, rename it to tea4cups
Open a terminal and run these commands
sudo cp /home/manifester/tea4cups/tea4cups.conf /etc/cups
sudo cp /home/manifester/tea4cups/tea4cups /usr/lib/cups/backend/
sudo chmod 700 /usr/lib/cups/backend/tea4cups
Run this command
sudo gedit /etc/cups/tea4cups.conf
Paste this in the bottom of the document
prehook_firstprinter: /usr/bin/lp -d Name of Printer yes the literal name in the printer window -o raw $TEADATAFILE
you will need a new line for every printer you have, so if you want to print to 3 printers you will need three of the above line each having the name of the printer it will be talking to.
save and close everything
open a terminal and run
sudo service cups restart
open a web browser and go to the browser cups controller
http://localhost:631/admin
go to Add Printer
you should see a printer named "tea4CUPSnothing"
If you don't see it go back and press "Find Printers"
it should be there
Change the info of the printer to "Print all" for all type fields"
Press Continue
The generic printer driver works because the printer doesn't actually exist.
Press Continue
Set Defaults
you should be done, go to your printer window on ubuntu and do a test print.

Script to add network printer to all users x64 bit print server

I use to use this script for Windows XP but since we're doing the switchover to 7 I tried to use it on the new images. It seems like it tries but it doesn't work. I have created a 64bit print server and these machines are 64bit which is the only difference. I read up on this and saw there was a GPO that needed to be set to allow this to work. Which was - Computer Configuration > Administrative Templates > Printers > Allow Print Spooler to accept client connections.
I have tried everything and can't get this to work, it doesn't give me an error or anything. It gives me the prompts for PC name and Printer name, then says 'Adding printer' from the echo command, and just sits there. I can run the command by itself and it doesn't work either....... Please help!
The main thing is that the printer needs to be added from a print server and to the computer for all users as their default.
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
ECHO Adding Printer...
\\ghostserver\installs\pstools\psexec \\%PC% -n 3 cmd /c rundll32
printui.dll,PrintUIEntry /y /ga /c\\%PC% /n\\PRINTSERVER\%PRINTER%
ECHO Restarting Print Spooler...
start /wait sc \\%PC% stop spooler
start /wait sc \\%PC% start spooler
Do you want to install or map the printer from the network ?
First thing to try : map the printer manually on a 7 x64 client. If it fails, your problem is not the batch.
Also try the simple rundll32 printui.dll PrintUIEntry /in n\\PRINTSERVER\%PRINTER% with a non-admin non-elevated account on the client to validate the print server configuration.
Is the "Disallow installation of printer using kernel-mode drivers" GPO disabled ? (Have to be)
Check the firewall settings, UAC/elevation configuration, admin access. Run a gpupdate /force and restart the client.
Check the event log on both the client and print server for any errors.
With an admin account (both print server and client), try to push the installation from the print server.
Have you tried to force adding the provider ? /j "LanMan Print Services"
If you have 2008 servers or DCs, you can use Print Management or Group Policy Preferences to deploy printers (easier than bat+psexec+printui.dll).
If you really want to do it via login script, there are also a bunch of tools in Vista/7/8 for print management in %WINDIR%\System32\Printing_Admin_Scripts, like this one.
Side note : start /wait is inefficient since sc.exe doesn't wait any response of the service. So, if you stop and start without a pause, chances are the service will not be stopped before the restart and skip the second order. You have to simulate a pause (ping 127.0.0.1 -n 5 >nul 2>&1) between stop & start or use a safer script to check the state of the service.
Thank you so much! That fixed it.
Here's my add script:
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
echo Adding Printer...
\\servername\installs\pstools\psexec -s -i -accepteula \\%pc% rundll32 printui.dll PrintUIEntry /in /y /ga /n\\PRINTSERVER\%PRINTER%
echo Restarting Print Spooler...
start sc \\%pc% stop spooler
pause
start sc \\%pc% start spooler
pause
Here's my remove script:
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
echo Adding Printer...
\\servername\installs\pstools\psexec -s -i -accepteula \\%pc% rundll32 printui.dll PrintUIEntry /gd /n\\PRINTSERVER\%PRINTER%
echo Restarting Print Spooler...
start sc \\%pc% stop spooler
pause
start sc \\%pc% start spooler
pause

Executing things on console2 startup

I am using console2 and I am trying to execute a few simple things when I fire console2 like
set PATH=%PATH%;C:\MyPgm\Git\bin
How can I do such things.
And on the side is there somewhere I can find example of nice configs of console2
The version I am working with is 2.00.148
First, create a batch file (*.bat) with any name you like ("shell.bat") and add the lines to be executed on the start up of the console.. additionally you have to add "cmd" (or "bash") command at the end of file to open up the command shell...the file will be like that
#echo off
set PATH=%PATH%;C:\MyPgm\Git\bin
cmd
then run console2 ..from Main Menu > Edit > Settings > "Console" Node .. and press ".." button beside "shell" field ,select "*.bat" file type then select the file you created earlier OR write down the path to this file..(you can also set working directory in "startup dir")...save and restart
that's it !!
Enjoy !!
My use case is that I would like to run a command just after start the console2, in this case it is a command to set ruby version. The process is similar to #nobleCoder
create the .bat file, I name it ruby_setup.bat
#echo off
set init_ruby_243=uru 243p205
cmd.exe /k "%init_ruby_243%"
specify .bat file to execute when start the console2 by going to Edit > Settings then at the Shell field select the file ruby_setup.bat or manually input it like C:/users/myname/ruby_setup.bat then save and restart.
Hope this help.

Powershell ISE appears to hang with interactive commands.

I've just downloaded Powershell 2.0 and I'm using the ISE. In general I really like it but I am looking for a workaround on a gotcha. There are a lot of legacy commands which are interactive. For example xcopy will prompt the user by default if it is told to overwrite a file.
In the Powershell ISE this appears to hang
mkdir c:\tmp
cd c:\tmp
dir > tmp.txt
mkdir sub
xcopy .\tmp.txt sub # fine
xcopy .\tmp.txt sub # "hang" while it waits for a user response.
The second xcopy is prompting the user for permission to overwrite C:\tmp\sub\tmp.txt, but the prompt is not displayed in the ISE output window.
I can run this fine from cmd.exe but then what use is ISE? How do I know when I need which one?
In a nutshell, Interactive console applications are not supported in ISE (see link below). As a workaround, you can "prevent" copy-item from overwriting a file by checking first if the file exists using test-path.
http://blogs.msdn.com/powershell/archive/2009/02/04/console-application-non-support-in-the-ise.aspx
Why would you be using XCOPY from PowerShell ISE? Use Copy-Item instead:
Copy-Item -Path c:\tmp\tmp.txt -Destination c:\tmp\sub
It will overwrite any existing file without warning, unless the existing file is hidden, system, or read-only. If you want to overwrite those as well, you can add the -force parameter.
See the topic "Working with Files and Folders" in the PowerShell ISE help file for more info, or see all the commands at MSDN.

Resources