PSCredential packages a username with a SecureString password so PowerShell commands can receive authentication data without passing ordinary password text. It fits automations that cross multiple environments and need credentials, API keys, or other secrets during execution. section 4.2 section 4.2.2
Create and use the object according to the authentication boundary:
- Prompt interactively with Get-Credential when a user can supply the identity and secret at run time. section 4.2.2
- Construct it manually from a username and SecureString when another system supplies the values; recognize that unsecured input copies remain in memory. section 4.2.2
- Pass it to commands that support PSCredential, such as a SQL diagnostic query, rather than converting it unnecessarily. section 4.4.2
- Convert it to a .NET NetworkCredential for external systems requiring that type, while recognizing that the converted password is plain text until the session closes. section 4.2.2
Rebuilding a Jenkins credential
Jenkins exposes bound values as environment-variable strings, so convert the password back to SecureString and create a PSCredential before passing it as SqlCredential. Bind sqlusername and sqlpassword to a stored username-and-password credential, then use sqlserver for the target instance. section 4.4.2
$Credential = Get-Credential
Use the compact interactive pattern when prompting for credentials is appropriate. section 4.2.2
$Username = 'Contoso\BGates' $Password = ConvertTo-SecureString 'Password' -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential $Username, $Password $NetCred = $Credential.GetNetworkCredential() $NetCred UserName Domain -------- ------ BGates Contoso
Compare manual PSCredential construction with conversion to NetworkCredential for systems that require a .NET credential object. section 4.2.2
