Cannot connect to SQL Server from PowerShell with domain credentials

前端 未结 5 921
旧巷少年郎
旧巷少年郎 2021-01-19 15:20

I have a problem where I cannot connect to a SQL Server using domain credentials. Using the SA credentials it works and the query runs as expected. But when I use domain cre

相关标签:
5条回答
  • 2021-01-19 15:30

    You cannot connect with domain credentials THAT WAY.

    Domain credentials are present in the process making the connection. The account using in the process (or if using the NETWORK ONLY option in RUNAS) will be used to make the connection to SQL Server using integrated security.

    Of course, because your process CANNOT actually run as a user on that domain because your computer is not in a trust relationship with that domain, I recommend you look at using RUNAS /NETONLY.

    This will prompt for a password, and so you may have to write your own replacement for it which uses the CreateProcessWithLogonW API instead.

    https://stackoverflow.com/a/758805/18255

    This will allow your process to use domain credentials from the other domain which are only used on network connections and verified at that time, while still using the local account for other things. This MAY cause problems getting to other network resources which might rely on your local (or other domain?) account, I haven't fully tested how RUNAS /NETONLY works if you make DIFFERENT network connections from the same process.

    0 讨论(0)
  • 2021-01-19 15:36

    You cannot pass username and password as string.

    Try something like this -

    #$cred = Get-Credential
    # this will prompt for username and password, fill them in
    # use this in your connection string
    $secpwd = ConvertTo-SecureString $password -AsPlainText -Force
    
    $SqlConnection.ConnectionString = 'Server=$SQLServer; Database=$SQLDBName; User ID=$username; Password=$secpwd;'
    
    0 讨论(0)
  • 2021-01-19 15:37
    Function SQL_CONN1 ($Query) {
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
    $SqlConnection.ConnectionString = "Server=myPC;Database=myDB;Integrated Security=True" 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
    $SqlCmd.Connection = $SqlConnection 
    $SqlCmd.CommandText = $Query 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
    $SqlAdapter.SelectCommand = $SqlCmd 
    $DataSet = New-Object System.Data.DataSet 
    $a=$SqlAdapter.Fill($DataSet) 
    $SqlConnection.Close() 
    $DataSet.Tables[0]  }
    
    SQL_CONN1 "Select * from [MyTable]"
    

    Try $SqlConnection.ConnectionString = "Server=myPC;Database=myDB;Integrated Security=True"

    0 讨论(0)
  • 2021-01-19 15:45

    If you are not worried about security you can do it like this (not secure, though):

    #Set variables here
    
    $connectionString="server=$s;database=$sqlDbName;user id=$userName;password=$passWord";
    
    $sqlConnection=New-Object System.Data.SqlClient.SqlConnection($connectionString);
    

    It works in SQL Server 2012. But, again, not secure. Hope that helps someone...

    0 讨论(0)
  • 2021-01-19 15:53

    If you have SQL Credentials use something like this:

    $ConnectionString = server=*serverName*;database=*databaseName*;user id=*userName*;password=*passWord*;
    

    If you have Domain Credentials use something like this:

    $ConnectionString = "server=*serverName*;database=*databaseName*;user id=*domain\username*;password=*passWord*;trusted_connection=true;"
    

    This works in my environment. Of course after that you do:

    $Connection = New-Object System.Data.SQLClient.SQLConnection($ConnectionString)
    
    0 讨论(0)
提交回复
热议问题