如何更新本地用户组用户名



我们的应用程序将在本地用户组中添加用户列表。

用户将与组相关联,如果用户名发生更改,我们可能需要在组中更新它们。

我试图在powershell中使用[ADSI]首先获取列表,然后对其进行修改。

$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 
   }
}

但是ADSI不接受直接传递组名。如果用户名与分配的组关联,如何更新用户名?

是否在要检查的每台计算机上运行powershell脚本?是否要将本地用户添加到本地组?你能尝试一下吗(当然是在管理员外壳内):

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

最新更新