Now, I’m currently using Powershell to write to a database regularly, and like most, I can’t be storing my passwords in scripts! Now, I use DPAPI to encrypt my password which I use to connect to MSOnline in my scripts, and I’m trying to apply this to an SQL Connection string, however this is not working, I get this error:
Exception calling "Open" with "0" argument(s): "Login failed for user '[USERNAME]'."
Thanks for the quick responses both of you - actually using the SQL connection from your CBT series Don!
However this hasn’t helped, which is strange as I know capturing the data like that has worked in the command - I tried this before. this is an attempt to connect to an Azure SQL, or I would have been simply using windows authentication.
Is it worth trying to use System.Data.SQLClient.SQLCredential instead of System.Management.Automation.PSCredential? If so, I will have to find out how to apply this data type…
I am also fairly new to Azure SQL, but as far as I know, [Username]@[random server name].database.windows.net is the credentials I intend to use in my connection string.
I have just tested your code on my machine without the SQL connection bit. The password property of $SQLCred does not contain the plain text password but the secure string of the password. You need to use the method GetNetworkCredential of the PSCredential object instance to access the plain text password.