powershell - 如何更新本地用户组的用户名?

标签 powershell powershell-2.0

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

用户将与组相关联,如果更改了用户名,我们可能需要按组进行更新。

我试图在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脚本?
是否要将本地用户添加到本地组?
你能尝试类似的东西吗(当然是在管理员 shell 内):

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

关于powershell - 如何更新本地用户组的用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11687152/

相关文章:

Powershell如何创建委托(delegate)

performance - perfmon如何获取 "in Use"内存

c# - 如何以编程方式知道传递给 cmdlet 的所有参数?

powershell - 在PowerShell中获取文件的完整路径

events - 在 PowerShell 中订阅对象的静态事件的语法是什么?

c# - 为什么 Powershell 无法识别 Add_Type block 中的 System.Data?

c# - 调用AzureInformationProtection Powershell命令时出现C#Microsoft InformationProtection解密错误

Powershell 在 reg 导入时抛出终止错误,但操作成功完成

PowerShell 别名、PSScriptAnalyzer 和 VSCode

powershell - 如何在powershell中执行击键?