Page 1 of 1 1
Topic Options
#31349 - 2002-10-24 03:38 PM Writing registry entry to log file
Chris Beggs Offline
Fresh Scripter

Registered: 2001-09-27
Posts: 27
Loc: Kitchener
I'm trying to write which printers are installed when a user logs on.
I have the script to write info to a log file and append it but not sure how to take registry info out and then write it to a text file.

Any help?

Chris

Top
#31350 - 2002-10-24 03:43 PM Re: Writing registry entry to log file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You'd use READVALUE() to read the value of a registry key. You can then use REDIRECTOUTPUT() or WRITELINE() to write to a text file. Take a look at the WRITELOG() and WRITELOG2() UDFs, they are used to write (time-stamped) log files or use the DISPLAYTEXT() UDF wich displays text on the screen and additionally can log it into a text file.

[ 24. October 2002, 15:50: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#31351 - 2002-10-24 03:47 PM Re: Writing registry entry to log file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Have you tried ReadValue( )to read a registry value and return it as a string which you could then write to your txt file?
{Edit} timing is everything...how's that for repetitive redundancy... [Wink] {/Edit)

[ 24. October 2002, 15:50: Message edited by: Waltz ]
_________________________
We all live in a Yellow Subroutine...

Top
#31352 - 2002-10-24 04:24 PM Re: Writing registry entry to log file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I would suggest that instead of writing to a TXT file with WriteLine() that you WriteProfileString() to an INI file instead.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#31353 - 2002-10-24 05:25 PM Re: Writing registry entry to log file
Chris Beggs Offline
Fresh Scripter

Registered: 2001-09-27
Posts: 27
Loc: Kitchener
I'm having some problems trying to get the outputed text into the log file.
When I run the script it only displays on the screen.

Here is the script.

$LogPath="\\freya\logging\LOG"
$LogFile="$LogPath\@USERID.LOG"
$RETURN=CHR(13)+CHR(10)
$Default = HKEY_CURRENT_USER\PRINTERS
$Printers + "HKEY_CURRENT_USER\PRINTERS\CONNECTIONS"

$LogError=OPEN(5,$LogFile,5)

$LogText="@USERID - @DAY @MDAYNO. @MONTH @YEAR - @TIME" GOSUB WRL
$LogText="" GOSUB WRL
$LogText="Name : @FULLNAME" GOSUB WRL
$LogText="Computername : @WKSTA" GOSUB WRL
$LogText="Domain : @DOMAIN" GOSUB WRL
$LogText="Logon Domain : @LDOMAIN" GOSUB WRL
$LogText="Logon Server : @LSERVER" GOSUB WRL
$LogText="DEFAULT PRINTER : READVALUE ("$DEFAULT","") redirectoutput("$LogFile") = 0"

Chris

Top
#31354 - 2002-10-24 05:34 PM Re: Writing registry entry to log file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
For your logging tasks checkout: URL-> WiteLog2()

{edit}
this line:
$LogText="DEFAULT PRINTER : READVALUE ("$DEFAULT","") redirectoutput("$LogFile") = 0"

Is completely wrong. Not sure how to fix it because I'm not sure what you want it to do.

$LogText="DEFAULT PRINTER : " + READVALUE ("$DEFAULT","") ????

[ 24. October 2002, 17:38: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#31355 - 2002-10-24 05:47 PM Re: Writing registry entry to log file
Chris Beggs Offline
Fresh Scripter

Registered: 2001-09-27
Posts: 27
Loc: Kitchener
What I want it to do is take all the printer entries in the regisrty and add them to a file.
It also has to identify which is the default printer.

Top
#31356 - 2002-10-24 05:47 PM Re: Writing registry entry to log file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Please read the documentation to REDIRECTOUTPUT() in the KiXtart Manual. You have to issue a REDIRECTOUTPUT() first, then everything that would normally be printed to the screen will be redirected to the file. At the end you will have to issue another REDIRECTOUTPUT() to revert back to the screen. Thisis all well documented in the manual.

OPEN() is only used in combination with WRITELINE(), READLINE(), and CLOSE().

Please read the KiXtart Manual!

[ 24. October 2002, 17:48: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#31357 - 2002-10-24 05:52 PM Re: Writing registry entry to log file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
this isn't a custom solution for you, but I think most of the logic is headed to where you want to go.
code:
Break on
$PrtLog = "\\ambdc009\log$$\macomprt\"+@wksta+".log"
If @Inwin=1 ;NT
;Process network connected printers
$Printers = fEnumKey("", "HKEY_CURRENT_USER\Printers\Connections")
For each $Printer in $Printers
$a = split($Printer,",")
If $a[2] = "n358sprint2k"
$oldprinter = "\\n358sprint2k\" + $a[3]
$newprinter = "\\us358fp00\" + $a[3]

$RC = AddPrinterConnection($newprinter)
If $RC = 0
WriteLog2($PrtLog,"@productType - " + $oldprinter + " updated to " + $newprinter,1)
$RC = SetDefaultPrinter ($newprinter)
$RC = DelPrinterConnection($oldprinter)
If $RC = 0
WriteLog2($PrtLog,"@productType - " + $oldprinter + " updated to " + $newprinter,1)
Else
WriteLog2($PrtLog,"@productType - " + $oldprinter + " found. FAILED deleting printer. Error: " + $RC,1)
Endif
Else
WriteLog2($PrtLog,"@productType - " + $oldprinter + " found. FAILED connecting to " + $newprinter +". Error: " + $RC,1)
Endif
Else
WriteLog2($PrtLog,"@productType - " + $oldprinter + " found, but not the old server",1)
Endif
Next

$userid = "sussacct"
$password = "pebkac"

setl "SU_COMMANDLINE=%tmp%\kix32.exe @Lserver\netlogon\macom01\prtmig2.kix"
setl "SU_DOMAIN=@domain"
setl "SU_PASSWORD=$password"
;shell "%comspec% /c su $userid"

Else ;Win9x
$Key = "HKEY_CURRENT_USER\Printers\Connections\"
$Printers = fEnumKey("", $Key)
For each $Printer in $Printers
$port = ReadValue ($PrinterKey+$Printer, "port")
If @ERROR = 0
$OldPrtServer = "\\n358sprint2k"
$NewPrtServer = "\\us358fp00"
$Len = Len($OldPrtServer)
If Left($port, $Len) = $OldPrtServer
$port2 = $NewPrtServer + Substr($OldPrtServer, $Len+1)
$RC=WriteValue($Key+$Printer, "port", $port2, "REG_SZ")
If $RC = 0
WriteLog2($PrtLog,"@ProductType - " + $port + " updated to " + $port2,1)
Endif
Endif
Endif
Next
Endif



;FUNCTION fEnumKey($Computer, $Key)
;
;AUTHOR Howard A. Bullock (hbullock@comcast.net)
;
;ACTION Enumerates registries keys on the specified computer.
;
;SYNTAX fEnumKey($Computer, $Key)
;
;PARAMETERS $Computer (Required) - String value
; $Key (Required) - String value
;
;REMARKS Do not prefix the computer name with "\\"'s. Can serve as a
; direct replacement for EnumKey().
;
;RETURNS Array of keys names.
;
;DEPENDENCIES KiXtart 4.02,
;
;EXAMPLES fEnumKey("", "HKEY_USERS")
; fEnumKey("Remote1", "HKEY_USERS")
;
Function fEnumKey($Server, $Key)
Dim $Index, $Error
$Index = 0
Dim $KeyName[$Index]
If $Server <> ""
$Key = "\\" + $Server + "\" + $Key
Endif

If KeyExist($Key)
Do
$x = ENUMKEY($Key, $Index)
$Error = @Error
If NOT $Error and $Index > Ubound($KeyName)
ReDim PRESERVE $KeyName[$Index]
Endif
If NOT $Error
$KeyName[$Index] = $x
$Index = $Index + 1
Endif
Until $Error
Else
$KeyName[0] = ""
Exit 2
Endif
$fEnumKey = $KeyName
Exit 0
Endfunction


;--------------------------------------------------------------------------------------------------
;FUNCTION WriteLog2()
;
;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com)
;
;ACTION Generic logging facility for scripts. Appends log entry to a file with an
; optional TimeStamp.
;
;SYNTAX WriteLog2($File, $text, [0|1])
;
;PARAMETERS $File (Required) - String value
; $text (Required) - String value
; $TimeStamp (Optional) Default(0) no TimeStamp (1 or 0)
;
;
;REMARKS This function writes (appends) an optionally time stamped log entry to the file
; defined in function. This function searches for the first unused file handle,
; open the file, and write the entry. The file handle is then closed. When the
; function is unable to perform its it write the error is displayed in a message box.
;
;RETURNS Nothing
;
;DEPENDENCIES None
;
;EXAMPLES WriteLog2("junk.txt","This is a test")
; WriteLog2("junk.txt","This is a test",0)
; WriteLog2("junk.txt","This is a test",1)
;
;
Function WriteLog2($File, $Text, optional $TimeStamp)
dim $RC, $File, $text, $FH, $TimeStamp
$FH=1
$RC=Open ($FH, $File, 5)
while $RC = -3
$FH=$FH +1
$RC=Open ($FH, $File, 5)
Loop
Select
Case $RC=0
if ($TimeStamp=1)
$TimeStamp = @Date + " " + @Time + " - "
else
$TimeStamp = ""
endif
$RC=Writeline ($FH, $TimeStamp + $Text + @CRLF)
$RC=Close ($FH)
Case $RC=-2
$text = "WriteLog2: Invalid file handle ($FH) specified when trying to Open $File."
$RC=MessageBox ($text,"Script Error",48)
Case $RC=-1
$text = "WriteLog2: Invalid file name ($File) specified for log file."
$RC=MessageBox ($text,"Script Error",48)
Case $RC=>0
$text = "System Error($RC) while attempting to open log file ($File)."
$RC=MessageBox ($text,"Script Error",48)
Endselect
EndFunction
;--------------------------------------------------------------------------------------------------


_________________________
Home page: http://www.kixhelp.com/hb/

Top
#31358 - 2002-10-24 06:23 PM Re: Writing registry entry to log file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Chris,
What are you going to do with the info once it's in a TXT file? Also how are you going to deal with multiple entries from the same machine.

If you plan to use the collected data programmatically, then I still say an INI file is the way to go.

Here's an exerpt from a script I started a while back that will take the default printer from the reg and write it to an INI.
code:
 break on
$_ = SetOption("WrapAtEOL", "On")
$OldPS = "FF_TREE"
$NewPS = "FFPRINT"
$WkstaINI = "\\PDC\Logs\Wksta.ini"
; Retrieve default printer from Registry
;
$PrnMigINI = "\\PDC\Logs\PrnMig.ini"
$HKCUSMWNTCVW = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows"
$CurDefPrnValDat = ReadValue($HKCUSMWNTCVW,"Device")
;?"Cur Def Printer Value: " + $CurDefPrnValDat
$CurDefSvrPrn = "\\" + SubStr($CurDefPrnValDat,3,InStr($CurDefPrnValDat,",")-3)
?"Cur Default Server\Printer: " + $CurDefSvrPrn
$CurDefSvr = SubStr($CurDefSvrPrn,3,InStrRev($CurDefSvrPrn,"\")-3)
?"Cur Default Server: " + $CurDefSvr

;*************************************************************
; Write default printer to INI
;
$RC = WriteProfileString($WkstaINI,@Wksta,"DefaultPrinter",$CurDefSvrPrn)

get $_

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#31359 - 2002-10-24 07:58 PM Re: Writing registry entry to log file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
les...
Call me crazy [Eek!] , but in my environment (WinXP Pro) your
code:
$CurDefPrnValDat = ReadValue($HKCUSMWNTCVW,"Device")  

returns the first device found, which may/may not be the default device. Comments?
_________________________
We all live in a Yellow Subroutine...

Top
#31360 - 2002-10-24 08:46 PM Re: Writing registry entry to log file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well, that's easy...

I don't allow XP on the network. [Big Grin]

That code in this thread was meant as an example of how to get info from the reg to an INI.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#31361 - 2002-10-24 09:02 PM Re: Writing registry entry to log file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Understood...

$XP==$XtraPains [Wink]

Anywho, WMI thingies can pinpoint the default.

BTW, in noodling with this, I stumbled on HKCU\Printers\DeviceOld which is advertised as the default when user last logged off.
_________________________
We all live in a Yellow Subroutine...

Top
#31362 - 2002-10-24 09:49 PM Re: Writing registry entry to log file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
les, you have made a new meaning of selfexplatonary varnames...

lol...
_________________________
!

download KiXnet

Top
#31363 - 2002-10-24 09:58 PM Re: Writing registry entry to log file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Even though I'm a Magyar, it's still not Hungarian Notation.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#31364 - 2002-10-24 10:39 PM Re: Writing registry entry to log file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
Chris...
This solution works for me. Note that it employs COM and not a reg read...
BTW, is your Waterloo the same as mine?
code:
$LogPath="\\freya\logging\LOG"
$LogFile=$LogPath+"\@USERID.LOG"
; get the default printer **********************************
$i=0
$j=0
$printers=""
$defprinter=""
FOR EACH $prnval IN Split(WMIQuery("default","Win32_Printer",@WKSTA),"|")
$i=$i+1
FOR EACH $prnname IN Split(WMIQuery("Deviceid","Win32_Printer",@WKSTA),"|")
$j=$j+1
IF $i == $j AND $prnval = -1 ; this is the default printer
$defprinter = $prnname
ENDIF
NEXT
NEXT
; get all the printers *************************************
FOR EACH $prnname IN Split(WMIQuery("Deviceid","Win32_Printer",@WKSTA),"|")
$printers=$printers+$prnname
NEXT
; write to file ********************************************
;IF REDIRECTOUTPUT($LogFile,0) = 0 ; output appends to file content
IF REDIRECTOUTPUT($LogFile,1) = 0 ; output overwrites file content
@USERID+" - "+@DAY+" "+@MDAYNO+" "+@MONTH+" "+@YEAR+" - "+@TIME??
"Name : "@FULLNAME?
"Computername : "@WKSTA?
"Domain : "@DOMAIN?
"Logon Domain : "@LDOMAIN?
"Logon Server : "@LSERVER?
"Printers : "$printers?
"Default Printer :"$defprinter?
ENDIF
; write to screen ******************************************
IF REDIRECTOUTPUT("") = 0
?"All done"?
ENDIF

;
FUNCTION WMIQuery($what,$where, OPTIONAL $computer)
DIM $strquery, $objenumerator, $value
IF NOT $computer $computer="@WKSTA"
ENDIF
$strquery = "Select $what From $where"
$systemset = GetObject("winmgmts:{impersonationLevel=impersonate}!//$computer")
$objenumerator = $systemset.execquery($strquery)
FOR EACH $objinstance IN $objenumerator
IF @error = 0 AND $objinstance <> ""
$=Execute("$$value = $$objInstance.$what")
$wmiquery="$value"+"|"+"$WMIQuery"
ENDIF
NEXT
$wmiquery=Left($wmiquery,Len($wmiquery)-1)
EXIT @error
ENDFUNCTION



[ 25. October 2002, 13:07: Message edited by: Waltz ]
_________________________
We all live in a Yellow Subroutine...

Top
#31365 - 2002-10-25 03:00 PM Re: Writing registry entry to log file
Chris Beggs Offline
Fresh Scripter

Registered: 2001-09-27
Posts: 27
Loc: Kitchener
Thanks for all the input from everyone.
Some ideas seemed to work better than others and some I just don't understand at all.

Although I've been working with kix for awhile, I just don't grasp all the concepts. I.E. WMI and arrays.

Waltz, I am from the same Waterloo as you, small world.

I'm going to try and combine some of the scripting ideas and break it down to something I can work with. Hopefully then I can move on to the next stage of what I want to do.

Thanks again,
Chris

P.S. Waltz, I still couldn't get your script to output the printers. Is there a version of KIX I need. Currently running 4.0

Top
#31366 - 2002-10-25 03:31 PM Re: Writing registry entry to log file
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
from K-W to K-W... [Cool]

Chris...

Kix 4.0 doesn't cut it for the printer stuff for you 'cuz ver 4.0 can't handle the COM thingy... [Frown] I believe that you'll need to be at least ver 4.11
Suggest you consider upgrading to latest ver (4.12B2) to take advantage of improved capabilities... [Smile]

As far as discovering WMI try this --> ADSI/WMI/COM Info and links
and for COM try this --> COM scripting primer

Cheers...
_________________________
We all live in a Yellow Subroutine...

Top
#31367 - 2002-12-07 04:57 PM Re: Writing registry entry to log file
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear,

For another solution see our script printers.kix, which you can find on
our site.
It can create all kind of files.
greetings.

btw: symbol  - on our homepage has been linked to related http://kixtart.org topic.
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 581 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.074 seconds in which 0.029 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org