2

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

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

我试图在 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 不接受直接传递组名。如果用户名与分配的组关联,如何更新用户名?

4

1 回答 1

0

您是否在要检查的每台计算机上运行您的 powershell 脚本?您想将本地用户添加到本地组吗?您可以尝试类似(当然在管理员外壳中):

$grp = [ADSI]"WinNT://$computerName/$groupName,group"
$grp.add("WinNT://$computerName/$NewUserName")
$grp.remove("WinNT://$computerName/$OldUserName")
于 2012-11-17T19:44:07.170 回答