Adding user to servers' administrator group, encountering error

0

I'm working on a script that will let me add a user to a group of servers, and then remove said user from the group at a later date. Right now I can make the script work when I only have one server but when I use a comma-separated list I get the following error: Exception calling "Invoke" with "2" argument(s): "Unknown error (0x80005000)"

I'm running the script as an admin on the machines necessary, and my execution policy is set to allow the script to run on any of the servers I'm targeting. Additionally, the domain situation is:

Target servers:

DomainA/ServerA
DomainA/ServerB

Users:

DomainB/UserA

I can add the user fine from being logged in to the server. Here's the code I'm working with:

param($serverName, $group, $user, $action)

if (! $serverName) {
  $serverlist = read-host "Enter server(comma separated list): ".split(",")
}

if (! $group) {
  $group = read-host "Enter group: "
}

if (! $action) {
  $action = read-host "Add, Remove or List: "
}

$objGroup = [ADSI]("WinNT://$serverName/$group")

if ($action.ToLower() -eq "list") {
  $members = @($objGroup.psbase.Invoke("Members"))
  $members | foreach {
    $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
  }
} else {
  if (! $user) {
    $user = read-host "Enter domain user id: "
  }

  foreach ($serverName in $serverList) {
    $serverName = $serverName.Trim().ToUpper()
    $objADUser = [ADSI]("WinNT://$serverName/$user")
  }

  $objGroup.PSBase.Invoke($action,$objADUser.PSBase.Path)
}
powershell
active-directory
asked on Stack Overflow Jul 2, 2013 by Sean Long • edited Jul 2, 2013 by Ansgar Wiechers

1 Answer

1

Change read-host "Enter server(comma separated list): ".split(",") to (read-host "Enter server(comma separated list)").split(",") (parenthesis are added)

answered on Stack Overflow Jul 3, 2013 by Χpẘ

User contributions licensed under CC BY-SA 3.0