#31349 - 2002-10-24 03:38 PM
Writing registry entry to log file
|
Chris Beggs
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
|
|
|
|
#31353 - 2002-10-24 05:25 PM
Re: Writing registry entry to log file
|
Chris Beggs
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
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 ]
|
|
Top
|
|
|
|
#31355 - 2002-10-24 05:47 PM
Re: Writing registry entry to log file
|
Chris Beggs
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
|
|
|
|
#31357 - 2002-10-24 05:52 PM
Re: Writing registry entry to log file
|
Howard Bullock
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 ;--------------------------------------------------------------------------------------------------
|
|
Top
|
|
|
|
#31358 - 2002-10-24 06:23 PM
Re: Writing registry entry to log file
|
Les
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
|
|
|
|
#31364 - 2002-10-24 10:39 PM
Re: Writing registry entry to log file
|
Waltz
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
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
Seasoned Scripter
Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
|
|
|
Top
|
|
|
|
#31367 - 2002-12-07 04:57 PM
Re: Writing registry entry to log file
|
MCA
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.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 641 anonymous users online.
|
|
|