Script to find if a SQL Server Instance Exist

  • Kindly provide a good script to check if a sql server instance does exist. If not need to throw a good alert. I'm trying using the below script, but taking long time to execute. Any better way to write it, plz suggest

    $instance = "domain\INST4"

    $serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)

    $instance = $instance.toupper()

    $serverName = $serverObject.ComputerNamePhysicalNetBIOS

    $serverName = $serverName +"\"+$serverObject.InstanceName

    $serverName

    Thanks.

  • My revised script.

    clear-host

    function checkserver

    {

    [CmdletBinding()]

    Param

    (

    [Parameter(Position=0,Mandatory=$true)][string]$instance

    )

    Begin

    {

    $VerbosePreference = 'Continue'

    $DebugPreference = 'stop'

    }

    Process

    {

    $Global:instance= Read-Host "Enter the SQL Service account(SQLSVCACCOUNT) to be used"

    $Global:instance.ToUpper();

    $instance

    $serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)

    $instance = $instance.toupper()

    $serverName = $serverObject.ComputerNamePhysicalNetBIOS

    $serverName = $serverName +"\"+$serverObject.InstanceName

    #$serverName

    IF ( $serverName -ne "\")

    {

    write-host "Server:" $serverName "exist" -ForegroundColor Green

    }

    ELSEIF ( $serverName -eq "\")

    {

    write-host "Wrong Server name" -ForegroundColor Red

    }

    }

    }

    Thanks.

Viewing 2 posts - 1 through 1 (of 1 total)

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