Connecting to a database

Hello everybody,
I play around with PowerShell for a few years, and today I receive a sum-up documentation from TradePub, that presents the CmdLet Get-SqlDatabase

I did not even imagine it was possible to connect to a database with PowerShell, so I try and connect to this :
Product : Microsoft SQL Server 2014
Instance : SQLEXPRESS
Instance ID : MSSQL12.SQLEXPRESS
Feature : Services Moteur de base de données
Language : 1033
Edition : Express Edition
Version : 12.0.2269.0
Clustered : Non
Configured : Oui

and it gives that :

PS SQLSERVER:\> Get-SqlDatabase Image -ServerInstance UC00009\SQLEXPRESS
Get-SqlDatabase : Image
Au caractère Ligne:1 : 1
+ Get-SqlDatabase Image -ServerInstance UC00009\SQLEXPRESS
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ([UC00009\SQLEXPRESS]:Server) [Get-SqlDatabase], Sq
   lPowerShellObjectNotFoundException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.IaaS.GetSqlDatab
   aseCommand

Hum, is anybody more at ease with this cmdlet?

I don’t use that cmdlet but the error message is pretty clear: the ServerInstance name you fed it cannot be found. Based on your prompt (PS SQLSERVER) it seems that you are already on the SQL server.
Have you tried running Get-SqlDatabase by itself with no arguments?

What is Image supposed to be?

It is a database, with one table, dbo.Lists
It is queried with

SELECT TOP (1000) [ListName]
      ,[LastWriteDate]
      ,[NbLinesFic]
  FROM [Images].[dbo].[Lists]

The top node in SSMS displays

UC00009\SQLEXPRESS (SQL Server 12.0.2269 - UC00009\User)

As you guess, ListName is the name of the first field.

Get-SqlDatabase, with no argument, answers that the path ­«SQLSERVER:\» cannot be found.
So I supposed I had to provide another path …

In the connection strings, the server provided is «.\SQLEXPRESS»
Providing that in the Path or Server argument gives no better results.

Down the page I see a thread about PowerShell 2.0 and SqlServer 2005.
Copying the code from there I obtain $conn.State = Open, which is a good beginning.
It does not use Get-SqlDatabase.

For the connection string I have to pay attention to give TrustServerCertificate in one word, whereas in SSMS it is written in three words.

I realize that with Get-SqlDatabase I gave no connection string.

$conn = New-Object system.Data.SqlClient.SqlConnection
$conn.connectionstring = $connectionString
$conn.open()

$conn.State

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.