-1

I want to make connection to SQL server configured on On prem using Powershell. But I don't want to pass any credential in connection string as shown below

#making a db connection string
$connString = "Data Source=$SqlServer;Database=$Database;User ID=$SqlAuthLogin;Password=$SqlAuthPw"
        
#Create a SQL connection object
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
        
#Attempt to open the connection
$conn.Open()

Is there any other way from which i can pass credential.

Any help will be thankfull.

11
  • 1
    you can use windows authentication Commented Oct 10, 2022 at 8:02
  • 1
    Which dbms are you using? (This isn't really related to the SQL language.) Commented Oct 10, 2022 at 8:06
  • @Roshan, thanks for reply, for windows authentication also we have to pass credential as ConnectionString 'Server=$server;Database=$databaseName;UID=$DOMAIN\USER;PWD=$password;Integrated Security=true;' as given in following link stackoverflow.com/questions/38177582/… Commented Oct 10, 2022 at 8:49
  • @jarlh, thanks for reply, i'm using SQL hosted on onprem Commented Oct 10, 2022 at 8:52
  • @Shubhkumar refer this sqlshack.com/… Commented Oct 10, 2022 at 8:54

1 Answer 1

0

You can use windows Authentication with Integrated Security.

# more secure windows authentication with current account

Invoke-Sqlcmd  -ConnectionString "Data Source=$SqlServer;Initial Catalog=$Database; Integrated Security=True;" -Query "$Query" | Format-Table

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply but I can't use windows authentication because most of users don't have access to database. So have to go with SQL username and Password , but not to pass password in connection string . Is it possible by any way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.