Execution Flow
START
FailoverClusters +
dbatools available?
throw; return
Step 1, Discover cluster
Get-Cluster → ClusterName | Get-ClusterNode → Nodes[]
Listener: Get-ClusterGroup → NetworkName + IPAddress resource → discListenerName / IP / Port
Step 2, Discover SQL instances (WMI per node)
Get-WmiObject Win32_Service -Filter "MSSQLSERVER or MSSQL$*" → instName + StartName (service account)
Step 3, Test connectivity
Connect-DbaInstance each node (Windows Auth or -SqlCredential)
Any node fails → throw "fix SPNs or pass -SqlCredential"
-BackupCluster
Settings=$true?
Write AlwaysOn_ClusterSettings_*.txt
ShouldProcess
create AG?
AgResult=WhatIf; -WhatIf passed to New-sqm...
New-sqmAvailabilityGroup (primary, secondaries[], listener, endpoints, seeding, databases)
Step 6, Post-creation
Sync-sqmLoginsToAlwaysOn (unless -SkipLoginSync) | Invoke-sqmSqlAlwaysOnAutoseeding
-GenerateSpn
Report=$true?
Write AlwaysOn_SPN_ADTeam_*.txt (setspn commands)
Return PSCustomObject { ClusterName, Nodes, PrimaryReplica, AvailabilityGroup, Status, AgResult, LoginSyncResult, SpnReportFile, … }
DONE
Synopsis
Acts as a headless replacement for the interactive AlwaysOnSetup GUI. Discovers the WSFC cluster and all SQL instances via Get-Cluster / Get-ClusterNode and WMI Win32_Service. The listener is auto-detected from the cluster network-name and IP-address resources; supply the -Listener* parameters to override. Connectivity is tested with Connect-DbaInstance, if any node fails without -SqlCredential, the setup aborts with an SPN repair hint. After the AG is created via New-sqmAvailabilityGroup, logins are synced to all secondaries and SEEDING_MODE=AUTOMATIC is ensured.
Requires both FailoverClusters and dbatools modules. Must run on a cluster member as a local administrator with sysadmin on all replicas.
When Kerberos SPNs are missing, pass -SqlCredential (SQL auth) or fix the SPNs using the generated SPN report file before running. The function does not create temporary SQL logins like the GUI tool did.
Syntax
Invoke-sqmAlwaysOnSetup
[-AvailabilityGroupName <String>]
[-Database <String[]>]
[-PrimaryReplica <String>]
[-ListenerName <String>] [-ListenerIPAddress <String[]>]
[-ListenerPort <Int>] [-ListenerSubnetMask <String>]
[-EndpointPort <Int>]
[-FailoverMode <Automatic|Manual>]
[-BackupPreference <Primary|Secondary|PreferSecondary|None>]
[-ServiceAccount <String>]
[-SqlCredential <PSCredential>]
[-BackupClusterSettings <Bool>]
[-GenerateSpnReport <Bool>]
[-OutputPath <String>]
[-SkipLoginSync ]
[-EnableException ]
[-WhatIf ] [-Confirm ]
Parameters
Parameter Type Default Description
-AvailabilityGroupName String Listener name Name of the AG to create. Defaults to the discovered cluster listener name.
-Database String[] , Databases to seed into the AG (auto-seeded).
-PrimaryReplica String First node Override the primary replica. Defaults to the first cluster node.
-ListenerName String Auto-discovered Override the listener name discovered from the cluster.
-ListenerIPAddress String[] Auto-discovered IP address(es) for the listener.
-ListenerPort Int 1433 Listener probe port (default if cluster provides none).
-EndpointPort Int 5022 Database mirroring endpoint port.
-FailoverMode String Automatic Automatic (sync) or Manual (async).
-BackupPreference String Primary Backup preference for the AG.
-SqlCredential PSCredential , SQL authentication. Omit to use Windows/Kerberos auth.
-BackupClusterSettings Bool $true Write a cluster-settings text file before making changes.
-GenerateSpnReport Bool $true Write an SPN request file (setspn commands) for the AD team.
-OutputPath String Get-sqmDefaultOutputPath Directory for backup and SPN report files.
-SkipLoginSync Switch $false Skip post-creation Sync-sqmLoginsToAlwaysOn.
-EnableException switch $false Throw on error instead of logging and returning a failed result. -EventLog string , See Invoke-sqmAlwaysOnSetup -? for details. -ListenerSubnetMask string '255.255.255.0' See Invoke-sqmAlwaysOnSetup -? for details. -ServiceAccount string , SQL service account for the endpoint CONNECT grant. Default: discovered from the SQL service.
Return Value
Property Description
ClusterName Discovered WSFC cluster name.
Nodes Array of cluster node hostnames.
PrimaryReplica SQL instance used as the primary replica.
SecondaryReplicas Remaining SQL instances.
AvailabilityGroup AG name.
Listener Listener name (if configured).
Status Success, WhatIf, or Failed.
AgResult Full result object from New-sqmAvailabilityGroup.
LoginSyncResult Result from Sync-sqmLoginsToAlwaysOn.
ClusterSettingsFile Path to the cluster-settings backup file.
SpnReportFile Path to the SPN request file for the AD team.
Examples
Example 1, Full auto-discovery setup, creates AG, syncs logins, writes reports
Invoke-sqmAlwaysOnSetup -AvailabilityGroupName "ProdAG" -Database "AppDb" copy
Example 2, Dry-run (shows what would happen without making changes)
Invoke-sqmAlwaysOnSetup -AvailabilityGroupName "ProdAG" -Database "AppDb" -WhatIf copy
Example 3, SQL auth when Kerberos SPNs are missing
Invoke-sqmAlwaysOnSetup -AvailabilityGroupName "ProdAG" -Database "AppDb" `
-SqlCredential (Get-Credential sa)copy
Example 4, Manual failover mode, no login sync
Invoke-sqmAlwaysOnSetup -AvailabilityGroupName "DrAG" `
-FailoverMode "Manual" -SkipLoginSync copy