ccoleman7059
ccoleman7059

Reputation: 13

Pull Samaccount name from get-adprincipal powershell

I currently have a powershell script to remove a user from all groups except domain users and logs the changes to the users account. When the script logs the changes to the account, it pulling the distinguished name. I would like the log entry show just the name of the group and not the whole distinguished name if possible. Maybe there is a better way to do this as well.

Thanks

$AssignendGroups = Get-ADPrincipalGroupMembership -Identity $sAMAccountName_Attribute
ForEach($ADGroup in $AssignendGroups){
    If($ADGroup -notlike "*Domain Users*"){

        Remove-ADPrincipalGroupMembership -Identity $sAMAccountName_Attribute -MemberOf $ADGroup -Confirm:$False

        Build-OutputLog -Text ("[Group Removal Action] - User: '$Name_Attribute' (Account: $sAMAccountName_Attribute) has removed been from group: $ADGroup") > $null
        Write-Output "[Group Removal Action] - User: '$Name_Attribute' (Account: $sAMAccountName_Attribute) has removed been from group: $ADGroup"

    }Else{
        Build-OutputLog -Text ("[Skipped Group Removal Action] - User: '$Name_Attribute' (Account: $sAMAccountName_Attribute) is a member of the default group: $ADGroup. Cannnot remove user from this group.") > $null
        Write-Output "[Skipped Group Removal Action] - User: '$Name_Attribute' (Account: $sAMAccountName_Attribute) is a member of the default group: $ADGroup. Cannnot remove user from this group."   

Upvotes: 0

Views: 156

Answers (1)

Jawad
Jawad

Reputation: 11364

Instead of using $ADGroup, use $ADgroup.Name to log only the name. Within the string, you will have to use $($ADGroup.Name)

example from your code...

Build-OutputLog -Text ("[Group Removal Action] - User: '$Name_Attribute' (Account: $sAMAccountName_Attribute) has removed been from group: $($ADGroup.Name)") | Out-Null

Instead of using > $null, I would recommend using powershell way of ignoring the output by using | out-null

Also, not sure what $Name_Attribute value is supposed to be, you might want to check that as well.

Upvotes: 1

Related Questions