Multiple logins for a single credential.

I have a need for storing multiple logins/passwords in a single credential object. Some time ago, I received a PowerShell Tip, which said it is possible. But I have lost that mail.

Here’s the crude way, I’m using:

$senduser1 = "User1" $password1 = "Pwd1"

$senduser2 = “User2”
$password2 = “Pwd2”

$PWord11 = ConvertTo-SecureString –String $password1 –AsPlainText -Force
$PWord22 = ConvertTo-SecureString –String $password2 –AsPlainText -Force

$Credz = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $senduser1, $PWord11
$Credz| where {$_.UserName -like ‘User1’}

$Credz = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $senduser2, $PWord22
$Credz| where {$_.UserName -like ‘User2’}

Is there a better way to do this?

Yes, see this post and PS function. And as an added benefit, you don’t have to put clear text passwords in script…

You can have an array of PS Credential objects like:

$Creds = @() # Declare an array

$Creds += Get-SBCredential 'domain1\MyADAdmin1'
$Creds += Get-SBCredential 'domain2\MyADAdmin1'
$Creds += Get-SBCredential 'domain1\MyADAdmin2'

$Creds # Show credentials

# Example using the credentials:
Disable-ADAccount -Identity 'Someone' -Server 'MyDomainController1' -Credential ( $Creds | where { $_.UserName -eq 'domain2\MyADAdmin1' } )

Thank you Sam.