Powershell / Credentials / Connect to SQL Server with PowerShell

How to login using SQL Server Management Object (SMO)

 

For windows authentication

 

SQL Authentication with hard coded password (note recommended)

 

Impersonate login as user using SQL authentication with secure credentials

 

So now we can repeatedly use those credentials

 

 

Similar Posts:

2 Responses to “ “Powershell / Credentials / Connect to SQL Server with PowerShell”

  1. Ramu says:

    Hi, Thanks For your Script. How to select database, Drop, delete and update table etc.,

    Please share those details

    • Barac says:

      Hi Ramu,
      You are welcome …
      You can use invoke-sqlcmd command to run SQL statement against your instance …
      $ConnStrDB = @{
      ‘Database’ = “yourdbname”
      ‘ServerInstance’ = “yourinstancename.database.windows.net”
      ‘Username’ = “yourusername”
      ‘Password’ = “yourpassword”
      ‘Query’ = “CREATE TABLE [dbo].[TestTAB](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [Column01] [nvarchar](125) NULL
      ) ON [PRIMARY]
      GO”
      }
      Invoke-Sqlcmd @ConnStrDB

      You can as well user parameters and pass those values
      param (
      [string]$servername = “yourinstancename.database.windows.net”,
      [string]$username = “yourusername”,
      [string]$password = “yourpassword”,
      [string]$dbname = “yourdbname”
      )

      Cheers

Leave a Reply to Ramu Cancel reply

Your email address will not be published. Required fields are marked *