Skip to content

Commit 3fab540

Browse files
🌟 [Major]: Introducing the PSCredential module (#3)
## Description This pull request introduces the `PSCredential` PowerShell module, adding functions, documentation, and tests. ### New Functions for Managing Credentials: * [`New-PSCredential`](diffhunk://#diff-aa91734ba63271be8a7ab9602778b91375c26e1fec6845e9ba22175e9efb6652R1-R58): A function to create `PSCredential` objects, supporting both secure string and plain text passwords. * [`Save-PSCredential`](diffhunk://#diff-8a06b7ceda52d583a0bac03ec26c39ea7f4fd01dc2a5b3a3eb548727d9dd651dR1-R52): A function to save `PSCredential` objects to a file, creating necessary directories if they do not exist. * [`Restore-PSCredential`](diffhunk://#diff-1a0a08ae412d3eba29c03fdf97dba14b8a76c158ac99962b0f9d54dd1aad3b6bR1-R51): A function to restore `PSCredential` objects from a file, ensuring the content is a valid `PSCredential` object. ### Documentation: * [`README.md`](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L1-R108): Includes installation instructions, usage examples, and contribution guidelines. * [`examples/General.md`](diffhunk://#diff-a967fe83dceabfadb93250149336d9defaf7854411d0434fef6d46af4e2d6c36R1-R79): Covers detailed examples for the new `New-PSCredential`, `Save-PSCredential`, and `Restore-PSCredential` functions. ### Tests: * [`tests/PSCredential.Tests.ps1`](diffhunk://#diff-05dbda35cc66dcc03e56aabc17eb0d195997c9174a771e5386026c64a63ea4ceR1-R53): Added tests for `New-PSCredential`, `Save-PSCredential`, and `Restore-PSCredential` functions to ensure they work as expected. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [x] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent c46b23e commit 3fab540

File tree

10 files changed

+361
-82
lines changed

10 files changed

+361
-82
lines changed

.github/workflows/Nightly-Run.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ jobs:
1414
Process-PSModule:
1515
uses: PSModule/Process-PSModule/.github/workflows/CI.yml@v3
1616
secrets: inherit
17+
with:
18+
Debug: true
19+
Verbose: true

README.md

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,108 @@
1-
# {{ NAME }}
1+
# PSCredential
22

3-
{{ DESCRIPTION }}
3+
`PSCredential` is a PowerShell module that provides functions for managing credentials.
4+
It allows users to create, save, and restore `PSCredential` objects from disk.
45

56
## Prerequisites
67

7-
This uses the following external resources:
8-
- The [PSModule framework](https://github.com/PSModule) for building, testing and publishing the module.
8+
This module requires:
9+
10+
- The [PSModule framework](https://github.com/PSModule) for building, testing, and publishing the module.
911

1012
## Installation
1113

1214
To install the module from the PowerShell Gallery, you can use the following command:
1315

1416
```powershell
15-
Install-PSResource -Name {{ NAME }}
16-
Import-Module -Name {{ NAME }}
17+
Install-PSResource -Name PSCredential
18+
Import-Module -Name PSCredential
1719
```
1820

1921
## Usage
2022

21-
Here is a list of example that are typical use cases for the module.
23+
Here are some typical use cases for the module.
24+
25+
### Example 1: Creating a new `PSCredential` object
26+
27+
This function generates a `PSCredential` object using user input.
28+
29+
```powershell
30+
New-PSCredential
31+
```
2232

23-
### Example 1: Greet an entity
33+
This prompts the user for a username and password and returns a `PSCredential` object.
2434

25-
Provide examples for typical commands that a user would like to do with the module.
35+
Alternatively, you can specify the credentials explicitly:
2636

2737
```powershell
28-
Greet-Entity -Name 'World'
29-
Hello, World!
38+
New-PSCredential -Username 'admin' -Password (ConvertTo-SecureString 'P@ssw0rd!' -AsPlainText -Force)
3039
```
3140

32-
### Example 2
41+
### Example 2: Saving a `PSCredential` object to a file
3342

34-
Provide examples for typical commands that a user would like to do with the module.
43+
You can save a credential to a file using `Save-PSCredential`:
3544

3645
```powershell
37-
Import-Module -Name PSModuleTemplate
46+
$credential = Get-Credential
47+
Save-PSCredential -Credential $credential -Path 'C:\secure\credential.cred'
48+
```
49+
50+
This saves the credential to `C:\secure\credential.cred`.
51+
52+
### Example 3: Restoring a `PSCredential` object from a file
53+
54+
To restore a credential object from a previously saved file:
55+
56+
```powershell
57+
Restore-PSCredential -Path 'C:\secure\credential.cred'
58+
```
59+
60+
Alternatively, you can use pipeline input:
61+
62+
```powershell
63+
'C:\secure\credential.cred' | Restore-PSCredential
3864
```
3965

4066
### Find more examples
4167

4268
To find more examples of how to use the module, please refer to the [examples](examples) folder.
4369

44-
Alternatively, you can use the Get-Command -Module 'This module' to find more commands that are available in the module.
45-
To find examples of each of the commands you can use Get-Help -Examples 'CommandName'.
70+
Alternatively, you can use the following commands:
71+
72+
- Find available commands in the module:
73+
74+
```powershell
75+
Get-Command -Module PSCredential
76+
```
77+
78+
- Get examples for a specific command:
79+
80+
```powershell
81+
Get-Help -Examples New-PSCredential
82+
```
4683

4784
## Documentation
4885

49-
Link to further documentation if available, or describe where in the repository users can find more detailed documentation about
50-
the module's functions and features.
86+
For further documentation, please refer to:
87+
88+
- [PSCredential](https://psmodule.io/PSCredential/)
5189

5290
## Contributing
5391

54-
Coder or not, you can contribute to the project! We welcome all contributions.
92+
Whether you're a coder or not, you can contribute to this project!
5593

5694
### For Users
5795

58-
If you don't code, you still sit on valuable information that can make this project even better. If you experience that the
59-
product does unexpected things, throw errors or is missing functionality, you can help by submitting bugs and feature requests.
60-
Please see the issues tab on this project and submit a new issue that matches your needs.
96+
If you experience unexpected behavior, errors, or missing functionality, you can help by submitting bug reports and feature requests.
97+
Please visit the [issues tab](https://github.com/PSModule/PSCredential/issues) and submit a new issue.
6198

6299
### For Developers
63100

64-
If you do code, we'd love to have your contributions. Please read the [Contribution guidelines](CONTRIBUTING.md) for more information.
65-
You can either help by picking up an existing issue or submit a new one if you have an idea for a new feature or improvement.
101+
If you are a developer, we welcome your contributions!
102+
Please read the [Contribution Guidelines](CONTRIBUTING.md) for more information.
103+
You can either help by picking up an existing issue or submit a new one if you have an idea for a feature or improvement.
66104

67-
## Acknowledgements
105+
## Disclaimer
68106

69-
Here is a list of people and projects that helped this project in some way.
107+
The Export-Clixml cmdlet is used to save credentials to disk, is not secure on Linux and macOS, and should be used with caution.
108+
For more information read the [Export-Clixml](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-7.5#example-4-exporting-a-credential-object-on-linux-or-macos) documentation.

examples/General.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# PSCredential Examples
2+
3+
This documentation provides details on three PowerShell functions for managing `PSCredential` objects:
4+
5+
- **`New-PSCredential`**: Creates a new `PSCredential` object.
6+
- **`Save-PSCredential`**: Saves a `PSCredential` object to a file.
7+
- **`Restore-PSCredential`**: Restores a `PSCredential` object from a file.
8+
9+
---
10+
11+
## `New-PSCredential`
12+
13+
This function generates a `PSCredential` object using a specified username and password.
14+
The password can be provided as a secure string or a plain text string.
15+
16+
### Example 1: Prompting for credentials
17+
18+
```powershell
19+
New-PSCredential
20+
```
21+
22+
Prompts the user for a username and password and returns a `PSCredential` object.
23+
24+
### Example 2: Creating a `PSCredential` object with a predefined password
25+
26+
```powershell
27+
New-PSCredential -Username 'admin' -Password (ConvertTo-SecureString 'P@ssw0rd!' -AsPlainText -Force)
28+
```
29+
Creates a `PSCredential` object for the specified username and password.
30+
31+
---
32+
33+
## `Save-PSCredential`
34+
35+
This function takes a `PSCredential` object and exports it to a file using `Export-Clixml`.
36+
If the specified file path does not exist, the function creates the necessary directory structure before saving the credential.
37+
38+
### Example 1: Saving credentials from user input
39+
40+
```powershell
41+
$credential = Get-Credential
42+
Save-PSCredential -Credential $credential -Path 'C:\secure\credential.xml'
43+
```
44+
Prompts for a username and password, then securely saves the credential to `C:\secure\credential.xml`.
45+
46+
### Example 2: Saving predefined credentials
47+
48+
```powershell
49+
$password = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
50+
$credential = New-PSCredential -Username 'UserName' -Password $password
51+
Save-PSCredential -Credential $credential -Path 'C:\secure\mycreds.xml'
52+
```
53+
54+
Saves the predefined credential to `C:\secure\mycreds.xml`.
55+
56+
---
57+
58+
## `Restore-PSCredential`
59+
60+
The `Restore-PSCredential` function retrieves a `PSCredential` object that was previously saved using `Export-Clixml`.
61+
It reads the file from the specified path and ensures the content is a valid `PSCredential` object before returning it.
62+
63+
### Example 1: Restoring credentials from a file
64+
65+
```powershell
66+
Restore-PSCredential -Path 'C:\secure\mycredential.xml'
67+
```
68+
69+
Restores the `PSCredential` object from the file located at `'C:\secure\mycredential.xml'`.
70+
71+
### Example 2: Using pipeline input to restore credentials
72+
73+
```powershell
74+
'C:\secure\mycredential.xml' | Restore-PSCredential
75+
```
76+
77+
Uses pipeline input to restore the `PSCredential` object from the specified file path.
78+
79+
---

examples/General.ps1

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function New-PSCredential {
2+
<#
3+
.SYNOPSIS
4+
Creates a new PSCredential object.
5+
6+
.DESCRIPTION
7+
This function generates a PSCredential object using a specified username and password.
8+
The password can be provided as a secure string or a plain text string, which will be
9+
converted into a SecureString automatically.
10+
11+
.EXAMPLE
12+
New-PSCredential
13+
14+
Prompts the user for a username and password and returns a PSCredential object.
15+
16+
.EXAMPLE
17+
New-PSCredential -Username 'admin' -Password (ConvertTo-SecureString 'P@ssw0rd!' -AsPlainText -Force)
18+
19+
Creates a PSCredential object for the specified username and password.
20+
21+
.LINK
22+
https://psmodule.io/PSCredential/Functions/New-PSCredential/
23+
#>
24+
25+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
26+
'PSUseShouldProcessForStateChangingFunctions', '',
27+
Justification = 'Does not change state, just holding a credential in memory'
28+
)]
29+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
30+
'PSAvoidUsingUsernameAndPasswordParams', '',
31+
Justification = 'The function is for creating a PSCredential'
32+
)]
33+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
34+
'PSAvoidUsingConvertToSecureStringWithPlainText', '',
35+
Justification = 'The function is for creating a PSCredential'
36+
)]
37+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
38+
'PSAvoidUsingPlainTextForPassword', '',
39+
Justification = 'The function is for creating a PSCredential'
40+
)]
41+
[OutputType([System.Management.Automation.PSCredential])]
42+
[CmdletBinding()]
43+
param(
44+
# Specifies the username for the PSCredential object.
45+
[Parameter()]
46+
[string] $Username = (Read-Host -Prompt 'Enter a username'),
47+
48+
# Specifies the password for the PSCredential object. Accepts a secure string or plain text.
49+
[Parameter()]
50+
[object] $Password = (Read-Host -Prompt 'Enter Password' -AsSecureString)
51+
)
52+
53+
if ($Password -is [String]) {
54+
$Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
55+
}
56+
57+
New-Object -TypeName System.Management.Automation.PSCredential($Username, $Password)
58+
}

src/functions/public/New-PSModuleTest.ps1

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Restore-PSCredential {
2+
<#
3+
.SYNOPSIS
4+
Restores a PSCredential object from a file.
5+
6+
.DESCRIPTION
7+
The Restore-PSCredential function retrieves a PSCredential object that was previously saved using `Save-PSCredential`.
8+
It reads the file from the specified path and ensures the content is a valid PSCredential object before returning it.
9+
10+
.EXAMPLE
11+
Restore-PSCredential -Path 'C:\secure\mycredential.cred'
12+
13+
Restores the PSCredential object from the file located at `C:\secure\mycredential.cred`.
14+
15+
.EXAMPLE
16+
'C:\secure\mycredential.cred' | Restore-PSCredential
17+
18+
Uses pipeline input to restore the PSCredential object from the specified file path.
19+
20+
.LINK
21+
https://psmodule.io/PSCredential/Functions/Restore-PSCredential/
22+
#>
23+
24+
[OutputType([System.Management.Automation.PSCredential])]
25+
[Alias('Import-PSCredential')]
26+
[CmdletBinding()]
27+
param(
28+
# Specifies the path to the credential file to restore.
29+
[Parameter(
30+
Mandatory,
31+
ValueFromPipeline,
32+
ValueFromPipelineByPropertyName
33+
)]
34+
[System.IO.FileInfo] $Path
35+
)
36+
37+
process {
38+
Write-Verbose "Restoring PSCredential from [$Path]"
39+
if (Test-Path $Path) {
40+
$credential = Import-Clixml -Path $Path
41+
} else {
42+
throw "Unable to locate a credential file for [$Path]"
43+
}
44+
45+
if ($credential -isnot [System.Management.Automation.PSCredential]) {
46+
throw "Unable to restore a PSCredential object from [$Path]"
47+
}
48+
49+
$credential
50+
}
51+
}

0 commit comments

Comments
 (0)