/build/static/layout/Breadcrumb_cap_w.png

Getting Printers and Addresses from Registry - String Manipulation in CMD or VBScript

OK, So I want to pull the existing printers from the computer before imaging, but since its running the KBE, I can't use the MS VBS files. So I tried to pull them from the registry using the following commands (I will change them to match the registry of the offline Windows installation once in the KBE):

reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers | findstr /i "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)">a.txt
FOR /f "usebackq delims=}" %p IN (`reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers ^| findstr /i "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)"`) DO reg query "%p\DsSpooler" /v portName | findstr "portName">>a.txt 

I can get the following output file, a.txt:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\HP Color LaserJet CP5220 Series PCL6
    portName    REG_MULTI_SZ    155.229.18.103

Now, what I need to do, is look in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\ and pull the HostName or IPAddress keys (just in case some creative tech made the name different from the actual address), but my problem is (I'm embarased to admit) manipulating the output to get JUST a) the name of the printer ("HP Color LaserJet CP5220 Series PCL6") and b) its portName (155.229.18.103) into a usable format so I can plug it back into reg.exe and get the actual address. Any help? I know this would be easier with VBS but its been forever and a day since I used VB; I considered PowerShell but I dont know if its built into the KBE. If you can forgive my archaic methods I would be forever grateful.


0 Comments   [ + ] Show comments

Answers (2)

Posted by: flip1001 9 years ago
Black Belt
0
I tested this on my PC, but I doubt it will work on a wide scale basis. Also, I don't have anything under the standard tcp/ip ports even though I have a wireless printer installed.

@ECHO OFF

REM Skip the DefaultSpoolDirectory and LANGIDOfLastDefaultDevmode lines
FOR /F "SKIP=5 TOKENS=*" %A IN ('REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers') DO (CALL :PRINTERINFO %A)
GOTO :END

:PRINTERINFO
REG QUERY "%*" | FINDSTR /I "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)" > nul

IF "%ERRORLEVEL%" EQU "0" (
FOR /F "SKIP=1 TOKENS=7 DELIMS=\" %A IN ('REG QUERY "%*\DsSpooler" /v portName') DO (ECHO %A>> a.TXT)
FOR /F "SKIP=2 TOKENS=3" %A IN ('REG QUERY "%*\DsSpooler" /v portName') DO (ECHO %A>> a.TXT)
)

GOTO :EOF

:END

Posted by: anonymous_9363 9 years ago
Red Belt
0
It would be *much* easier in VBS, you're right.

To simplify the string manipulation, I'd first use Split with 'portName' as the separator, then use Split again on the first element of the returned array but this time using '\' as the separator. However, it would be a lot easier to use the registry class which I have posted a quadzillion times here on ITNinja and just get the values you want from the registry, using a For Each loop to...errrr...loop through the 'Printers' key. Actually, there's a function in that class that will retrieve the value names and the associated data and put it into an array.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ