Connecting via SMO to a named instance
A question came up in tonight’s User group session regarding connecting to SQL server instances using SMO
If you have just a default instance – just give the server name
$server = New-Object -TypeName "Microsoft.SqlServer.Management.Smo.Server" -ArgumentList "W08R2SQl12"
If you have a named instance the give the instance name as well
$serverI = New-Object -TypeName "Microsoft.SqlServer.Management.Smo.Server" -ArgumentList "W08R2SQl12\instance_name"
These work as long as the SQL Server browser service is running.
If it isn’t this is what I think you have to do.
This is untested and a best guess. I will try and test.
$cons = "server=W08R2SQL12\instance_name,port_number;Trusted_Connection=true;multipleactiveresultsets=false"
$cn = New-Object -TypeName "System.Data.SqlClient.SqlConnection" -ArgumentList $cons
$serverZ = New-Object -TypeName "Microsoft.SqlServer.Management.Smo.Server" -ArgumentList $cn
If anyone manages to test this please let me know – I’ll test as soon as I can