Check Existence of Server and find Instance Type

,

Compile the function

Execute the function checkserver

Enter sql server name

clear-host
$instance =""

function checkserver
{

$instance= Read-Host "Enter the SQL Service Instance Name [Dont use "" "" in the servername]:"
write-host "`n You entered server name as:["$instance.trim() "]" -ForegroundColor Yellow

$begin = (get-date).Millisecond
$serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)

$instance1 = $instance.toupper()
$instance = $instance1.trim();

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


IF ( $serverName1 -NE "\")
{

write-host " `n Server: *****" "'$serverName1'" "**** exist `n" -ForegroundColor Green


IF ($serverName10 -notcontains "\*"){

 write-host "'$serverName10'" is a Named Instance -ForegroundColor Magenta
 
 } 
 ELSE {
 write-host "'$serverName10'" is a Default Instance  -ForegroundColor DarkGray
 
 }

$end = (get-date).Millisecond
$timeTaken = $end - $begin
write-host "Time taken to search the server: ***" $timeTaken "*** Milliseconds" -ForegroundColor DarkGray
}

ELSEIF ( $serverName1 -EQ "\")
{

write-host "`n Wrong Server name entered:[" $instance "]" -ForegroundColor Red
$end = (get-date).Millisecond
$timeTaken = $end - $begin
write-host "Time taken to search the server: ***" $timeTaken "*** Milliseconds" -ForegroundColor DarkGray

}
}

## EXECUTE ##
checkserver

Rate

5 (1)

Share

Share

Rate

5 (1)