-1

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.

usertestREACT
  • 263
  • 2
  • 7
  • 15

1 Answers1

1

Some versions of invoke-sqlcmd don't have the -credential parameter: PowerShell: invoke-sqlcmd with Get-Credential doesn't work (Actually, I don't either.)

js2010
  • 23,033
  • 6
  • 64
  • 66