This Module contains several functions to pull information from the database used by BES. This enables fast access to information, and the possibility of scripting with this information.

It contains the following functions:

    • Get-BESUser()
    • Get-BESUserStats() 
    • Get-BESCALs()
    • Get-BESServerStats()

Each function has at least basic help that can be read by using the Get-Help cmdlet with the function name.

Use the Import-Module cmdlet to import the module before use.

 

Source Code

  1. ################################################################################
  2. # DFSR-Report.ps1 - DFS replication report                                     #
  3. # Author: John Sneddon                                                         #
  4. # Version: 1.0.5                                                               #
  5. #                                                                              #
  6. ################################# REQUIREMENTS #################################
  7. # Run this from the DFS host you want to report against - ideally a hub server #
  8. # if you want data on all connections                                          #
  9. #                                                                              #
  10. ################################## PARAMETERS ##################################
  11. # $partner - Comma seperated array of partner servers to report against        #
  12. #                                                                              #
  13. ############################### VERSION HISTORY ################################
  14. # 1.0.0 - Initial release                                                      #
  15. # 1.0.1 - Added parameter to restrict report to specified partner server(s)    #
  16. # 1.0.2 - Added date and report conditions to report. Also appends date/time   #
  17. #         to output filename                                                   #
  18. # 1.0.3 - Bug fixes to email report. Seperate XSL is used to remove javascript #
  19. #         elements and CSS fixes due to HTML rendering in Outlook              #
  20. # 1.0.4 - Option to only send email when there is a backlog                    #
  21. # 1.0.5 - Updated True False section for only sending when backlog present     #
  22. #		  (Known Bug - if you computer name is longer than 15 characters       #
  23. #         the ping section will error.  you can just add a 15 character long   #
  24. #         name in DNS to solve this)                                           #
  25. #                                                                              #
  26. ################################################################################
  27. param([string[]]$partner, [string]$mailTo)
  28.  
  29. $smtpServer = "MailServer"					 	
  30. $emailFrom = '"DFS-R Report" '
  31.  
  32. # Set this to true if you only want to be alerted when there is a backlog
  33.  
  34. $suppressEmail = $false;
  35.  
  36. # Highlight backlog count on console if above this number
  37. $BacklogErrorLevel = 10
  38.  
  39. ## Setup my variables
  40. $ping = New-Object System.Net.NetworkInformation.Ping
  41. $ComputerName = $env:ComputerName
  42. $backlogPresent = $false
  43.  
  44. ## Query DFSR groups from the local MicrosftDFS WMI namespace.
  45. $DFSRGroupWMIQuery = 'SELECT FROM DfsrReplicationGroupConfig'
  46. $RGroups = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $DFSRGroupWMIQuery
  47.  
  48. $xml = New-Object xml
  49. [void]$xml.PrependChild($xml.CreateProcessingInstruction('xml-stylesheet', "type='text/xsl' href='report.xsl'"))
  50. $root = $xml.CreateElement("DFS")
  51. $root.SetAttribute("date", (get-date -format "HH:mm dd/MM/yyyy") )
  52. $root.SetAttribute("server", $ComputerName )
  53. if (!$partner)
  54. {
  55. 	$root.SetAttribute("partners", "all partner servers" )
  56. }
  57. else
  58. {
  59. 	$root.SetAttribute("partners", "$partner" )
  60. }
  61. [void]$xml.AppendChild($root)
  62.  
  63. foreach ($Group in $RGroups)
  64. {
  65. 	if ($RepGrpXML)
  66. 	{
  67. 		Clear-Variable -name RepGrpXML
  68. 	}
  69. 	## Cycle through all Replication groups found
  70. 	$DFSRGFoldersWMIQuery = "SELECT FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
  71. 	$RGFolders = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $DFSRGFoldersWMIQuery
  72.  
  73. 	## Grab all connections associated with a Replication Group
  74. 	$DFSRConnectionWMIQuery = "SELECT FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
  75. 	$RGConnections = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query $DFSRConnectionWMIQuery	
  76. 	foreach ($Connection in $RGConnections)
  77. 	{
  78. 		$ConnectionName = $Connection.PartnerName.Trim()
  79. 		$IsInBound = $Connection.Inbound
  80. 		$IsEnabled = $Connection.Enabled
  81.  
  82. 		## Do not attempt to look at connections that are Disabled
  83. 		if ($IsEnabled -eq $True)
  84. 		{  
  85. 			## If the connection is not ping-able, do not attempt to query it for Backlog info
  86. 			$Reply = $ping.send("$ConnectionName")
  87. 			if ($reply.Status -eq "Success")
  88. 			{
  89. 				## Cycle through the Replication Folders that are part of the replication group and run DFSRDIAG tool to determine the backlog on the connection partners.
  90. 				foreach ($Folder in $RGFolders)
  91. 				{	
  92. 					$RGName = $Group.ReplicationGroupName
  93. 					$RFName = $Folder.ReplicatedFolderName
  94.  
  95. 					## Determine if current connect is an inbound connection or not, set send/receive members accordingly
  96. 					if ($IsInBound -eq $True)
  97. 					{
  98. 						$SendingMember = $ConnectionName
  99. 						$ReceivingMember = $ComputerName
  100. 					}
  101. 					else
  102. 					{
  103. 						$SendingMember = $ComputerName
  104. 						$ReceivingMember = $ConnectionName
  105. 					}
  106.  
  107. 					if ((!$partner) -or (($partner -contains $SendingMember) -and ($ReceivingMember -eq $ComputerName)) -or (($partner -contains $ReceivingMember) -and ($SendingMember -eq $ComputerName)))
  108. 					{
  109. 						$RGName + ":" + $RFName +  " - S:"+$SendingMember + " R:" + $ReceivingMember 
  110.  
  111. 						## Execute the dfsrdiag command and get results back in the $Backlog variable
  112. 						$BLCommand = "dfsrdiag Backlog /RGName:'" + $RGName + "' /RFName:'" + $RFName + "' /SendingMember:" + $SendingMember + " /ReceivingMember:" + $ReceivingMember
  113. 						$Backlog = Invoke-Expression -Command $BLCommand
  114.  
  115. 						$BackLogFilecount = 0
  116. 						foreach ($item in $Backlog)
  117. 						{
  118. 							if ($item -ilike "*Backlog File count*")
  119. 							{
  120. 								$BacklogFileCount = [int]$Item.Split(":")[1].Trim()
  121. 							}
  122. 						}
  123. 						if ($BacklogFileCount -gt $BacklogErrorLevel)
  124. 						{
  125. 							$outColour = "red"
  126. 						}
  127. 						else
  128. 						{
  129. 							$outColour = "green"
  130. 						}
  131.  
  132. 						if ($BacklogFileCount -gt 0)
  133.  
  134. 						{
  135.  
  136. 							$backlogPresent = $true;
  137.  
  138. 						}
  139. 						#Write-Host "$RGName: $RFName has $BacklogFilecount files in the backlog from $SendingMember to $ReceivingMember`n" -ForegroundColor $outColour
  140. 						Write-Host "$BacklogFilecount files in backlog`n" -ForegroundColor $outColour
  141.  
  142. 						$ConnXML = $xml.CreateElement("Connection")
  143. 						$ConnXML.SetAttribute("foldername", $RFName)
  144. 						$ConnXML.SetAttribute("Smem", $SendingMember)
  145. 						$ConnXML.SetAttribute("Rmem", $ReceivingMember) 
  146. 						$ConnXML.SetAttribute("backlogcount", $BacklogFilecount) 
  147.  
  148. 						if ($BacklogFilecount -gt 0 )
  149. 						{
  150. 							$blXML = $xml.CreateElement("backlog")
  151. 							foreach ($line in $Backlog)
  152. 							{
  153. 								if ($line -match "([0-9]*)\. (.*)")
  154. 								{
  155. 									$blitemXML = $xml.CreateElement("file")
  156. 									$blitemXML.innerText = $Matches[2]
  157. 									[void]$blXML.AppendChild($blitemXML)							
  158. 								}
  159. 							}
  160. 							[void]$ConnXML.AppendChild($blXML)							
  161. 						}
  162. 						# if the replication group XML doesn't exist, add it. Do this to avoid empty elements
  163. 						if (!$RepGrpXML)
  164. 						{
  165. 							$RepGrpXML = $xml.CreateElement("ReplicationGroup")
  166. 							$RepGrpXML.SetAttribute("name", $Group.ReplicationGroupName)
  167. 						}
  168. 						[void]$RepGrpXML.AppendChild($ConnXML)
  169. 					}
  170. 				}
  171. 			}
  172. 			else
  173. 			{ 
  174. 				Write-Host $ConnectionName "is not pingable" 
  175. 				"Server """ + $ConnectionName + """ could not be reached.`nPlease verify it is on the network and pingable."
  176. 			}
  177. 		}		
  178. 	}
  179. 	if ($RepGrpXML)
  180. 	{
  181. 		[void]$root.AppendChild($RepGrpXML)
  182. 	}
  183. }
  184. $date = get-date -format "yyyy-MM-dd_HH-mm"
  185. $xml.save("$pwd\$date-DFSR-Report.xml")
  186.  
  187. Write-Host "Completed Report bit" 
  188. Write-Host $backlogPresent
  189. Write-Host $suppressEmail
  190.  
  191.  
  192.  
  193. # Email the report
  194. if ($mailTo -and ($backlogPresent -and -not $suppressEmail))
  195. { 
  196. 	# apply the XSL transform - first try .Net 3.5 as is is quicker
  197. 	$EAP = $ErrorActionPreference
  198. 	$ErrorActionPreference = "SilentlyContinue"
  199. 	$script:xslt = new-object system.xml.xsl.xslcompiledtransform
  200. 	trap [System.Management.Automation.PSArgumentException] 
  201. 	{  # no 3.5, use the slower 2.0 one
  202. 		$ErrorActionPreference = $EAP
  203. 		$script:xslt = new-object system.xml.xsl.xsltransform
  204. 	}
  205. 	$ErrorActionPreference = $EAP
  206.  
  207. 	# load xslt file and transform
  208. 	$xslt.load( "$pwd\email.xsl" )
  209. 	$xslt.Transform( "$pwd\$date-DFSR-Report.xml", "$pwd\$date-DFSR-Report.html" )
  210.  
  211. 	"`nEmailing Report...`n"
  212. 	# setup email variables
  213. 	if (!$partner)
  214. 	{
  215. 		$subject = "DFS-R Report between $ComputerName and all partners"
  216. 	}
  217. 	else
  218. 	{
  219. 		$subject = "DFS-R Report between $ComputerName and $partner"
  220. 	}
  221. 	$msg = New-Object System.Net.Mail.MailMessage $emailFrom, $mailTo, $subject, (Get-Content "$pwd\$date-DFSR-Report.html")
  222. 	$msg.IsBodyHtml = $true;
  223.  
  224. 	# send email
  225. 	$smtp = new-object Net.Mail.SmtpClient($smtpServer)
  226. 	$smtp.Send($msg)
  227. } 
comments powered by Disqus