Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Compare-Object fails due to way object looks from function write-output

$
0
0

Hi all, 

Ive made a function to test if a box has a SQL instance on it, ive finally it outting now to the pipeline and such using Write-Output, and it seems all good,

I wanted to do a quick compare from the orginal list of Servers, to the results of my function (So i list out which servers have failed. But im running into a problem where my function outputs look like 

@{ComputerName=myComputerName}

Where as my text file output is is the more desirable

mycomputerName2

Im running :

$importservers = get-content 'C:\Temp\CMS.txt'

$SuccessfulConnection = Test-SQLConnection -Servers $importservers 

Compare-Object $importservers $SuccessfulConnection 

 

obviously i get 0 matches as it sees $SucessfulConnection results differently to the $importServers. 

any ideas what ive done wrong? 

the function is@

FUNCTION Test-SQLConnection {

    [CmdletBinding()]

   Param (

   [Parameter(Mandatory=$true)]

   [string[]] $Servers

   )

BEGIN {}

PROCESS {

    foreach ($s in $servers){

        ## confirm Connection is possible.

        $connectionString = "Data Source=$s;Integrated Security=true;Initial Catalog=master;Connect Timeout=3;"

        #write-host $connectionString

            TRY {

                    $sqlConn = new-object ("Data.SqlClient.SqlConnection") $connectionString

                    $sqlConn.Open()

                    if ($sqlConn.State -eq 'Open')

                    {

                        $sqlConn.Close();

                        $obj = New-Object –typename PSObject

                        $obj | Add-Member –membertype NoteProperty –name ComputerName –value ($s)

                        Write-Output $obj

                        Write-Verbose "Available Server: $s"

                    }

                }

            CATCH   {Write-verbose "Not available Server: $s"}

            FINALLY {}

        }

}  

}


 


Viewing all articles
Browse latest Browse all 6937

Trending Articles