JeffS
(Fresh Scripter)
2007-08-08 11:22 PM
Validating a File Exists on Remote Computers

The basic jist of this is I that the script is reading in a text file that contains all of the computer names in the domain. Ideally what I would like for it to do is go through and validate if this file exists on the computer. If it does, I want it to write to this text file that say the computer name and if the file exists or not.

 Code:
 
Break on

Open(1,"SymForm.txt",2)

Do $Comp = ReadLine(1)
	$UNCComp = "\\"+$Comp

	If Exist("$UNCComp\C$\Program Files\SymForm API\SymStoreMod.dll")=1
		$ToWrite = $Comp+Chr(9)+"File Exists"
		Open(2, "\\servername\sharename\filename.txt", 5)
		WriteLine(2, $ToWrite + @CRLF)
		Close(2)
	Else
		$NoWrite = $Comp+Chr(9)+"File Does Not Exist"
		Open(2, "\\servername\sharename\filename.txt",5)
		WriteLine(2, $NoWrite + @CRLF)
		Close(2)
	EndIf

Until $Comp=""
Close(1)


There are few problems I am experiencing. For testing I typically start with one computer until the code appears to be correct. For some reason everytime I read the one computer name, it will create a valid return in the text file, but then create a new line with no computer name and say File does not exist. Then when I start to read in multiple computers it it throws off all of the records.

For some reason I can't wrap my head around why it isn't working. I am definately open to other ideas of how to validate that this file exist on all of the domain computers. Any suggestions are appreacatied.

Thanks!
Jeff


Glenn BarnasAdministrator
(KiX Supporter)
2007-08-08 11:58 PM
Re: Validating a File Exists on Remote Computers

Untested, but should work.

 Code:

; not only good practice, but needed because you are using "C$" in a string
; to specify the admin share!
$ = SetOption('NoVarsInStrings', 'On')

If Open(1,"SymForm.txt",2) = 0

  ; send all output to this file
  $ = RedirectOutput('\\servername\sharename\filename.txt')

  $Comp = ReadLine(1)   ; read the first line
  While Not @ERROR      ; loop until error occurs (End of File)
    If $Comp            ; process if not blank

      ; This wraps the Computer name in \\ and \, removing any slashes that already exist
      $Target = '\\' + Join(Split($Comp, '\'), '', 3) + '\'

      ; this print statement is common to all processing
      $Comp Chr(9) 
      If Exist($Target + 'C$\Program Files\SymForm API\SymStoreMod.dll')
        'File Exists' ?
      Else
        'File Missing' ?
      EndIf

    EndIf
    $Comp = ReadLine(1)	; read the next line
  Loop
  Close(1)

  ; close the output redirection
  $ = RedirectOutput('')

EndIf


RedirectOutput is so much easier to use for this kind of thing. I'd also consider using WriteProfileString to record the status to an ini file.

Glenn


JeffS
(Fresh Scripter)
2007-08-09 01:25 AM
Re: Validating a File Exists on Remote Computers

Thanks Glenn!

It works but doesn't work. You are getting the same issue I am. When I place one computer name in the SymForm.txt file I get the correct anwser of 'File Exists' however, once I input multiple computer names into the file, I get 'File Missing' even with computers that I have verified that the file is in the correct folder. So back to square one just with better code! ;-)


Glenn BarnasAdministrator
(KiX Supporter)
2007-08-09 04:08 AM
Re: Validating a File Exists on Remote Computers

Try this mod - it should write to the log, but tell you on the command line what is happening. Should display :#, where the # is 0 or 1 - the output of the Exist function. Will also display the exit status of the ReadLine(1) function.

I changed the search path to something I have, and this code works for me - searched 6 servers here and found files, but didn't find them on my kid's XP workstations.
 Code:
; not only good practice, but needed because you are using "C$" in a string
; to specify the admin share!
$ = SetOption('NoVarsInStrings', 'On')

If Open(1,"SymForm.txt",2) = 0

  $Comp = ReadLine(1)	; read the first line
  While Not @ERROR	; loop until error occurs (End of File)
    If $Comp		; process if not blank

      ; This wraps the Computer name in \\ and \, removing any that already exist
      $Target = '\\' + Join(Split($Comp, '\'), '', 3) + '\'

      'Processing ' $Comp ':' Exist($Target + 'C$\Program Files\SymForm API\SymStoreMod.dll') ?      

      ; send all output to this file
      $ = RedirectOutput('\\server\share\filename.txt')

      ; this print statement is common to all processing
      $Comp Chr(9) 
      If Exist($Target + 'C$\Program Files\SymForm API\SymStoreMod.dll')
        'File Exists' ?
      Else
        'File Missing' ?
      EndIf

      $ = RedirectOutput('')

    EndIf
    $Comp = ReadLine(1)	; read the next line
    @SERROR ?
  Loop
  Close(1)

  ; close the output redirection

EndIf


JeffS
(Fresh Scripter)
2007-08-10 02:12 AM
Re: Validating a File Exists on Remote Computers

Thanks for all of your help Glenn. It was odd, I tried your new code and I was still getting incorrect data back. Frustrated that it wasn't working and it should have been I scratched the code and rewrote a new one just to add to our login scripts. Sometimes it is just easier when you don't have to deal with remote machines and just make it seem local.

Here is the code I ended up with:
 Code:
	;RedirectOut sends all of the output information to the file \\server\share\file
	$ = RedirectOutput('\\server\share\filename')
	
	;Checks to see if SymStoreMod.dll exists
	If Exist("C:\Program Files\SymForm API\SymStoreMod.dll")
		;If file exists than it will run this command
		@WKSTA + Chr(9)+@DATE+Chr(9)'File Exists'?
	Else
		;If the file does not exist will run the following command
		@WKSTA + Chr(9)+@DATE+Chr(9)+'File Does Not Exist' ?
	EndIf
	
	;Closes the output redirection
	$ = RedirectOutput('')

:End 


I am going to let the code run for a few days in the login script and then go through the log file, erase any duplicates and what not, and hopefully get an accurate listing of what computers are missing the DLL file.

Thanks again for your help. Any comments on this new code would be helpful just so I can learn if I am doing things correctly or not.


LonkeroAdministrator
(KiX Master Guru)
2007-08-10 02:53 AM
Re: Validating a File Exists on Remote Computers

 Quote:

; not only good practice, but needed because you are using "C$" in a string
; to specify the admin share!
$ = SetOption('NoVarsInStrings', 'On')

heh, since when did vars have $-sign at the end?