Page 1 of 1 1
Topic Options
#175725 - 2007-04-26 06:59 PM replacing items in a text file, the joy of writeline.
likedark Offline
Fresh Scripter

Registered: 2005-03-23
Posts: 6
I have been kicking around the following code for a few days. The higher ups where I work would like a simple log to track some anti-virus info. I did a search and while I found some things here that were helpful, they wanted to use a specific format.

My problem is I want to be able to only add new items to the log and overwrite existing lines for pre-existing computers. I have tried writeline and readline but cannot seem to get anything to work in concert with the other. Either the Writelog functions works with everything else turned off or the writeline portion works but with everything else remmed out (meaning the open, if..then, while...loop statements, EVERYTHING).

It seems pretty simple to do, loop through each line for variable "x", if you find it, write this, if not, then write this. But then I am not a programmer by nature, so please bear with me.

Oh, I am using Kix 4.53.0.0, all our computer are XP with latest service packs, etc, etc.

Anyone have suggestions?

 Code:
debug off
; declare variables for McAfee items and fill in the values with Registry locations.
     $virusProd=ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8600", "Product Name")
     $virusver=ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8600", "Version")
     $virusdat=ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8600", "DATVersion")
     $virusdate=ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8600", "DATDate")
     $virusHotFix=ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8600", "HotFixVersions")
     $epoagentver=ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework\", "Version")

; open the file and go through each line. If the line starts with the local computer name already present,
; overwrite that line with new info. Otherwise just write to log.
     open (1,"\\aimemsrv05\scripts\logs\antivirus.txt",5)
     $x=readline(1)
          while @error=0
          if $x=@WkSta
          writeline(1, '@WkSta, @Date, $virusProd, $virusver, $virusdat, $virusdate, $virusHotFix, $epoagentver')
               else
               WriteLog("\\aimemsrv05\scripts\logs\Antivirus.txt","@WkSta, @Date, $virusProd, $virusver, $virusdat, $virusdate, $virusHotFix, $epoagentver")
          endif
     $x=readline(1)
     loop
     close (1)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----- create text files for use as logs
FUNCTION WriteLog($LogFileName, $LogFileMessage)

	$WriteLog = 0
	OPEN(1, $LogFileName, 5)
	IF WRITELINE(1, $LogFileMessage + @CRLF)=0
		$WriteLog = 1
	ENDIF
	CLOSE(1)
endfunction

Top
#175729 - 2007-04-26 07:30 PM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22298
Loc: OK
so, the antivirus info generic udf doesn't work for you?

anyways, I use ini-files with writeprofilestring myself.
this way, there is no need to care about lines but kixtart takes care of the logic for me.

the only down side is that, if I want to have a nice excel showing all the info of our domain, need to read it with readprofilestring.
but, that's not a problem for me.
_________________________
!

download KiXnet

Top
#175730 - 2007-04-26 07:53 PM Re: replacing items in a text file, the joy of writeline. [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4256
Loc: New Jersey
I'm in agreement regarding INI files and read/write profilestring functions. The data you collect is a database, from which you generate a report - it isn't a report itself.

You can use an INI file with one section per computer, and detail in each section. From that, you can use EnumIni() to extract all the values (or INI2Ary), and the CSV() udf to write the arrays to a CSV file so it can be opened/formatted in Excel.

Just my $0.47

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#175731 - 2007-04-26 08:01 PM Re: replacing items in a text file, the joy of writeline. [Re: Glenn Barnas]
likedark Offline
Fresh Scripter

Registered: 2005-03-23
Posts: 6
[quote=Jooel]so, the antivirus info generic udf doesn't work for you?[quote]

No, not at all. I went through that one a few times, nice coding.

Top
#175732 - 2007-04-26 08:02 PM Re: replacing items in a text file, the joy of writeline. [Re: Glenn Barnas]
likedark Offline
Fresh Scripter

Registered: 2005-03-23
Posts: 6
 Originally Posted By: Glenn Barnas
I'm in agreement regarding INI files and read/write profilestring functions. The data you collect is a database, from which you generate a report - it isn't a report itself.

You can use an INI file with one section per computer, and detail in each section. From that, you can use EnumIni() to extract all the values (or INI2Ary), and the CSV() udf to write the arrays to a CSV file so it can be opened/formatted in Excel.

Just my $0.47

Glenn


I would agree. However, I detest working with ini files almost as much as dealing with requests for things like this when I know an easier way. know what I mean?

Top
#175733 - 2007-04-26 08:11 PM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22298
Loc: OK
so, let me know, when you find the easier-than-ini way. ;\)
_________________________
!

download KiXnet

Top
#175736 - 2007-04-26 08:45 PM Re: replacing items in a text file, the joy of writeline. [Re: Lonkero]
likedark Offline
Fresh Scripter

Registered: 2005-03-23
Posts: 6
well, I thought I was onto something with what I had written. Again, this is just something I have "picked up", I don't program for a living.
Top
#175737 - 2007-04-26 08:58 PM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4256
Loc: New Jersey
Using INI files is easy with Kix via Read/Write ProfileString functions.

Think of ReadProfileString()as
READ ( Database_file, Record_ID, Field_Name )

and WriteProfileString() as
WRITE( Database_file, Record_ID, Field_Name, Data )

You can read an entire "record" into an array by leaving the Field_Name blank
$Record_Array = ReadProfileString($File, "Record17")

Really can't get much easier than that, and you're totally isolated from the INI format.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#175738 - 2007-04-26 11:12 PM Re: replacing items in a text file, the joy of writeline. [Re: Glenn Barnas]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
True. INI files are easy to read and write. One INI file per computer would also be possible.
Top
#175818 - 2007-04-30 03:49 PM Re: replacing items in a text file, the joy of writeline. [Re: Witto]
likedark Offline
Fresh Scripter

Registered: 2005-03-23
Posts: 6
One INI file per computer? Ugh, 120 files to process through and write another script for. The heck with it, my boss can find another way to do this by himself. Some things just aren't worth doing in Kix. This must be one of them.
Top
#175820 - 2007-04-30 04:58 PM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
KiXtart is not the issue. This is basic I/O in windows. You have to deal with file locking and permissions with which ever tool or scripting language you choose.

I use a combination of KiXtart on the clients (logon script) and Perl to pickup and load the database for 40,000 clients. No big deal.

Process definition and proper control is part of the job. You can do it correctly or poorly, but if you do not add the proper logic and controls you are better to not do it at all.

It must be nice to tell your boss to shove it.


Edited by Howard Bullock (2007-04-30 05:00 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#175827 - 2007-04-30 06:35 PM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Why not an ini file per computer?
Create a list of all your computers in your domain.
 Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $RC
$RC=SetOption("Explicit", "On")
$RC=SetOption("NoMacrosInStrings", "On")
$RC=SetOption("NoVarsInStrings", "On")
$RC=SetOption("WrapAtEOL", "On")

Dim $objDomain, $objComputer, $strDomainName
Dim $arrComputers, $strComputer
$strDomainName = "FooCompany"

$objDomain = GetObject("WinNT://"+$strDomainName)
$objDomain.Filter = "Computer",""
For Each $objComputer In $objDomain
 	If NOT $arrComputers = ""
		$arrComputers = $arrComputers + ","
 	EndIf
 	$arrComputers = $arrComputers + $objComputer.Name
Next
$arrComputers = Split($arrComputers,",")

For Each $strComputer In $arrComputers
	;Do stuff
Next

At the
;Do stuff
you can do something per computer, per example see if an ini file exists with that name.
If the file exists, you should not check the computer anymore.
You can let this script run for some days until (almost) all computers have been checked.
After the job is done, enumerate all ini file in your directory. For every file in the list, you can do the same things reading the information in the file, per example, write a report in Excel.
I just did this some weeks ago to get a good idea about installed software on our computers.


Edited by Witto (2007-04-30 11:14 PM)
Edit Reason: Forgot to declare $objComputer in example

Top
#175830 - 2007-04-30 08:56 PM Re: replacing items in a text file, the joy of writeline. [Re: Witto]
likedark Offline
Fresh Scripter

Registered: 2005-03-23
Posts: 6
Witto -

Thanks, that makes more sense when described that way. Thank You.

Top
#175832 - 2007-04-30 11:18 PM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
You are welcome. If you have some (working) script, please come back and just let other users know what your solution is. Good luck!
Top
#175834 - 2007-05-01 01:11 AM Re: replacing items in a text file, the joy of writeline. [Re: likedark]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Maybe in your case, one ini file with the structure
 Code:
[Computer1]
ProductName=aaa
Version=bbb
DATVersion=ccc
DATDate=ddd
HotFixVersions=eee
FrameworkVersion=fff
[Computer2]
ProductName=aaa
Version=bbb
DATVersion=ccc
DATDate=ddd
HotFixVersions=eee
FrameworkVersion=fff
[Computer3]
ProductName=aaa
Version=bbb
DATVersion=ccc
DATDate=ddd
HotFixVersions=eee
FrameworkVersion=fff

Would do
What about something like:
;*************************************************************************
; Script Name: FindAVInfo
; Author: likedark
; Date: 1/05/2007
; Description: Read registry info of every computer in domain about AntiVirus program
;*************************************************************************


;Script Options
If
 NOT @LOGONMODE
    Break On
Else
    Break Off
EndIf
Dim $RC
$RC=SetOption("Explicit", "On")
$RC=SetOption("NoMacrosInStrings", "On")
$RC=SetOption("NoVarsInStrings", "On")
$RC=SetOption("WrapAtEOL", "On")

;Declare variables
Dim
 
$objDomain, $objComputer, $strDomainName
Dim $arrComputers, $strComputer
$strDomainName = "FooCompany"

Dim $strVIRUSSCAN8600, $strFramework
$strVIRUSSCAN8600 = "HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\VIRUSCAN8600"
$strFramework = "HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework\"

Dim $strProductName, $strVersion, $strDATVersion, $strDATDate, $strHotFixVersions, $strFrameworkVersion

Dim $strINI
$strINI = @SCRIPTDIR+"\AntiVirus.ini"

;Create array with all computers in domain
$objDomain
 =
GetObject
("WinNT://"+$strDomainName)
$objDomain.Filter = "Computer",""
For Each $objComputer In $objDomain
    If NOT $arrComputers = ""
        $arrComputers = $arrComputers + ","
    EndIf
    $arrComputers = $arrComputers + $objComputer.Name
Next
$arrComputers = Split($arrComputers,",")

;Get AV info and write to INI
;For each computer in the array

For Each $strComputer In $arrComputers
    ;Is there a section with the computername? If yes, the computer has been scanned before
    ;If scanned before, skip
    If AScan(Split(ReadProfileString($strINI,"",""),CHR(10)), $strComputer) = -1
       
;Can the first registry value be read?
        ;If no, the computer is probably offline
        ;Or the product is not installed
        ;If registry cannot be read, skip
        If NOT ReadValue("\\"+$strComputer+"\"+$strVIRUSSCAN8600, "Product Name") = ""
           
           
;Read the registry values and put them in variables
            $strProductName      = ReadValue("\\"+$strComputer+"\"+$strVIRUSSCAN8600, "Product Name")
           
$strVersion          = ReadValue("\\"+$strComputer+"\"+$strVIRUSSCAN8600, "Version")
           
$strDATVersion       = ReadValue("\\"+$strComputer+"\"+$strVIRUSSCAN8600, "DATVersion")
           
$strDATDate          = ReadValue("\\"+$strComputer+"\"+$strVIRUSSCAN8600, "DATDate")
           
$strHotFixVersions   = ReadValue("\\"+$strComputer+"\"+$strVIRUSSCAN8600, "HotFixVersions")
           
$strFrameworkVersion = ReadValue("\\"+$strComputer+"\"+$strFramework    ,  "Version")
           
           
;Write values to INI file
            $RC = WriteProfileString($strINI, $strComputer, "ProductName"     , $strProductName)
           
$RC = WriteProfileString($strINI, $strComputer, "Version"         , $strVersion)
           
$RC = WriteProfileString($strINI, $strComputer, "DATVersion"      , $strDATVersion)
           
$RC = WriteProfileString($strINI, $strComputer, "DATDate"         , $strDATDate)
           
$RC = WriteProfileString($strINI, $strComputer, "HotFixVersions"  , $strHotFixVersions)
           
$RC = WriteProfileString($strINI, $strComputer, "FrameworkVersion", $strFrameworkVersion)
           
       
EndIf
    EndIf
Next

Some more on
If AScan(Split(ReadProfileString($strINI,"",""),Chr(10)), $strComputer) = -1

ReadProfileString($strINI,"","")
gives all sections in one string, but divided by the ASCII character 10

Split(ReadProfileString($strINI,"",""),Chr(10))
gives an array with all section names

AScan(Split(ReadProfileString($strINI,"",""),Chr(10)), $strComputer)
tries to find $strComputer in the array and returns the position starting from 0
if not in array, -1 is returned

If AScan(Split(ReadProfileString($strINI,"",""),Chr(10)), $strComputer) = -1
If AScan returns -1, it was not found, there is no section with that computername

Maybe create a batch file where you loop this script. After a few days, you should have info about (almost) all computers

The script is not tested. It is just as is. Please revise the code yourself. The environment to test in is yours, not mine!!!


Edited by Witto (2007-05-01 10:42 AM)
Edit Reason: Revised conditions

Top
Page 1 of 1 1


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

Who's Online
0 registered and 177 anonymous users online.
Newest Members
dboozcruise, 2786707460, Marc57, EricMyers, siddharthinfosy
17519 Registered Users

Generated in 0.112 seconds in which 0.062 seconds were spent on a total of 13 queries. Zlib compression enabled.

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