locked
Receiving a parameter as input for function RRS feed

  • Question

  • Guys,

    I think this is a very simple question for a Powershell expert, but as I am not there yet, I might need your help.

    I have this cmdlet Get-DbaAgReplica from dbatools where I can get the replicas of an Availability Groups. So I created this command.

    $Secondary = Get-DbaAgReplica -SqlInstance srv-sqlserveragl01 | where {$_.Role -eq "Secondary" } | select Name 

    When I execute it and just read the output I have.

    Name          
    ----          
    SRV-SQLSERVER01

    So far so good. Now I have created a small custom function to detect the server and execute the failover accordingly. The beginning of the function is this.

    function Failover-AvailabilityGroups{
    param
    (
    [string]$Environment,

    [string]$SecondaryServer
    )

    When I call the function, the same is not executing and testing the function, this is what I receive as a parameter.

    @{Name=SRV-SQLSERVER01}

    Instead, I should receive only SRV-SQLSERVER01. 

    So, does anyone know how I can resolve it?

    Thanks in advance!

    Marcos Freccia

     


    Att,<br/> Marcos Freccia [MVP em SQL Server]<br/> <a href="http://marcosfreccia.wordpress.com">Blog</a>|<a href="http://twitter.com/sqlfreccia">Twitter</a> <br/> Assine também os feeds clicando <a href="http://marcosfreccia.wordpress.com/feed/"> aqui</a>


    Wednesday, October 18, 2017 6:03 AM

Answers

  • $Secondary = Get-DbaAgReplica -SqlInstance zdc-sqldevagl01 | where {$_.Role -eq "Secondary" } | select -Expand Name 

    \_(ツ)_/

    Wednesday, October 18, 2017 6:39 AM

All replies

  • $Secondary = Get-DbaAgReplica -SqlInstance zdc-sqldevagl01 | where {$_.Role -eq "Secondary" } | select -Expand Name 

    \_(ツ)_/

    Wednesday, October 18, 2017 6:39 AM
  • Thanks a lot! What is the explanation behind the -Expand switch? If you don't mind to answer for sure..



    Att,<br/> Marcos Freccia [MVP em SQL Server]<br/> <a href="http://marcosfreccia.wordpress.com">Blog</a>|<a href="http://twitter.com/sqlfreccia">Twitter</a> <br/> Assine também os feeds clicando <a href="http://marcosfreccia.wordpress.com/feed/"> aqui</a>

    Wednesday, October 18, 2017 6:51 AM
  • help select-object -par expandproperty

    It is a mind expanding bit of magic from PowerShell.


    \_(ツ)_/

    Wednesday, October 18, 2017 6:56 AM