Submitted By: Anonymous Submission

Searches for orphaned DFS Links and writes results to a log file. Syntax: cscript.exe checkdfs.vbs \\DFSROOT$ c:\temp\dfs_log.txt

Visual Basic
Edit|Remove
 Option Explicit
'========================================================================================
'
' NAME: checkdfslinks.vbs v1
'
' Zweck:  Auffinden von orphaned Links im DFS
'	  Search orphanded DFS-Link
'
' AUTHOR: JSE2006
' Mail: x
' DATE  : 14.01.2006
'
' COMMENT: 	Liest aus Dfsrootx (argurment)die DfsLinks+deren Targets aus und setzt ein
'		GetFolder auf das Target => wenn erfolgreich, dann existiert das
'		Ziel; ansonsten ist das Ziel vielleicht nicht mehr da...toter Link
'
'		Check, wheather a DFS-Link's target is accessible or not.
'		Why ? To find orphaned DFS-Links
'
' Syntax:	e.g.: cscript.exe checkdfs.vbs \\DFSROOT$ c:\temp\dfs_log.txt
' Requirement:  DFSCMD.exe is used by the script
'==========================================================================================

Function checklink (pfad)
   Dim f, o
   Set f = CreateObject("Scripting.FileSystemObject")
   On Error Resume next
   		Set o = f.GetFolder(pfad)
   		If Err.Number=0 Then checklink = True Else checklink=Err.Description
   On Error GoTo 0
End Function

Function suchen(ro)
	Dim  befehl, objExec, objShell, zeile, pos1, pos2, pos3, pos4, dfslink , dfstarget
	befehl= "%windir%\system32\dfscmd.exe /view " & ro & "  /batchrestore "  
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec(befehl)
    Do While objExec.StdOut.AtEndOfStream <> True
      zeile = objExec.StdOut.readline
      If InStr (zeile,"\\") Then 
       ' chr(34) = " 
       pos1 = InStr (zeile, Chr(34) ) + 1
       pos2 = InStr (pos1+1,zeile, Chr(34)) 
       dfslink   = Mid(zeile,pos1+1,pos2-pos1-1)
       pos3 = InStr (pos2+1,zeile,Chr(34))
       pos4 = InStr (pos3+1,zeile,Chr(34))
       dfstarget = Mid(zeile,pos3+1,pos4-pos3-1)
       suchen = suchen & dfslink & TAB & dfstarget & TAB & checklink(dfstarget) & CRLF
      End If
    Loop  
 end Function

'==========================================================================
 Const ForReading = 1, ForWriting = 2
 Dim aa, dfs,objArgs,CRLF, TAB, FSO, FL, Auswertung
 CRLF = Chr(13) & Chr(10)
 TAB  = Chr (9)
'==========================================================================   
  Set objArgs = WScript.Arguments
  if objargs.count<>2 then 
	WScript.StdOut.Write "Syntax e.g.: cscript.exe checkdfslinks.vbs \\DFSROOT$ c:\temp\dfs_log.txt"
	wscript.quit
  end if 
  dfs=objArgs(0)
  Auswertung=objArgs(1)
  

  Set fso = CreateObject("Scripting.FileSystemObject")
  on error resume next
	  Set fl = fso.CreateTextFile(Auswertung)
   	   if err.number<> 0 then 
 			WScript.StdOut.Writeline "Cannot create LogFile: " & err.description
			wscript.quit
	   end if
	   set aa = fso.getfolder(dfs)
 	   if err.number <> 0 then 
 			WScript.StdOut.Writeline "DFS " & dfs & ": " &  err.description
			wscript.quit	   
           end if
  on error goto 0
  fl.WriteLine suchen(dfs)
  WScript.StdOut.Writeline 
  WScript.StdOut.Writeline " Finished...take a look at " & Auswertung & "."
  WScript.StdOut.Writeline
  fl.Close