SQL Server AOAG Add 3 IP's to LIstener with Powershell Script Not working

  • Team,

    Why does the below not work? Using parameters for listener name and ip addresses. When i hard code the IP addresses in there it works even with the listener as a parameter . I assume it has to do with the quotes. Any assistance is greatly appreciated. See full section of code not including the parameter request to enter.

    I have tried

    1. direct parameter $IP1, etc
    2. double quotes "$IP1"
    3. quotes with plus sign '+$IP+', "+$IP1+"

    $AddListener = @"

    USE [master]

    GO

    ALTER AVAILABILITY GROUP [$AGName]

    ADD LISTENER N'$LsnrName' (

    WITH IP

    ((N'$IP1', N'255.255.252.0'),

    (N'$IP2', N'255.255.252.0'),

    (N'$IP3', N'255.255.252.0')

    )

    , PORT=$SrvPort);

    "@

    invoke-sqlcmd -query $AddListener -Database "master" -ServerInstance $SQLInstance -ErrorAction SilentlyContinue

    ¤ §unshine ¤

  • Not sure why it isn't working, but you don't need to splat it, that is a single string variable for -query. Just use [string]$addListner = "..." to start with.

    Does the SQL command work running it as SQL in dev?

  • RESOLUTION: Yes regular sql or  even as powershell hardcoded without parameters it works. However, just found resolution. Use pure powershell. While i couldnt find an exact example, i just tried it myself and it worked!

    New-SqlAvailabilityGroupListener -Name $LsnrName -StaticIp "$IP1/255.255.252.0","$IP2/255.255.252.0","$IP1/255.255.252.0" -Path "SQLSERVER:\SQL\PrimaryServer\MSSQLSERVER\AvailabilityGroups\AGName" -Port $Port

    ¤ §unshine ¤

  • This was removed by the editor as SPAM

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply