Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. function ExitWithCode {
  2. param
  3. (
  4. $exitcode
  5. )
  6.  
  7. $host.SetShouldExit($exitcode)
  8. exit
  9. }
  10.  
  11. function Write-Log {
  12. <#
  13. .SYNOPSIS
  14. Write-Log writes a message to a logfile
  15.  
  16. .DESCRIPTION
  17. The Write-Log function is designed to add logging capability to other scripts.
  18. In addition to writing output and/or verbose you can write to a log file for
  19. later debugging.
  20. #>
  21. [CmdletBinding()]
  22. Param
  23. (
  24. [Parameter(Mandatory = $true,ValueFromPipelineByPropertyName = $true)]
  25. [ValidateNotNullOrEmpty()]
  26. [Alias('LogContent')]
  27. [string]$Message,
  28.  
  29. [Parameter(Mandatory = $false)]
  30. [ValidateSet("Error", "Info", "Status")]
  31. [string]$Level = "Info",
  32.  
  33. [Parameter(Mandatory = $false)]
  34. [Alias('LogPath')]
  35. [string]$Path = 'C:dataloopADHealthCheck.log'
  36.  
  37. )
  38.  
  39. BEGIN {
  40.  
  41. [string]$FormattedDate = Get-Date -Format "dd-MM-yyyy HH:mm"
  42.  
  43. If (-NOT (Test-Path $path)) {
  44. Write-Verbose "Creating $Path"
  45. [System.IO.FileInfo]$LogFile = New-Item $Path -Force -ItemType file
  46. }
  47. }
  48. PROCESS {
  49. [string]$LogLine = "$FormattedDate - $Level - $message"
  50. $LogLine | Out-File -FilePath $Path -Append
  51.  
  52. Switch ($Level) {
  53.  
  54. "Info" {Write-Verbose $LogLine}
  55. "Status" {Write-Output $LogLine}
  56. "Error" {Write-Error $LogLine}
  57. }
  58. }
  59. END {}
  60. }
  61.  
  62. function Get-ADHealthCheck {
  63. [CmdletBinding()]
  64. param()
  65.  
  66. BEGIN {
  67. Write-Log "Beginning the AD Health Check..."
  68. }
  69.  
  70.  
  71. PROCESS {
  72. $DCs = Get-ADDomainController -Filter * |sort name
  73.  
  74. Write-Log "$($DCs.Count) Domain Controllers found" -level Info
  75.  
  76. $results = @()
  77.  
  78. ForEach ($DC in $DCs) {
  79.  
  80. Write-Log "Getting replication metadata for $($DC.HostName)" -level Status
  81. $ReplStatuses = Get-ADReplicationPartnerMetadata -target $DC.HostName -PartnerType Both -ErrorAction SilentlyContinue
  82.  
  83. If ($ReplStatuses) {
  84.  
  85. Write-Log "$($ReplStatuses.Count) replication links found for $($DC.HostName)" -level Info
  86.  
  87. ForEach ($ReplStatus in $ReplStatuses) {
  88.  
  89. $Partner = $ReplStatus.Partner.Split(",")[1].Replace("CN=","")
  90.  
  91. $results += [pscustomobject] @{
  92. 'Source DC' = $DC.HostName.ToUpper()
  93. 'Partner DC' = (Get-ADComputer $Partner).DNSHostName.ToUpper()
  94. 'Direction' = $ReplStatus.PartnerType
  95. 'Type' = $ReplStatus.IntersiteTransportType
  96. 'Last Attempt' = $ReplStatus.LastReplicationAttempt
  97. 'Last Success' = $ReplStatus.LastReplicationSuccess
  98. 'Last Result' = $ReplStatus.LastReplicationResult
  99. }
  100. }
  101. }
  102.  
  103. Else {
  104.  
  105. Write-Log "Unable to get replication status for $($DC.HostName)" -level Error
  106. $results += [pscustomobject] @{
  107. 'Source DC' = $DC.HostName.ToUpper()
  108. 'Partner DC' = "N/A"
  109. Direction = "N/A"
  110. Type = "N/A"
  111. 'Last Attempt' = "N/A"
  112. 'Last Success' = "N/A"
  113. 'Last Result' = "N/A"
  114. }
  115. }
  116. }
  117.  
  118. ForEach ($result in $results) {
  119.  
  120. If ("$($results.'Last Result')" -eq "0") {
  121.  
  122. Write-Log "There were no replication issues found" -Level Info
  123. ExitWithCode -exitcode 0
  124. }
  125.  
  126. Else {
  127.  
  128. Write-Log "These domain controllers have replication errors. Please review them..." -Level Error
  129. $error = $results | where {"$($_.'Last Result')" -ne "0"} | select 'Source DC','Partner DC','Direction' | ft -AutoSize
  130. Write-Log $error -Level Error
  131. ExitWithCode -exitcode 2
  132. }
  133. }
  134. }
  135. }
  136. Get-ADHealthCheck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement