locked
Powershell ask for credentials and save in powershell RRS feed

  • Question

  • Hello,

    I would like to know if it's possible to ask for a Username and store it in the same Powershell file, i got this powershell command to copy MemberOf from 1 to another AD User.

    Underneath you can find the script i use. I want Powershell to ask for for the Name of the Source and Target and store it in between both '-':

        $Source=''    
        $Target=''

    # Script to copy group memberships from a source user to a target user.

    Param ($Source, $Target)

    If ($Source -ne $Null -and $Target -eq $Null)
    {
        $Target=''
    }
    If ($Source -eq $Null)
    {
        $Source=''    
        $Target=''
    }

    # Retrieve group memberships.
    $SourceUser = Get-ADUser $Source -Properties memberOf
    $TargetUser = Get-ADUser $Target -Properties memberOf

    # Hash table of source user groups.
    $List = @{}

    #Enumerate direct group memberships of source user.
    ForEach ($SourceDN In $SourceUser.memberOf)
    {
        # Add this group to hash table.
        $List.Add($SourceDN, $True)
        # Bind to group object.
        $SourceGroup = [ADSI]"LDAP://$SourceDN"
        # Check if target user is already a member of this group.
        If ($SourceGroup.IsMember("LDAP://" + $TargetUser.distinguishedName) -eq $False)
        {
            # Add the target user to this group.
            Add-ADGroupMember -Identity $SourceDN -Members $Target
        }
    }

    # Enumerate direct group memberships of target user.
    ForEach ($TargetDN In $TargetUser.memberOf)
    {
        # Check if source user is a member of this group.
        If ($List.ContainsKey($TargetDN) -eq $False)
        {
            # Source user not a member of this group.
            # Remove target user from this group.
            Remove-ADGroupMember $TargetDN $Target
        }
    }

    -----

    Is there any 1 who knows how to ask for credentials and store it in the same powershell.

    Thanks!

    Thursday, May 18, 2017 9:15 AM

All replies

  • Did you try to use Get-credential command ?
    Thursday, May 18, 2017 12:46 PM
  • You can create a credential variable and save it like this:

    $credentials = get-credential

    This will prompt you for the creds and save as a variable to reuse


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Proposed as answer by Hello_2018 Monday, May 22, 2017 7:21 AM
    Friday, May 19, 2017 1:12 PM
  • Hi Perry,

    Apart from others suggestion, you could also try this:

    $password =Read-Host -Prompt "mypassword" | ConvertTo-SecureString -asPlainText -Force

    $username = "nwtraders\administrator" 

    $credential = New-Object System.Management.Automation.PSCredential($username,$password)

    Refer to link below:

    https://msdn.microsoft.com/en-us/library/system.management.automation.pscredential%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Monday, June 5, 2017 7:02 AM
    Monday, May 22, 2017 7:21 AM
  • I have seen a way how to export pscred to xml file for later use, it gets crypted with your account rsa key. In the pub now, let me know if you want me to search for it tomorrow

    --------------------- Leos

    Friday, May 26, 2017 5:55 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, June 5, 2017 7:01 AM