1

I am really new to Powershell and hopefully someone can help me with this error. I have been given the below script that is supposed to take a list of SQL servers from a text file and lists out the availablity group and the replicas for each.

# Import the dbatools and sqlserver modules
Import-Module dbatools
Import-Module sqlserver
# Specify the path to the file containing the list of servers
$serverList = "C:\Path\To\ServerList.txt"
# Read the list of servers from the file
$servers = Get-Content $serverList
# Loop through each server and retrieve availability group information
foreach ($server in $servers) {
 # Get the availability groups on the server
 $groups = Get-DbaAvailabilityGroup -SqlInstance $server
 # Loop through each availability group and retrieve replica information
 foreach ($group in $groups) {
 # Get the replicas in the availability group
 $replicas = Get-DbaAvailabilityGroupReplica -AvailabilityGroupName $group.AvailabilityGroupName -SqlInstance $server
 # Output the availability group and replica information
 Write-Output "Availability Group: $($group.AvailabilityGroupName)"
 Write-Output "Replicas:"
 foreach ($replica in $replicas) {
 Write-Output " - $($replica.ReplicaServerName) ($($replica.ReplicaRole))"
 }
 Write-Output ""
 }
}

The error I am getting is on"Get-DbaAvailabilityGroupReplica : The term 'Get-DbaAvailabilityGroupReplica' is not recognized...". I have tried multiple times to reinstall the dbatools and sqlserver module but it does not resolve the issue.

asked Apr 12, 2023 at 15:34
6
  • Check for any errors or issues around importing the dbatools module, which is what defines that cmdlet. Commented Apr 12, 2023 at 15:46
  • No issues with the install. I even uninstalled and the reran the install with -force. Commented Apr 12, 2023 at 17:10
  • If you run get-command -module dbatools does it show the function? Commented Apr 12, 2023 at 17:49
  • No it does not. I am on latest version (1.1.146). Commented Apr 12, 2023 at 17:55
  • I don't use dbatools, but I don't see that function, I do see Get-DbaAgReplica Commented Apr 12, 2023 at 18:13

1 Answer 1

1

Here is the corrected code. By replacing the cmdlet Get-DbaAvailabilityGroupReplica with Get-DbaAgReplica I was able get the code working. My issue seemed to come from the use of Get-DbaAvailabilityGroupReplica.

 # Import the dbatools and sqlserver modules
 Import-Module dbatools
 Import-Module sqlserver
 
 # Specify the path to the file containing the list of servers
 $serverList = "C:\Path\To\ServerList.txt"
 
 # Read the list of servers from the file
 $servers = Get-Content $serverList
 
 # Loop through each server and retrieve availability group information
 foreach ($server in $servers) {
 # Get the availability groups on the server
 $groups = Get-DbaAvailabilityGroup -SqlInstance $server
 
 # Loop through each availability group and retrieve replica information
 foreach ($group in $groups) {
 # Get the replicas in the availability group
 $replicas = Get-DbaAgReplica -AvailabilityGroup $group.AvailabilityGroupName -SqlInstance $server
 
 # Output the availability group and replica information
 Write-Output "Availability Group: $($group.AvailabilityGroupName)"
 Write-Output "Replicas:"
 foreach ($replica in $replicas) {
 Write-Output " - $($replica.ReplicaServerName) ($($replica.ReplicaRole))"
 }
 Write-Output ""
 }
 }
answered Apr 12, 2023 at 18:31
1
  • 1
    To make this answer more useful to future readers, you may explain what was the root cause of the error and highlight what exactly fixes it. Commented Apr 12, 2023 at 19:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.