2
votes

Our application will add list of user in local user groups.

Users will be associated with the groups , if the user name is changed we may need to update them in groups.

I was trying to use [ADSI] in powershell to get the list first then modify it .

$MyprojGroups=@("Myproj Engineers",
             "Myproj Managers",
             "MyprojDBUser",
             "MyprojUser")

Foreach( $MyprojGroup in $MyprojGroups) {
    Write-host "MyprojGroup : $MyprojGroup "
    $usergroup=[ADSI]($MyprojGroup).psbase.Path
    $usergroup

    UpdateUserName -groupName $usergroup -OlduserName "Administrator" -NewuserName "Admin"

}

Function UpdateUserName {
Param (
    [string]$OlduserName,
    [string]$groupName,
    [string]$NewuserName
)

    # To check whether the user name is associated with the group
$MEm=$groupName.psbase.Invoke("Members") | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

   If ($mem -eq "OlduserName") {

       # Has to update the New User Name 
   }
}

But passing the groupname directly is not being accepted for ADSI. How to update the user name if it associated with the assigned groups?

1
If the user's name is changed in the user object, then groups are automagically updated (unique id, known as SID saved us here :-). So it is alla matter of add & remove users.Mat M

1 Answers

0
votes

do you run your powershell script on each computer you want to check ? Do you want to add local users to the local groups ? can you try something like (within administrator shell of course) :

$grp = [ADSI]"WinNT://$computerName/$groupName,group"
$grp.add("WinNT://$computerName/$NewUserName")
$grp.remove("WinNT://$computerName/$OldUserName")