2

I have a function which returns some instances of a database:

function Get-ServiceStatus ([string[]]$server)
{
    foreach ($s in $computer)
    {
        if (Test-Connection $s -Count 2 -Quiet)
        {
            Get-WmiObject win32_Service -Computer $s |
            where {$_.DisplayName -match "SQL Server"} | 
            select Name
        }
    }
}

"Instans/er: "
get-servicestatus

It filters out the name of each instance, and prints it as such:

Instans/er:

Name          
----          
instance1
instance2     
instance3

How do I format the text to print out as:

Instans/er: instance1, instance2, instance3
1
  • I'd suggest having a look at format-list Commented Jan 22, 2019 at 10:19

3 Answers 3

2

Without changing your current function, you could do:

$InstanceList = (Get-ServiceStatus).Name -Join ', '
"Instans/er: $InstanceList"

Or you could modify the function to output this way by default (bearing in mind this results in your function no longer returning a collection of names, but instead a single string that might be less useful):

function Get-ServiceStatus ([string[]]$server)
{
    $InstanceList = foreach ($s in $computer)
    {
        if (Test-Connection $s -Count 2 -Quiet)
        {
            Get-WmiObject win32_Service -Computer $s |
            where {$_.DisplayName -match "SQL Server"} | 
            select -ExpandProperty Name
        }
    }
    $InstanceList -Join ', '

}

"Instans/er: $(Get-ServiceStatus)"

Both solutions just use the -Join operator to join the collection of Name strings with a comma and space.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

function Get-ServiceStatus ([string[]]$server)
{
    $instances = foreach ($s in $computer)
                {
                    if (Test-Connection $s -Count 2 -Quiet)
                    {
                        Get-WmiObject win32_Service -Computer $s |
                        where {$_.DisplayName -match "SQL Server"} | 
                        select -ExpandProperty Name
                    }
                }
    foreach ($instance in $instances)
    {
        $formattedinstances = $instance + ", "
    }
    $formattedinstances = $formattedinstances.Substring(0,$formattedinstances.Length-2)
    return $formattedinstances
}
"Instans/er: $(Get-ServiceStatus)"

Comments

1

In addition to what has been said already, I would strongly suggest to improve your function to be an advanced function as follows:

function Get-SqlServerInstance {
    param (
        [Parameter(
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string]$Computer = "localhost" # (default)
    )
    process {
        Get-WmiObject win32_Service -Computer $Computer | where {
            $_.DisplayName -match "SQL Server"
        }
    }
}

Then you can do this:

$instances = $servers | where {
    Test-Connection $_ -Count 1 -Quiet
} | Get-SqlServerInstance  | select -Unique -ExpandProperty Name
"Instans/er: $($instances -join ', ')"

If that is too long for you, wrap that in a small helper function:

function Print_ServiceStatus ([string[]]$servers) {
    $instances = $servers | where {
        Test-Connection $_ -Count 1 -Quiet
    } | Get-SqlServerInstance  | select -Unique -ExpandProperty Name
    Write-Host "Instans/er: $($instances -join ', ')"
}

But, I am very sure it would make sense to output the Computer name too:

$servers | Get-SqlServerInstance | select PSComputerName, Name

IMHO, this would be the best practice and most "PowerShell-ish" way to do it.

Benefits:

  • Get-SqlServerInstance is a more appropriate name describing what the function does
  • The function now returns objects, not strings (the way to go in PowerShell!)
  • It works with the pipeline now (always preferred for performance)
  • The Test-Connection is not really part of the function's purpose. Your function should throw if it cannot connect! So I moved it to outside the function.

Comments

Your Answer

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.