none
Test Scripts for Code RRS feed

  • General discussion

  • Use PsToHtml.vbs V 1.6 4/27/2012

    Function AddMember
    {
        # Function to add a user member to a group.
        # Prompt for member to add.
        $Member = Read-Host "Enter user to be added to the group"

        # Check if the user exists.
        Try {
                $User = (Get-ADUser -Identity $Member).distinguishedName
            }
        Catch
            {"User $Member not found"; Break}

        # Add the user to the group.
        Try
            {
                Add-ADPrincipalGroupMembership -Identity $Member -MemberOf $GroupName  
            }
        Catch
            {
                "$Member is already a member of $GroupName"
                "Or you lack permissions to add members to the group"
            }
    }

    # Prompt for group name.
    $GroupName = Read-Host "Enter Group Name"

    #Retrieve number of members in the group.
    Try
        {$Group = (Get-ADGroup -Identity $GroupName -Properties members).members}
    Catch
        {"Group $GroupName not found"; Break}
    $Count = $Group.Count

    Switch ($GroupName)
    {
        "Accounting"
            {If ($Count -ge 100)
                 {
                     "Group $GroupName already has 100 members"
                     "No more members can be added"
                 }
             Else {AddMember}
            }
        "cn=Engineering,ou=West,dc=MyDomain,dc=com"
            {If ($Count -ge 100)
                 {
                     "Group $GroupName already has 100 members"
                     "No more members can be added"
                 }
                Else {AddMember}
            }
        "Grade7"
            {If ($Count -ge 50)
                 {
                     "Group $GroupName already has 50 members"
                     "No more members can be added"
                 }
                Else {AddMember}
            }
        "Grade8"
            {If ($Count -ge 50)
                 {
                     "Group $GroupName already has 50 members"
                     "No more members can be added"
                 }
                Else {AddMember}
            }
         Default
             {
                 # Group with no membership limit.
                 AddMember
             }
    }

    -----

    Use PsToHtml.vbs V 1.9 1/5/2015

    Function AddMember
    {
        # Function to add a user member to a group.
        # Prompt for member to add.
        $Member = Read-Host "Enter user to be added to the group"
    
        # Check if the user exists.     Try {             $User = (Get-ADUser -Identity $Member).distinguishedName         }     Catch         {"User $Member not found"; Break}
        # Add the user to the group.     Try         {             Add-ADPrincipalGroupMembership -Identity $Member -MemberOf $GroupName           }     Catch         {             "$Member is already a member of $GroupName"             "Or you lack permissions to add members to the group"         } }
    # Prompt for group name. $GroupName = Read-Host "Enter Group Name"
    #Retrieve number of members in the group. Try     {$Group = (Get-ADGroup -Identity $GroupName -Properties members).members} Catch     {"Group $GroupName not found"; Break} $Count = $Group.Count
    Switch ($GroupName) {     "Accounting"         {If ($Count -ge 100)              {                  "Group $GroupName already has 100 members"                  "No more members can be added"              }          Else {AddMember}         }     "cn=Engineering,ou=West,dc=MyDomain,dc=com"         {If ($Count -ge 100)              {                  "Group $GroupName already has 100 members"                  "No more members can be added"              }             Else {AddMember}         }     "Grade7"         {If ($Count -ge 50)              {                  "Group $GroupName already has 50 members"                  "No more members can be added"              }             Else {AddMember}         }     "Grade8"         {If ($Count -ge 50)              {                  "Group $GroupName already has 50 members"                  "No more members can be added"              }             Else {AddMember}         }      Default          {              # Group with no membership limit.              AddMember          } }

    --

    Use "Insert Code Block"

    Function AddMember
    {
        # Function to add a user member to a group.
        # Prompt for member to add.
        $Member = Read-Host "Enter user to be added to the group"
    
        # Check if the user exists.
        Try {
                $User = (Get-ADUser -Identity $Member).distinguishedName
            }
        Catch
            {"User $Member not found"; Break}
    
        # Add the user to the group.
        Try
            {
                Add-ADPrincipalGroupMembership -Identity $Member -MemberOf $GroupName   
            }
        Catch 
            {
                "$Member is already a member of $GroupName"
                "Or you lack permissions to add members to the group"
            }
    }
    
    # Prompt for group name.
    $GroupName = Read-Host "Enter Group Name"
    
    #Retrieve number of members in the group.
    Try
        {$Group = (Get-ADGroup -Identity $GroupName -Properties members).members}
    Catch
        {"Group $GroupName not found"; Break}
    $Count = $Group.Count
    
    Switch ($GroupName)
    {
        "Accounting"
            {If ($Count -ge 100)
                 {
                     "Group $GroupName already has 100 members"
                     "No more members can be added"
                 }
             Else {AddMember}
            }
        "cn=Engineering,ou=West,dc=MyDomain,dc=com"
            {If ($Count -ge 100)
                 {
                     "Group $GroupName already has 100 members"
                     "No more members can be added"
                 }
                Else {AddMember}
            }
        "Grade7"
            {If ($Count -ge 50)
                 {
                     "Group $GroupName already has 50 members"
                     "No more members can be added"
                 }
                Else {AddMember}
            }
        "Grade8"
            {If ($Count -ge 50)
                 {
                     "Group $GroupName already has 50 members"
                     "No more members can be added"
                 }
                Else {AddMember}
            }
         Default
             {
                 # Group with no membership limit.
                 AddMember
             }
    }
    


    Richard Mueller - MVP Directory Services

    Thursday, September 10, 2015 4:34 PM