Simple Batch File

Soldato
Joined
23 Nov 2004
Posts
3,794
All,

I have been set a task to output a specific REG key value to a text file. The Script/App needs to loop round and repeat for 140 different servers on our VPN. (Store.ini contains list of servers)

I have given up trying to do this in WSH or VBS as my boss ordered, so i am going to do it in a simple batch file. See my code below.

Code:
@echo off
REM Printer Find Script
REM Script connects to all devices in Store.ini and querys the registry for the default printer installed
for /f %%i in (Store.ini) do (
	ECHO Querying Default Printer on %%iWNT01
	ECHO ---------------------------------------------------------------------------------- >>Results.txt
	ECHO Printer Driver Information For %%iWNT01 >>Results.txt
	REG QUERY "\\%%iWNT01\HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\%%iprn01" /V "Printer Driver" >>Results.txt
	ECHO ---------------------------------------------------------------------------------- >>Results.txt
	Echo. >>Results.txt
	Echo. >>Results.txt
)
ECHO Finished Running Script.
PAUSE

Currently it reutns the below Results: (X's = Server name)


----------------------------------------------------------------------------------
Printer Driver Information For XXXXXXXX

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\XXXprn01
Printer Driver REG_SZ HP LaserJet 4 Plus

----------------------------------------------------------------------------------

I don't want it to display all the other cack, i just want it to output the HP LaserJet.... bit with the manual ECHO's i have specified.

Do i need to create a different batch file to rip all this crap out, or can i specify it using a flag for REG QUERY? I have googled to no avail. :(
 
May not be 100%, but this is how I would do it in a vbs script....

Code:
Const FOR_READING = 1
Const HKEY_LOCAL_MACHINE = &H80000002
strFilename = "store.ini"
strOutputFilename = "results.txt"
strKeyPath = "SYSTEM\CurrentControlSet\Control\Print\Printers\"
strValueName = "Print Drivers"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilename) Then
  Set objFile = objFSO.OpenTextFile(strFilename, FOR_READING)
Else
  WScript.Echo "Input file " & strFilename & " not found."
  WScript.Quit
End If


Do Until objFile.AtEndOfStream
'Get name of computer.
  strComputer = objFile.ReadLine
Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\\" & _
   strComputer & "\root\default:StdRegProv") 

oReg.GetDWORDValue _
    HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue
    
Set objFile = objFSO.OpenTextFile(strOutputFilename, ForWriting)
objFile.WriteLine dwValue
objFile.Close
Loop
 
It after about 5/6 seconds and brings up the below error

errorlr2.jpg


Thanks for your time on this, baking my noodle here...

Edit: Sorry for white canvas in background as well, only got MSPAINT.
 
Sorry,
I've just looked at the code I posted and its pretty poor to say the least.
This should work:

Code:
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("store.ini", ForReading)
Set objTextFile2 = CreateObject("Scripting.FileSystemObject")
Set strOutputLog = objTextFile2.CreateTextFile("results.txt", True)

Do Until objTextFile.AtEndOfStream
    ReadRegistry
Loop

   Sub ReadRegistry()
    Err.Clear
    On Error Resume Next
    strComputer = objTextFile.Readline
    Set oReg=GetObject( _
       "winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")
        strKeyPath = "SYSTEM\CurrentControlSet\Control\Print\Printers\"
    strValueName = "Print Drivers"
    oReg.GetSTRINGValue _
       HKEY_LOCAL_MACHINE,strKeyPath,strValueName,szValue
    strOutputLog.WriteLine szValue
   End Sub

objTextFile.Close
strOutputLog.Close

WScript.Echo "Finished"

Note: Check I've entered the correct reg key etc.
If you want to add the computer's name to the output file, change the strOutputLog.WriteLine szValue to strOutputLog.WriteLine strComputer & " - " & szValue or however you want it formatted :)
 
Back
Top Bottom