Powershell Catch Error: Unable to find a default server with Active Directory Web Services running

1k Views Asked by At

Need help to catch the error. Whenever my network connection is unstable my get-ad script will be terminated.

    $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path

$Date = Get-Date -Format yyyy_MM_dd_THHmm

Start-Transcript -Path "$ScriptDir\Logs.log" -Append

$List = Import-Csv "$ScriptDir\Accounts.csv"

$TotalCount = $List.Length

$CurrentCount = 0

Write-Host 'Total Count: '$TotalCount

    ForEach ($user in $List) {

 # Retrieve UserSamAccountName and ADGroup
$Groups = $User.Group
$UserSam = $User.SamAccountName

# Retrieve SamAccountName and ADGroup
$ADUser = Get-ADUser -Filter "SamAccountName -eq '$UserSam'" | Select-Object SamAccountName
$ADGroups = Get-ADGroup -Filter * | Select-Object Name

    # User does not exist in AD
    if ($ADUser -eq $null) {
        Write-Host "$UserSam does not exist in AD" -ForegroundColor DarkCyan
        Continue
    }

    # User does not have a group specified in CSV file
    if ($Groups -eq $null) {
        Write-Host "$UserSam has no group specified in CSV file" -ForegroundColor Yellow
        Continue
    }

    # Retrieve AD user group membership
    $ExistingGroups = Get-ADPrincipalGroupMembership $UserSam | Select-Object Name

    foreach ($Group in $Groups.Split(';')) {
        # Group does not exist in AD
        if ($ADGroups.Name -notcontains $Group) {
            Write-Host "$Group group does not exist in AD" -ForegroundColor Gray
            Continue
        }

     # User already member of group
        if ($ExistingGroups.Name -eq $Group) {
            Write-Host "$UserSam already exists in group $Group." -ForeGroundColor Yellow
        }

        else {
            # User not a member of group
            Write-Host "User $UserSam doesn't exists in $Group AD group" -ForegroundColor Red
        }

}

    $CurrentCount++
 $Percentage = [math]::Round($CurrentCount/$TotalCount*100)
 Write-Progress -Activity "Building report in progress" -Status "$Percentage% Complete" -PercentComplete $Percentage

}

Stop-Transcript
Write-Output 'Script complete.'

OUTPUT ERROR

    Get-ADUser : Unable to find a default server with Active Directory Web Services running.
At C:\Users\JVERE05\Desktop\Powershell\Powershell\CheckIfExist\08_08.ps1:22 char:11
+ $ADUser = Get-ADUser -Filter "SamAccountName -eq '$UserSam'" | Select ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ADUser], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.GetADUser
1

There are 1 best solutions below

0
Mathias R. Jessen On

The fully qualified type name is Microsoft.ActiveDirectory.Management.ADServerDownException:

try {
    Get-ADUser ... -ErrorAction Stop
}
catch [Microsoft.ActiveDirectory.Management.ADServerDownException] {
    <# AD Server is down #>
}
catch {
    <# Something else went wrong #>
}