PowerShell/docs/save-credentials.md

77 lines
2.0 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *save-credentials.ps1* Script
===========================
2024-01-25 13:31:10 +01:00
2024-11-08 12:35:11 +01:00
This PowerShell script asks for credentials and saves them encrypted into a target file.
2024-01-25 13:31:10 +01:00
Parameters
----------
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/save-credentials.ps1 [[-targetFile] <String>] [<CommonParameters>]
2024-01-25 13:31:10 +01:00
2024-11-08 12:35:11 +01:00
-targetFile <String>
Specifies the target file ("~\my.credentials" by default)
2024-01-25 13:31:10 +01:00
Required? false
Position? 1
2024-11-08 12:35:11 +01:00
Default value ~\my.credentials
2024-01-25 13:31:10 +01:00
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./save-credentials.ps1
Enter username and password, please.
2024-11-08 12:35:11 +01:00
✅ Your credentials have been saved to C:\Users\Markus\my.credentials (encrypted).
2024-01-25 13:31:10 +01:00
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Saves credentials encrypted
.DESCRIPTION
2024-11-08 12:35:11 +01:00
This PowerShell script asks for credentials and saves them encrypted into a target file.
.PARAMETER targetFile
Specifies the target file ("~\my.credentials" by default)
2024-01-25 13:31:10 +01:00
.EXAMPLE
PS> ./save-credentials.ps1
Enter username and password, please.
2024-11-08 12:35:11 +01:00
✅ Your credentials have been saved to C:\Users\Markus\my.credentials (encrypted).
2024-01-25 13:31:10 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-11-08 12:35:11 +01:00
param([string]$targetFile = "~\my.credentials")
2024-01-25 13:31:10 +01:00
try {
Write-Host "Enter username and password, please." -foreground red
$cred = Get-Credential
2024-11-08 12:35:11 +01:00
$cred.Password | ConvertFrom-SecureString | Set-Content "$targetFile"
"✅ Your credentials have been saved to $targetFile (encrypted)."
2024-01-25 13:31:10 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:52:00)*