﻿# api: multitool
# version: 0.2
# title: Add users to groups
# description: Add-ADGroupMember
# type: inline
# category: bulk
# icon: user
# param: username, bulk
# vars:
#    { name: usernames, type: text, value="$username", description: User/users to add to groups }
#    { name: groups, type: text, value="PERM_Test1; PERM_StoreThings", description: "list of permission groups, can be delimited by comma/semicolon/spaces" }
# hidden: 0
# status: beta
# config: -
#
# Add user(s) to list of groups
#
#  ❏ `username` from standard field
#  ❏ comma-separated list of AD groups in `groups` field
#


Param(
    $usernames = (Read-Host "Usernames"),
    $groups = (Read-Host "groups")
);

$usernames = $usernames -split "[;,\s]+" | ? { $_ -match "\w+" } | % { $_ -replace "^\w+\\","" } | ? { Get-ADUser $_ }
$groups = $groups -split "[;,\s]+" | ? { $_ -match "\w+" }


ForEach ($group in $groups) {
    Write-Host "❏ Add-ADGroupMember -Identity $group -Members $($usernames -join ',')"
    $r = Add-ADGroupMember -Identity $group -Members $usernames
    if ($r) {
        Write-Host -f Green "✔ added"
    }
    else {
        Write-Host -f Red "✘ (already had?)"
    }
}

