'Convert Secret Variables to PSCredential

In my Release, I want to run a Powershell DSC script needing a PSCredential object. Are there any "clean" ways to convert a Secret Variable to a PSCredential object?

Currently I use:

$secpasswd = ConvertTo-SecureString $secretPasswordParam -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)

and need to set PSDscAllowPlainTextPassword to true.

Update:

When running Invoke-ScriptAnalyzer I get:

PSAvoidUsingPlainTextForPassword    Warning      deploy.ps1 4     Parameter '$secretPasswordParam' should use
                                                                  SecureString, otherwise this will expose sensitive
                                                                  information. See ConvertTo-SecureString for more
                                                                  information.
PSAvoidUsingConvertToSecureStringWi Error        deploy.ps1 21    File 'deploy.ps1' uses ConvertTo-SecureString with
thPlainText                                                       plaintext. This will expose secure information.
                                                                  Encrypted standard strings should be used instead.

Because this messages and a not good feeling setting a explicit option named PSDscAllowPlainTextPassword, I search for a cleaner solution.



Solution 1:[1]

I use ConvertTo-SecureString which receives a string variable type. The parameter type is -AsPlainText -Force. If you secret variable is not a string type than the cmdlet will fail

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Golden Lion