Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ExitWithCode {
- param
- (
- $exitcode
- )
- $host.SetShouldExit($exitcode)
- exit
- }
- function Write-Log {
- <#
- .SYNOPSIS
- Write-Log writes a message to a logfile
- .DESCRIPTION
- The Write-Log function is designed to add logging capability to other scripts.
- In addition to writing output and/or verbose you can write to a log file for
- later debugging.
- #>
- [CmdletBinding()]
- Param
- (
- [Parameter(Mandatory = $true,ValueFromPipelineByPropertyName = $true)]
- [ValidateNotNullOrEmpty()]
- [Alias('LogContent')]
- [string]$Message,
- [Parameter(Mandatory = $false)]
- [ValidateSet("Error", "Info", "Status")]
- [string]$Level = "Info",
- [Parameter(Mandatory = $false)]
- [Alias('LogPath')]
- [string]$Path = 'C:dataloopADHealthCheck.log'
- )
- BEGIN {
- [string]$FormattedDate = Get-Date -Format "dd-MM-yyyy HH:mm"
- If (-NOT (Test-Path $path)) {
- Write-Verbose "Creating $Path"
- [System.IO.FileInfo]$LogFile = New-Item $Path -Force -ItemType file
- }
- }
- PROCESS {
- [string]$LogLine = "$FormattedDate - $Level - $message"
- $LogLine | Out-File -FilePath $Path -Append
- Switch ($Level) {
- "Info" {Write-Verbose $LogLine}
- "Status" {Write-Output $LogLine}
- "Error" {Write-Error $LogLine}
- }
- }
- END {}
- }
- function Get-ADHealthCheck {
- [CmdletBinding()]
- param()
- BEGIN {
- Write-Log "Beginning the AD Health Check..."
- }
- PROCESS {
- $DCs = Get-ADDomainController -Filter * |sort name
- Write-Log "$($DCs.Count) Domain Controllers found" -level Info
- $results = @()
- ForEach ($DC in $DCs) {
- Write-Log "Getting replication metadata for $($DC.HostName)" -level Status
- $ReplStatuses = Get-ADReplicationPartnerMetadata -target $DC.HostName -PartnerType Both -ErrorAction SilentlyContinue
- If ($ReplStatuses) {
- Write-Log "$($ReplStatuses.Count) replication links found for $($DC.HostName)" -level Info
- ForEach ($ReplStatus in $ReplStatuses) {
- $Partner = $ReplStatus.Partner.Split(",")[1].Replace("CN=","")
- $results += [pscustomobject] @{
- 'Source DC' = $DC.HostName.ToUpper()
- 'Partner DC' = (Get-ADComputer $Partner).DNSHostName.ToUpper()
- 'Direction' = $ReplStatus.PartnerType
- 'Type' = $ReplStatus.IntersiteTransportType
- 'Last Attempt' = $ReplStatus.LastReplicationAttempt
- 'Last Success' = $ReplStatus.LastReplicationSuccess
- 'Last Result' = $ReplStatus.LastReplicationResult
- }
- }
- }
- Else {
- Write-Log "Unable to get replication status for $($DC.HostName)" -level Error
- $results += [pscustomobject] @{
- 'Source DC' = $DC.HostName.ToUpper()
- 'Partner DC' = "N/A"
- Direction = "N/A"
- Type = "N/A"
- 'Last Attempt' = "N/A"
- 'Last Success' = "N/A"
- 'Last Result' = "N/A"
- }
- }
- }
- ForEach ($result in $results) {
- If ("$($results.'Last Result')" -eq "0") {
- Write-Log "There were no replication issues found" -Level Info
- ExitWithCode -exitcode 0
- }
- Else {
- Write-Log "These domain controllers have replication errors. Please review them..." -Level Error
- $error = $results | where {"$($_.'Last Result')" -ne "0"} | select 'Source DC','Partner DC','Direction' | ft -AutoSize
- Write-Log $error -Level Error
- ExitWithCode -exitcode 2
- }
- }
- }
- }
- Get-ADHealthCheck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement