Forum Discussion
fweymouth
Jun 15, 2022Copper Contributor
Passing credentials to SQL connection string
Hello. I am new to PowerShell and would really appreciate some help! I have a working script (Example A) which allows me to connect to SQL servers in my current domain using windows authenticatio...
LainRobertson
Jun 16, 2022Silver Contributor
You can't specify alternate Windows credentials in a connection string. When you leverage the integrated authentication option, the credentials of the host process are used.
Which brings me to some simple alternatives to worrying about the connection string:
- Launch a new PowerShell shell using "run as a different user" and use the appropriate credentials from the remote forest (assuming the forest trusts facilitate doing this);
- Use Invoke-Command, New-PSSession, Start-Process, etc. with the -Credential parameter to run the script under the context of the account from the remote forest;
- Use an SQL account, not a Windows account, in the connection string.
I'd probably go with the first option if I was going to do a few things against the server. If it was just running the one command, I'd probably go with Invoke-Command, but to each their own.
I mention point 3 for completeness, but really, it's a rubbish solution. There really shouldn't be common SQL-defined logins across multiple SQL hosts.
Cheers,
Lain
- fweymouthJun 16, 2022Copper ContributorThank you Lain for the detailed response. I will see what I can accomplish with the Invoke-Command.