Multiple logins for a single credential.

Welcome Forums General PowerShell Q&A Multiple logins for a single credential.

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #55302

    Participant
    Topics: 6
    Replies: 7
    Points: 0
    Rank: Member

    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?

  • #55304

    Participant
    Topics: 9
    Replies: 409
    Points: 603
    Helping Hand
    Rank: Major Contributor

    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' } )
    
  • #55308

    Participant
    Topics: 6
    Replies: 7
    Points: 0
    Rank: Member

    Thank you Sam.

The topic ‘Multiple logins for a single credential.’ is closed to new replies.