Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommymaynard/98031ccd5de67005bf3063db06a33851 to your computer and use it in GitHub Desktop.
Save tommymaynard/98031ccd5de67005bf3063db06a33851 to your computer and use it in GitHub Desktop.
Specify and Create Multiple Credential Objects
<#
TechNet Contribution: Specify and Create Multiple Credential Objects
Previous link: https://gallery.technet.microsoft.com/Specify-and-Create-d80ad39e
Downloaded: 461 times (as of 05/21/2020)
This advanced function will allow a user to specify and then create multiple credential objects. Creating additional
credential objects allows a user the ability to use different credentials for various purposes while working in the Windows
PowerShell console, or ISE. More information, including a walkthrough of the function's development, can be found here: http://tommymaynard.com/ql-creating-multiple-credential-objects-2015.
Although this advanced function has only been tested on Windows 8.1, I suspect it should work on most other Windows
Operating System versions running, at minimum, PowerShell 2.0.
Update: Version 2.0 was uploaded on May 27, 2016. Read here: http://tommymaynard.com/script-sharing-creating-multiple-credential-objects-part-ii-2016 for more information.
#>
Function New-TMMultiCred {
##### ** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK **
<#
.SYNOPSIS
Prompts user to create credentials objects.
.DESCRIPTION
Prompts user to create credentials objects based on the number specified by the Set parameter.
.PARAMETER Set
This mandatory parameter specifies how many credential objects to create between 1 and 10.
.PARAMETER NoGui
This parameter specifies that the Get-Credential GUI not be displayed to collect the username and password. Instead, it's collected at the command line.
.EXAMPLE
PS > New-TMMultiCred -Set 1
Prompts a user to create one credential object and stores it in $CredSet1. This example will use the Get-Credential GUI.
.EXAMPLE
PS > New-TMMultiCred 2
Prompts a user to create two credential objects and stores them in $CredSet1 and $CredSet2. This example will use the Get-Credential GUI.
.EXAMPLE
PS > New-TMMultiCred 3 -NoGui -Verbose
Prompts a user to create three credential objects and stores them in $CredSet1, $CredSet2, and $CredSet3 without using the Get-Credential GUI. Additionally, this will write verbose information.
.NOTES
NAME: New-TMMultiCred
AUTHOR: Tommy Maynard
VERSION: 2.0
Notes:
- Added the NoGui parameter and code to handle creating credential objects without the use of the Get-Credential GUI.
- Changed to use objects as output, instead of writing statements using Write-Output.
- Added End block, and Added verbose statements to Begin and End blocks.
- Remove existing $CredSet* variables when they exist (completed in Begin block).
- Edited comment-based help: changed some previous help, and added some additional information.
LASTEDIT: 5/26/2016
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position=0)]
[ValidateRange(1,10)]
[int]$Set,
[switch]$NoGui
)
Begin {
Write-Verbose -Message "Removing any previously created credential object variables."
If (Get-Variable -Name CredSet* -Scope Global) {Remove-Variable -Name CredSet* -Scope Global}
Write-Verbose -Message "Beginning to create $Set credential objects."
} #End Begin.
Process {
$Object = @()
For ($i = 1; $i -le $Set; $i++) {
# Create Credential Objects based on number of sets to be created.
## Don't use Get-Credential GUI.
If ($NoGui) {
Write-Verbose -Message "Prompting for credentials for `$CredSet$($i)."
$User = Read-Host -Prompt 'Enter Username'
$Pass = Read-Host -Prompt 'Enter Password' -AsSecureString
try {
Set-Variable -Name CredSet$($i) -Value (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($User, $Pass)) -Scope Global
} catch {
Write-Verbose -Message "Username or password wasn't entered."
Write-Warning -Message "No credential object was created for set $($i)."
}
## Use Get-Credential GUI.
} Else {
try {
Write-Verbose -Message "Prompting for credentials for `$CredSet$($i)."
Set-Variable -Name CredSet$($i) -Value (Get-Credential -Credential $null) -Scope Global
} catch {
Write-Verbose -Message 'User canceled prompt for credentials.'
Write-Warning -Message "No credential object was created for set $($i)."
}
}
# Create object and append to object variable.
If (Get-Variable -Name CredSet$($i) -Scope Global -ErrorAction SilentlyContinue) {
$Object += [pscustomobject]@{
'CredSet' = [string]$i
'Variable' = "`$Credset$i"
'UserName' = (Get-Variable -Name CredSet$($i) -ValueOnly).Username
'Password' = (Get-Variable -Name CredSet$($i) -ValueOnly).Password
}
}
} # End For.
Write-Verbose -Message 'Writing output information.'
$Object
} #End Process.
End {
Write-Verbose -Message "Completed creating $Set credential objects."
} # End End.
} #End Function: New-TMMultiCred.
@tommymaynard
Copy link
Author

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment