Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save PSCredential in the file

Tags:

I know I can save password to the file:

Read-Host "Enter Password" -AsSecureString |  ConvertFrom-SecureString | Out-File $passwordfile 

and read it from file:

$secpasswd = (Get-Content $passwordfile | ConvertTo-SecureString) 

and then create PSCredential object:

$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd) 

But can I save $credential in the file, so username and his password were kept together?

like image 946
Alexan Avatar asked Oct 13 '16 19:10

Alexan


People also ask

Where are PowerShell credentials stored?

Starting in PowerShell 3.0, if you enter a user name without a domain, Get-Credential no longer inserts a backslash before the name. Credentials are stored in a PSCredential object and the password is stored as a SecureString.

What is a PSCredential?

The PSCredential is a placeholder for a set of credentials – it basically contains a username and a password. The PSCredential object offers a safe and convenient way to handle a username and password.


1 Answers

Update on non-Windows Platforms

A lot has changed since this answer was first written. Modern versions of PowerShell are based on .net core, and run cross-platform. The underlying type that enables this whole answer is called [securestring] and the security and encryption that backs it comes from the Data Protection API (DPAPI) on Windows, which is not open source and not available cross-platform.

As such, while you can possibly use the same code here on non-Windows platforms, do note that there will be absolutely no encryption backing it.

tl;dr: don't use this on non-Windows platforms!

More information available in this excellent answer on a related question.


To store and retrieve encrypted credentials easily, use PowerShell's built-in XML serialization (Clixml):

$credential = Get-Credential  $credential | Export-CliXml -Path 'C:\My\Path\cred.xml' 

To re-import:

$credential = Import-CliXml -Path 'C:\My\Path\cred.xml' 

The important thing to remember is that by default this uses the Windows data protection API, and the key used to encrypt the password is specific to both the user and the machine that the code is running under.

As a result, the encrypted credential cannot be imported by a different user nor the same user on a different computer.

By encrypting several versions of the same credential with different running users and on different computers, you can have the same secret available to multiple users.

By putting the user and computer name in the file name, you can store all of the encrypted secrets in a way that allows for the same code to use them without hard coding anything:

Encrypter

# run as each user, and on each computer  $credential = Get-Credential  $credential | Export-CliXml -Path "C:\My\Secrets\myCred_${env:USERNAME}_${env:COMPUTERNAME}.xml" 

The code that uses the stored credentials:

$credential = Import-CliXml -Path "C:\My\Secrets\myCred_${env:USERNAME}_${env:COMPUTERNAME}.xml" 

The correct version of the file for the running user will be loaded automatically (or it will fail because the file doesn't exist).

like image 192
briantist Avatar answered Sep 28 '22 06:09

briantist