debal
debal

Reputation: 1017

Add user to Remote Admin group on multiple hosts

I am using the following code to add a single user to the Admin group on multiple hosts.

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

When I try to execute using the following command,

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

I get the following error,

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

What is the error here?

Upvotes: 0

Views: 322

Answers (1)

Prasoon Karunan V
Prasoon Karunan V

Reputation: 3043

I can propose you a different approach.

  • Use net localgroup command to do it

  • Run the net localgroup command using Invoke-Command on all computers.

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

Upvotes: 1

Related Questions