0

I have some existing code that I've pieced together from my google searches.

Hi All,

I spent some time googling to find the code needed and this is what I have below. What I'm wanting to do is log the groups being removed from a user either.

a) Into a local text (.log) file or b) Into the users "Notes" field.

I've found code on how to do logging if I use quest addin, but cannot fathom how to make it fit WITHOUT using quest.

Doing this as a learning + work functional task to make it speedier to disable users.

Import-Module activedirectory -ErrorAction silentlycontinue
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction silentlycontinue
$username = read-host "Username"
$users= get-aduser $username

Function RemoveMemberships
{
param([string]$SAMAccountName) 
$user = Get-ADUser $SAMAccountName -properties memberof

$userGroups | %{get-adgroup $_ | Remove-ADGroupMember -confirm:$false -member    $SAMAccountName}
$userGroups | %{get-adgroup $_ | Remove-ADGroupMember -confirm:$false -member    $SAMAccountName}    $userGroups = $user.memberof
$userGroups = $null
}

$users | %{RemoveMemberships $_.SAMAccountName}

Move-ADObject $users -TargetPath "OU=Disabled Users,DC=contoso,DC=com" -PassThru | Disable-ADAccount

edit: The inclusion of the Exchange PSSnapin is for enhancing with disabling on GAL. I have code, but didn't include it in this.

1 Answer 1

0

A simple on-screen answer using Write-Host

$userGroups | %{Write-Host "Removing user from group $_"; get-adgroup $_ | Remove-ADGroupMember -confirm:$false -member $SAMAccountName}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.