[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DFS Monitoring VB Script

Posted on 2011-05-04
3
Medium Priority
?
558 Views
Last Modified: 2012-05-11
I need to run this but include a line wherein if the DFS link/s fail/s I need to send an email to certain people. Can someone please provide assistance? thank you...

'Designate the DFS server you want to query
strComputer = "yourfileserver"
'Number of times you want to loop, 0 is indefinite
numLoop = 0
'Wait time between queries - for large DFS trees make this longer
'Time is in minutes
numWait = 5


'**** Do not edit beyond this point!  ****
Dim objExplorer, binFirstWindow, strFiles

If numLoop = 0 Then
	numSubtract = 0
Else
	numSubtract = 1
	numLoop = numLoop - 1
End If

'Convert minutes to miliseconds
numWait = (numWait * 60) * 1000

showTable("Running...")
Do Until numLoop < 0
	Call Main()
	numLoop = numLoop - numSubtract
	Wscript.Sleep numWait
Loop

wscript.echo "Done!"

Sub Main
	strHTML = "<table border=" & chr(34) & "1" & chr(34) & ">" & vbCRLF
	strHTML = strHTML & "<th>Replication Group</th><th>Sending Partner</th><th>Receiving Partner</th><th>Backlog</th><th>Files</th>" & vbCRLF
	Set oWMIService = GetObject("winmgmts:\\" & strComputer & "\root\MicrosoftDFS")
	Set colRGroups = oWMIService.ExecQuery("SELECT * FROM DfsrReplicationGroupConfig")
	For Each oGroup in colRGroups
		Set colRGFolders = oWMIService.ExecQuery("SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='" & oGroup.ReplicationGroupGUID & "'")
		For Each oFolder in colRGFolders
			Set colRGConnections = oWMIService.ExecQuery("SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='" & oGroup.ReplicationGroupGUID & "'")
			For Each oConnection in colRGConnections
			
				binInbound = oConnection.Inbound
				strPartner = oConnection.PartnerName
				strRGName = oGroup.ReplicationGroupName
				strRFName = oFolder.ReplicatedFolderName
				If binInbound = True Then
					strSendingComputer = strPartner
					strReceivingComputer = strComputer
				Else
					strSendingComputer = strComputer
					strReceivingComputer = strPartner
				End If
				If oConnection.Enabled = True Then
					strHTML = strHTML & "<tr>" & vbCRLF
					strHTML = strHTML & "<td>" & strRGName & "</td>" & vbCRLF
					strHTML = strHTML & "<td>" & strSendingComputer & "</td>" & vbCRLF
					strHTML = strHTML & "<td>" & strReceivingComputer & "</td>" & vbCRLF
					numBackLog = getBackLog(strSendingComputer, strReceivingComputer, strRGName, strRFName)
					If numBackLog = 0 Then
						strHTML = strHTML & "<td>" & numBackLog & "</td>" & vbCRLF
					Else
						strHTML = strHTML & "<td style=" & chr(34) & "background-color:red" & chr(34) & ">" & numBackLog & "</td>" & vbCRLF
					End If
					strHTML = strHTML & "<td>" & strFiles & "</td>" & vbCRLF
					strHTML = strHTML & "</tr>" & vbCRLF
					'wscript.echo strHTML
				End If
			Next
		Next
	Next
	strHTML = strHTML & "</table>" & vbCRLF
	'wscript.echo strHTML
	showTable(strHTML)
End Sub


Function getBackLog(strSending, strReceiving, strReplicationGroup, strReplicatedFolder)
	Set WshShell = CreateObject ("Wscript.shell")
	'wscript.echo "dfsrdiag Backlog /RGName:" & strReplicationGroup & " /RFName:" & strReplicatedFolder & " /SendingMember:" & strSending & " /ReceivingMember:" & strReceiving
	Set objExec = WSHshell.Exec("dfsrdiag.exe Backlog /RGName:" & strReplicationGroup & " /RFName:" & strReplicatedFolder & " /SendingMember:" & strSending & " /ReceivingMember:" & strReceiving)
	strResult = ""
	Do While Not objExec.StdOut.AtEndOfStream
		strResult = strResult & objExec.StdOut.ReadLine() & "\\"
	Loop
	'wscript.echo strResult
	strTemp = "&nbsp;"
	If InStr(strResult, "No Backlog") > 0 Then
		getBackLog = 0
	Else
		arrLines = Split(strResult, "\\")
		arrResult = Split(arrLines(1), ":")
		getBackLog = arrResult(1)
		If UBound(arrLines) > 1 Then
			strTemp = ""
			For x = 2 to UBound(arrLines)
				If InStr(arrLines(x), "Succeeded") = 0 and arrLines(x) <> "" Then
					strTemp = strTemp & arrLines(x) & "<br>"
				End If
			Next
			'wscript.echo strTemp
		End If
	End If
	strFiles = strTemp
End Function


Sub showTable(strText)
	If binFirstWindow = False Then
		binFirstWindow = True
		Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
		With objExplorer
			.Navigate "about:blank"   
			.ToolBar = 0
			.StatusBar = 0
			.Width = 750
			.Height = 600 
			.Left = 0
			.Top = 0
		End With
		
		Do While (objExplorer.Busy)
			Wscript.Sleep 200
		Loop
		objExplorer.Visible = 1 
	End If

	On Error Resume Next
	objExplorer.Document.Body.InnerHTML = strText
	If Err.Number <> 0 Then
		Wscript.Quit
	End If
	On Error Goto 0
End Sub

Open in new window

0
Comment
Question by:mmoya
  • 2
3 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35695443
Hi, in the current script, what determines where it fails?  Or doesn't it have that yet?  Is the WMI connection error you are talking about?

Rob.
0
 

Author Comment

by:mmoya
ID: 35706589
Fails = when DFS link is unaccessible then send email to certain users for acknowledgement...
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35710587
Does the script currently error out when a link failure occurs?  I'm just trying to figure out where to put the email functionality.....

If you run this code, it should not error out, but will write to the HTML an error when the WMI call fails.  If that's the time when you want to send the email, let me know.

Regards,

Rob.
'Designate the DFS server you want to query
strComputer = "yourfileserver"
'Number of times you want to loop, 0 is indefinite
numLoop = 0
'Wait time between queries - for large DFS trees make this longer
'Time is in minutes
numWait = 5


'**** Do not edit beyond this point!  ****
Dim objExplorer, binFirstWindow, strFiles

If numLoop = 0 Then
	numSubtract = 0
Else
	numSubtract = 1
	numLoop = numLoop - 1
End If

'Convert minutes to miliseconds
numWait = (numWait * 60) * 1000

showTable("Running...")
Do Until numLoop < 0
	Call Main()
	numLoop = numLoop - numSubtract
	Wscript.Sleep numWait
Loop

wscript.echo "Done!"

Sub Main
	strHTML = "<table border=" & chr(34) & "1" & chr(34) & ">" & vbCRLF
	strHTML = strHTML & "<th>Replication Group</th><th>Sending Partner</th><th>Receiving Partner</th><th>Backlog</th><th>Files</th>" & vbCRLF
	On Error Resume Next
	Set oWMIService = GetObject("winmgmts:\\" & strComputer & "\root\MicrosoftDFS")
	Set colRGroups = oWMIService.ExecQuery("SELECT * FROM DfsrReplicationGroupConfig")
	If Err.Number = 0 Then
		For Each oGroup in colRGroups
			Set colRGFolders = oWMIService.ExecQuery("SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='" & oGroup.ReplicationGroupGUID & "'")
			For Each oFolder in colRGFolders
				Set colRGConnections = oWMIService.ExecQuery("SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='" & oGroup.ReplicationGroupGUID & "'")
				If Err.Number = 0 Then
					For Each oConnection in colRGConnections
					
						binInbound = oConnection.Inbound
						strPartner = oConnection.PartnerName
						strRGName = oGroup.ReplicationGroupName
						strRFName = oFolder.ReplicatedFolderName
						If binInbound = True Then
							strSendingComputer = strPartner
							strReceivingComputer = strComputer
						Else
							strSendingComputer = strComputer
							strReceivingComputer = strPartner
						End If
						If oConnection.Enabled = True Then
							strHTML = strHTML & "<tr>" & vbCRLF
							strHTML = strHTML & "<td>" & strRGName & "</td>" & vbCRLF
							strHTML = strHTML & "<td>" & strSendingComputer & "</td>" & vbCRLF
							strHTML = strHTML & "<td>" & strReceivingComputer & "</td>" & vbCRLF
							numBackLog = getBackLog(strSendingComputer, strReceivingComputer, strRGName, strRFName)
							If numBackLog = 0 Then
								strHTML = strHTML & "<td>" & numBackLog & "</td>" & vbCRLF
							Else
								strHTML = strHTML & "<td style=" & chr(34) & "background-color:red" & chr(34) & ">" & numBackLog & "</td>" & vbCRLF
							End If
							strHTML = strHTML & "<td>" & strFiles & "</td>" & vbCRLF
							strHTML = strHTML & "</tr>" & vbCRLF
							'wscript.echo strHTML
						End If
					Next
				Else
					strHTML = strHTML & "<tr>" & vbCrLf
					strHTML = strHTML & "<td colspan=5>WMI Error during query SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='" & oGroup.ReplicationGroupGUID & "'. Error " & Err.Number & ": " & Err.Description & "</td>" & vbCrLf
					strHTML = strHTML & "</tr>" & vbCrLf
					Err.Clear
					On Error Goto 0
				End If
			Next
		Next
	Else
		strHTML = strHTML & "<tr>" & vbCrLf
		strHTML = strHTML & "<td colspan=5>WMI Error connecting to " & strComputer & ". Error " & Err.Number & ": " & Err.Description & "</td>" & vbCrLf
		strHTML = strHTML & "</tr>" & vbCrLf
		Err.Clear
		On Error Goto 0
	End If
	strHTML = strHTML & "</table>" & vbCRLF
	'wscript.echo strHTML
	showTable(strHTML)
End Sub


Function getBackLog(strSending, strReceiving, strReplicationGroup, strReplicatedFolder)
	Set WshShell = CreateObject ("Wscript.shell")
	'wscript.echo "dfsrdiag Backlog /RGName:" & strReplicationGroup & " /RFName:" & strReplicatedFolder & " /SendingMember:" & strSending & " /ReceivingMember:" & strReceiving
	Set objExec = WSHshell.Exec("dfsrdiag.exe Backlog /RGName:" & strReplicationGroup & " /RFName:" & strReplicatedFolder & " /SendingMember:" & strSending & " /ReceivingMember:" & strReceiving)
	strResult = ""
	Do While Not objExec.StdOut.AtEndOfStream
		strResult = strResult & objExec.StdOut.ReadLine() & "\\"
	Loop
	'wscript.echo strResult
	strTemp = "&nbsp;"
	If InStr(strResult, "No Backlog") > 0 Then
		getBackLog = 0
	Else
		arrLines = Split(strResult, "\\")
		arrResult = Split(arrLines(1), ":")
		getBackLog = arrResult(1)
		If UBound(arrLines) > 1 Then
			strTemp = ""
			For x = 2 to UBound(arrLines)
				If InStr(arrLines(x), "Succeeded") = 0 and arrLines(x) <> "" Then
					strTemp = strTemp & arrLines(x) & "<br>"
				End If
			Next
			'wscript.echo strTemp
		End If
	End If
	strFiles = strTemp
End Function


Sub showTable(strText)
	If binFirstWindow = False Then
		binFirstWindow = True
		Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
		With objExplorer
			.Navigate "about:blank"   
			.ToolBar = 0
			.StatusBar = 0
			.Width = 750
			.Height = 600 
			.Left = 0
			.Top = 0
		End With
		
		Do While (objExplorer.Busy)
			Wscript.Sleep 200
		Loop
		objExplorer.Visible = 1 
	End If

	On Error Resume Next
	objExplorer.Document.Body.InnerHTML = strText
	If Err.Number <> 0 Then
		Wscript.Quit
	End If
	On Error Goto 0
End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question