Autoit: How to delete a printer based on its IP address? - printing

I am creating an AutoIt application to install network printers. However, I need a way to check for or delete a printer if it is already installed. I am using Prnmngr.vbs to check for names:
RunWait (#ComSpec & ' /c Cscript "' & $Prnmngr & '" -d -p "' & $printerName & '"', "", #SW_HIDE)
However, the name of any particular printer can vary from computer to computer. Is there a way I can search by IP address and thus get the name as installed on the computer?

Related

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.

How to print in duplex mode with smblient?

Because I don't want to give any information about a corporate printer to CUPS, because I suspect it trying to access it even when I'm not on the corporate network, I use the following "script" to print documents on a corporate printer managed by a Windows print server:
smbclient "//printserv.acme.corp/BUILDING0PRINTER1" "password" -c "print /tmp/my.pdf" -U "username" -W "Domain"
The printer is some HP MFP and can very well print duplex. My command, however, does not print duplex.
My question is: How to print a PDF in duplex mode (such that both sides of the paper sheet are used, aka flip on long edge)?
I don't mind extending my "script" by prepending some weird PCL or Postscript commands, but I don't want CUPS to be searching for the printer (and potentially send my password into the ether).
I found that pdftops provides a -duplex switch.
So I scripted the following:
pdftops -paper A4 -duplex "$1" "${1}.ps"
smbclient "//printserv.acme.corp/BUILDING0PRINTER1" "password" -c "print \"${1}.ps\"" -U "username" -W "Domain"

Reading netstat Output to Install Printer

Long story behind it, but here's what I'm trying to do:
I am working on a remote virtual machine implementation, where depending on the client device's location, the appropriate network printer will be installed via batch file (no VBS or PowerShell).
So, my idea is this:
Run netstat -an -p tcp to find the line containing port 49404.
Filter that output to grab the second IP address that will be
returned
Replace last octet of that IP with "250" (the printer IP
for each network)
Run nslookup on newly calculated IP to obtain
the name of that printer
Install printer by name.
Here's what I do have so far, pieced together from older posts around the web (I haven't gotten to steps 4 or 5 yet):
#echo off
netstat -p tcp -an | FIND "49404" > %temp%\TEMPIP.txt
FOR /F "tokens=2 delims=:" %%a in (%temp%\TEMPIP.txt) do set IP=%%a
del %temp%\TEMPIP.txt
set IP=%IP:~9%
set "ip=%IP%"
for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=232
)
I'm sure there are cleaner or more efficient ways to perform this task, so I'm hoping you all can point me in the right direction. Thanks!

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

Exitcode 6 using PsExec in Windows Service

I am using PsExec to defrag machines remotely on my network. This works fine in my c# code, but because it also does a bunch of other tasks and I want it to run continuously I have converted it into a windows service.
When the code gets to the PsExec portion it exits with the code 6 which seems to mean "The handle is invalid" I have tried entering a username and password with sufficient privileges without success, I have also tried adding -s to use the system account which did not help either.
Here is the line where I set my PsExec arguements...
psexec.StartInfo.Arguments = #" \\" + machine + "/accepteula -s defrag.exe " + volume + " -f";
I have also tried...
psexec.StartInfo.Arguments = #" \\" + machine + "/accepteula defrag.exe " + volume + " -f";
psexec.StartInfo.Arguments = #" \\" + machine + "/accepteula defrag.exe " + volume + "-u myuser -p mypass -f";
Any help would be greatly appreciated!!
But nothing seems to fix my problem.
I know this has been open a long time, but did you find a stuck psexec service on the remote machine?
What we found was the PsExec service entry was still in Services and PsExeSvc was still in C:\WINDOWS folder.
We deleted exe from C:\WINDOWS and ran “sc delete PsExec” from an elevated command prompt to get rid of service.

Resources