Cannot connect to SQL Server from PowerShell with domain credentials

前端 未结 5 920
旧巷少年郎
旧巷少年郎 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: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;'
    

提交回复
热议问题