简体   繁体   中英

How to secure mysql connection with powershell

I have a powershell function which connects to a mysql database. In this function there is a connection string which looks like this:

$ConnStr = "Server=server;Uid=user;Pwd=password;Database=DB";

Now whats the best way to store these information secure and not just have the string in plain text in the script?

You can create a encrypted password that is specific to the PC the file is saved on using...

$folder = [environment]::getfolderpath("mydocuments")     
$passFile = "$folder\MyPass.txt"
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content $passFile

Then when you need to retrieve the password and use it.

$passwdfolder = [environment]::getfolderpath("mydocuments")     
$passwdFile = "$passwdfolder\MyPass.txt.txt"
$securePasssword = Get-Content $passwdFile | ConvertTo-SecureString 
$credentials = New-Object System.Net.NetworkCredential($null, $securePasssword,$null)
$MyPass = $credentials.Password

It's not perfect. Anyone with access to the computer can get the password with a bit of know-how, but it's better than storing your password in plain text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM