I had the following code in a PowerShell script:
$sql_instance = "SQLEXPRESS"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
$server = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $sql_instance
Write-Host $server.InstanceName
And nothing was returned. After about 20 minutes of researching I realized I forgot to include the computer name when creating the $server object.
The code should have been:
$sql_instance = "MYSERVER\SQLEXPRESS"
My question is: how can I verify that the server object created is actually representing a valid SQL Server instance?
I imagine something like: If $server.IsNotValid -eq "True" Then Throw Exception