将用户添加到多个主机上的远程管理组

时间:2018-10-01 18:14:43

标签: powershell

我正在使用以下代码将单个用户添加到多个主机上的Admin组中。

param(
        [string] $Domain,
        [string] $UserName
    )

$ComputerListFile = "D:\Scripts\AddWindowsUser\ComputerList.txt"
$ComputerList = Get-Content $ComputerListFile -ErrorAction SilentlyContinue

foreach( $Computer in $ComputerList) {
    $Group = [ADSI]"WinNT://$Computer/Administrators,group"
    $User = [ADSI]"WinNT://$Domain/$UserName,user"
    $Group.Add($User.Path)
}

当我尝试使用以下命令执行时,

.\AddWindowsUser.ps1 -Domain "AD" -User "356989"

出现以下错误,

distinguishedName : 
Path              : WinNT://computername.domain.global/Administrators,group

The following exception occurred while retrieving member "distinguishedName": "The network 
path was not found.
"
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand

这是什么错误?

1 个答案:

答案 0 :(得分:1)

我可以为您提出另一种方法。

  • 使用net localgroup命令

  • 使用net localgroup运行Invoke-Command命令 在所有计算机上。

    Invoke-Command -Computer $ComputerNames -Scriptblock {
        Param(
             $UserName,
            $DomainName
        )
        net localgroup administrators /add $Domain\$UserName
    } -ArgumentList $UserName,$DomainName
    
相关问题