We have several contractors that come and go, it would be helpful to have a custom command that will copy only the member of groups from one user to another. We have done this previously with a powershell script, however when we tried to create a custom command in Adaxes we received the following errors.
Powershell script is:
$verbose = $true
Set-Alias read Read-Host
$logger = ".\user_anlage.txt"
# Import Module for ActiveDirectory
Import-Module ActiveDirectory
Clear-Host
function message($message) {
write-host $message -f "Yellow"
}
function print_groups($username) {
$groups = Get-ADPrincipalGroupMembership $username
foreach ($g in $groups) {write " + $($g.name)"}
}
try {
message "`Copycat! Copy group memberships of user A to B`n"
$username = read 'enter username'
message "User: $username`n"
print_groups $username
$option = read "`ncopy to user? (y|n)"
if($option -eq "y") {
do {
$copy_user = read "enter username"
message "User: $copy_user`n"
$groups = Get-ADPrincipalGroupMembership $username
try {
Add-ADPrincipalGroupMembership $copy_user -MemberOf $groups -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
catch { message " + " }
print_groups $copy_user
$option = read "`ncopy to user? (y|n)"
}
while($option -eq "y")
}
# script finished
message "`nFantastic!`n"
}
catch {
Write-Host $Error[0].Exception;
write-warning "I'm sorry, Jay. I'm afraid I can't do that."
}