Skip to content

Test-DbaAgPolicyState - Add new command for Always On policy state checks#10246

Open
andreasjordan wants to merge 2 commits intodevelopmentfrom
claude/issue-9911-20260314-1957
Open

Test-DbaAgPolicyState - Add new command for Always On policy state checks#10246
andreasjordan wants to merge 2 commits intodevelopmentfrom
claude/issue-9911-20260314-1957

Conversation

@andreasjordan
Copy link
Collaborator

Creates a new Test-DbaAgPolicyState command that evaluates SQL Server Availability Groups against Microsoft's predefined Always On policies.

Implements the first 7 of 13 predefined policies (server and AG facets) as a starting point, all using the AvailabilityGroupState SMO object.

Related to #9911

Generated with Claude Code

…ecks

Implements the first 7 of the 13 predefined Microsoft Always On policies
(server and availability group facets) as a starting point.

Covers:
- WSFC Cluster State (Critical, Server facet)
- Availability Group Online State (Critical, AG facet)
- Availability Group Automatic Failover Readiness (Critical, AG facet)
- Availability Replicas Connection State (Warning, AG facet)
- Availability Replicas Data Synchronization State (Warning, AG facet)
- Availability Replicas Role State (Warning, AG facet)
- Synchronous Replicas Data Synchronization State (Warning, AG facet)

Each policy uses the AvailabilityGroupState SMO object and returns IsHealthy
as a boolean along with PolicyName, Category, Facet, Issue, and Details.

Related to #9911

(do Test-DbaAgPolicyState)

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Collaborator Author

I will close #10233 and #10234 and @ReeceGoding and I will try to use this pull request as a starting point.

I will test this tomorrow in my lab.

@ReeceGoding feel free to test as well and to add comments here with things we should change.

@andreasjordan
Copy link
Collaborator Author

"Issue" is always filled with the suitable text if IsHealthy is $false. Sould we set "Issue" to $null if IsHealthy is $true?

@andreasjordan
Copy link
Collaborator Author

The parameters $Secondary and $SecondarySqlCredential are currently not used. If they are never used, we should remove them.

@ReeceGoding
Copy link
Contributor

ReeceGoding commented Mar 22, 2026

Played around with it. It is definitely close to the right thing. This is going to be good!

image

Initial thoughts

  • It is currently returning everything, even the healthy stuff.
  • I wonder if the WSFC Cluster State test is dishonest. My lab is clusterless, but this tests reports that my cluster is healthy.
  • I agree with you that Issue should be blank when there is no issue. Details already tells us anything else we would want to know.

Breaking it

When I deliberately tried to break it, I was surprised to see that pulling the plug on my secondary did not cause any issues to be flagged.

image

I suspect that a .Refresh() is missing somewhere.

image

Come to think of it, docker stop would be a good tool for integration tests. My others ideas for them have been lacking.

@andreasjordan
Copy link
Collaborator Author

@claude please read our comments. Then implement all the over policies, but also only fill the Issue property in case there is an issue.

@claude
Copy link

claude bot commented Mar 22, 2026

Claude finished @andreasjordan's task in 4m 56s —— View job


Changes implemented

  • Read existing implementation and comments
  • Research remaining 6 policies
  • Fix Issue property to be $null when IsHealthy is $true
  • Add Replica and Database properties to output (needed for replica/database-level policies)
  • Add $ag.Refresh() to get current state (addresses ReeceGoding's concern about stale data)
  • Implement 3 availability replica-level policies
  • Implement 3 availability database-level policies
  • Update .OUTPUTS documentation
  • Commit and push changes

Summary

All 13 predefined Always On policies are now implemented. Here's what changed:

Issue property fix: Now $null when IsHealthy is $true, only populated with the issue description when there is actually a problem.

$ag.Refresh(): Added before evaluating policies to get current state rather than cached data.

New output properties: Added Replica and Database columns — $null for server/AG-level policies, populated for replica/database-level policies so you can identify exactly which replica or database has an issue.

6 new policies added (replica-level):

PolicyName Category Facet Condition
Availability Replica Role State Warning Availability replica Role = Primary or Secondary
Availability Replica Connection State Warning Availability replica Connected or Role = Primary
Availability Replica Joined State Warning Availability replica JoinState ≠ NotJoined

(database-level):

PolicyName Category Facet Condition
Availability Database Synchronization State Critical Availability database SynchronizationState = Synchronized or Synchronizing
Availability Database Suspension State Warning Availability database IsSuspended = False
Availability Database Join State Warning Availability database IsJoined = True

|

…, add Refresh

- Add 6 remaining policies: Availability Replica Role/Connection/Joined State
  and Availability Database Synchronization/Suspension/Join State
- Fix Issue property to be null when IsHealthy is true (only populate on failure)
- Add Replica and Database output properties for replica/database-level policies
- Add $ag.Refresh() to ensure current state is evaluated
- Update .OUTPUTS documentation to reflect new properties

(do Test-DbaAgPolicyState)

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Collaborator Author

I just tested with SuspendDataMovement():

image

@andreasjordan
Copy link
Collaborator Author

I think it should return also the healthy stuff like other Test- commands do as well.

@andreasjordan
Copy link
Collaborator Author

The ClusterQuorumState is maybe missleading. In my test I use -ClusterType None. When I have time I will setup a new windows cluster and test the command there.

@andreasjordan
Copy link
Collaborator Author

But I think this is a good starting point and ready to be published so people can test this in their enviroments.

@andreasjordan andreasjordan marked this pull request as ready for review March 22, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants