Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Credential attribute with title settings and save/load functionality #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ The `-AllowClobber` switch is to allow the BetterCredentials module to do what i
Features
========

BetterCredentials is backwards compatible in the sense that having the module loaded will not break code that uses the built-in commands or attributes in PowerShell. You can, for instance, `Import-Module BetterCredentials` on your computer, and all scripts which call Get-Credential will automatically import stored credentials! Of course, if you write scripts or functions using the additional functionality to control the prompt messages or to -Store, then you have a dependency on BetterCredentials, and you should add: `#requires -Module BetterCredentials`.

Most of the features are available through _both_ the `Get-Credential` command and the `BetterCredentials.CredentialAttribute`, so once you've required the module, you can simply add the attribute to your Credential parameters to store (and load) credentials in the Windows Credential Vault, and control the prompt text, title, etc.

One final note, before we get into the details: BetterCredentials is a old module (from the era of PowerShell 2), and is consequently a Windows-only module. Parts of it could be ported to be cross-platform, but apart from the prompting (which I will try to get added to PowerShell 7.2) the rest of the functionality revolves around storing credentials in the Windows Credential Vault, so it's going to stay Windows-only.

Prompting
---------

Expand All @@ -42,14 +48,15 @@ Once you've stored credentials in the vault, future requests for the same creden

Additionally, in 4.5 there are two commands for searching and/or testing for credentials in the vault: `Find-Credential` and `Test-Credential`...


Unattended Usage
----------------

When Get-Credential is called from a script running unattended, e.g. in a scheduled task, script execution will hang prompting for credentials if there is no credential in the vault corresponding to the given username. Normally one might execute `Get-Credential username -Store` to populate the credential vault prior to putting the scheduled task into production, but might also forget to do so. In version 4.5 the new `Test-Credential` command solves the script hanging problem by returning a true or false value depending on whether a credential corresponding to a user name is currently stored in the vault.

##### NOTES

In my scripts and sample code, I nearly always use `BetterCredentials\Get-Credential` as a way to make sure that I'm invoking this overload of Get-Credential, but the idea is that you can simply import the BetterCredentials module in your profile and automatically get this overload whenever you're calling Get-Credential. Of course, I haven't (yet) overloaded the [Credential] transform attribute, so the automatic prompting when you pass a user name to a `-Credential` attribute doesn't use my module -- you have to explicitly call `Get-Credential`.
In my scripts and sample code, I nearly always use `BetterCredentials\Get-Credential` as a way to make sure that I'm invoking this overload of Get-Credential, but the idea is that you can simply import the BetterCredentials module in your profile and automatically get this overload whenever you're calling Get-Credential.

Although I've added a better Credential attribute, I haven't stepped on the `System.Management.Automation` namespace, so the automatic prompting when you pass a user name to a `-Credential` attribute doesn't use my defintion -- you have to explicitly call `Get-Credential` or write a `ProxyCommand` using the `BetterCredentials.CredentialAttribute`.

Licensed under MIT license, see [License](LICENSE).
Licensed under MIT license, see [License](LICENSE).
5 changes: 5 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#requires -Module @{ ModuleName = "ModuleBuilder"; ModuleVersion = "2.0" }
[CmdletBinding()]param()
Push-Location $PSScriptRoot
Build-Module .\source\
Pop-Location
206 changes: 88 additions & 118 deletions BetterCredentials.psd1 → source/BetterCredentials.psd1
Original file line number Diff line number Diff line change
@@ -1,118 +1,88 @@
@{

# Script module or binary module file associated with this manifest.
RootModule = 'BetterCredentials.psm1'

# Version number of this module.
ModuleVersion = '4.5'

# ID used to uniquely identify this module
GUID = 'd63b6487-26db-49ca-b282-e69a256c23cc'

# Author of this module
Author = 'Joel Bennett'

# Company or vendor of this module
CompanyName = 'HuddledMasses.org'

# Copyright statement for this module
Copyright = '(c) 2014 Joel Bennett. All rights reserved.'

# Description of the functionality provided by this module
Description = 'A (compatible) major upgrade for Get-Credential, including support for storing credentials in Windows Credential Manager, and for specifying the full prompts when asking for credentials, etc.'

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module
FunctionsToExport = 'Get-Credential','Find-Credential','Set-Credential', 'Remove-Credential','Test-Credential'

# Cmdlets to export from this module
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = @()

# Aliases to export from this module
AliasesToExport = @('gcred', 'scred', 'rcred', 'tcred', 'fdcred')

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
FileList = @('CredentialManagement.cs','BetterCredentials.psm1','BetterCredentials.psd1','about_bettercredentials.help.txt', 'LICENSE')

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('Credential','Get-Credential','Vault','Storage')

# A URL to the license for this module.
LicenseUri = 'http://opensource.org/licenses/MIT'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/Jaykul/BetterCredentials'

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
ReleaseNotes = '
This release adds a lot of functionality to the module, allowing enumeration and deletion, etc.

- Add Test-Credential for explicitly checking whether a credential is already stored
- Add Set-Credential for explicitly storing or updating stored credentials
- Add Remove-Credential for clearing stored credentials
- Add Find-Credential to search stored credentials
'

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

@{

# Script module or binary module file associated with this manifest.
RootModule = 'BetterCredentials.psm1'

# Version number of this module.
ModuleVersion = '5.0'

# ID used to uniquely identify this module
GUID = 'd63b6487-26db-49ca-b282-e69a256c23cc'

# Author of this module
Author = 'Joel Bennett'

# Company or vendor of this module
CompanyName = 'HuddledMasses.org'

# Copyright statement for this module
Copyright = '(c) 2014-2020 Joel Bennett. All rights reserved.'

# Description of the functionality provided by this module
Description = 'A (compatible) major upgrade for Get-Credential, including support for storing credentials in Windows Credential Manager, and for specifying the full prompts when asking for credentials, etc.'

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = '5.1'

RequiredModules = @()
RequiredAssemblies = @()
ScriptsToProcess = @()
TypesToProcess = @()
FormatsToProcess = @()
NestedModules = @()
CmdletsToExport = @()
VariablesToExport = @()
DscResourcesToExport = @()

# Functions to export from this module
FunctionsToExport = 'Get-Credential','Find-Credential','Set-Credential', 'Remove-Credential','Test-Credential'
AliasesToExport = @('gcred', 'scred', 'rcred', 'tcred', 'fdcred')

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @('BetterCredentials.cs','BetterCredentials.psm1','BetterCredentials.psd1','about_bettercredentials.help.txt', 'LICENSE')

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('Credential','Get-Credential','Vault','Storage')

# A URL to the license for this module.
LicenseUri = 'http://opensource.org/licenses/MIT'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/Jaykul/BetterCredentials'

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
ReleaseNotes = '
5.0 Added some cast operators that are compatible with, but better than the built-in options.

- By default, Find and Test now search only credentials added by BetterCredentials.
To get the previous behavior, you need to provide the -AllCredentials switch


4.5 Release adds a lot of functionality to the module, allowing enumeration and deletion, etc.

- Add Test-Credential for explicitly checking whether a credential is already stored
- Add Set-Credential for explicitly storing or updating stored credentials
- Add Remove-Credential for clearing stored credentials
- Add Find-Credential to search stored credentials
'
} # End of PSData hashtable
} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

5 changes: 5 additions & 0 deletions source/build.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@{
ModuleManifest = "BetterCredentials.psd1"
OutputDirectory = ".."
CopyPaths = ".\classes\BetterCredentials.cs"
}
Loading