Trying to secure/encrypt password in PowerShell.
Right now I have a script that goes through several servers and invoke SQL command.
I used to have the password and username hard coded and I would like to encrypt them due to security purposes.
$query = @'
SELECT TOP 10 FROM [dbo].[TABLE]
'@
$SERVERS = LIST OF SERVERS
(Get-Credential).Password | ConvertFrom-SecureString | Set-Content "C:\ Password.txt"
$passWord1 = Get-Content "C:\ Password.txt" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential ('username',$passWord1)
$SQLCredentials = @{
server = $server
database = 'databasename'
Query= $query
Credential = $credential
}
$Results = Invoke-SqlCmd @SQLCredentials
$Results | Select-Object -Property * -Skip 1
Also I tried the following but does not seem to work Invoke SQL Command with Secure Creds
error: Invoke-Sqlcmd : A parameter cannot be found that matches parameter name 'Credential'.
Goal is to run Invoke-SqlCmd
using secured username and password.