Use Powershell to Add AD Computer Object to an AD Group


hello, trying write script adds computer object in active directory group.  script works fine if run against computer object has been in ad long time.  however, if run script on object has been added domain, fails.  purpose of process run on newly domain joined machines, isn't working.

i following error:

the following exception occurred while retrieving member "add": "unknown error (0x80005000)"

anyone have ideas why happening , ideas on how fix it?  script below (note: can't use ad cmdlets accomplish have able run servers not have components installed):

# parameters, if not specified ask them or set default value.
param ([string] $adgroup, [string] $computername)
if (!$adgroup)
{
 $adgroup = read-host "enter active directory group add object to: "
}

if (!$computername)
{
    $computername = $env:computername
}

# set ldap search parameters find computer account.
$computerfilter = "(&(objectcategory=computer)(cn=$computername))"
$groupfilter = "(&(objectcategory=group)(cn=$adgroup))"

$domain = new-object system.directoryservices.directoryentry

$searcher = new-object system.directoryservices.directorysearcher
$searcher.searchroot = $domain
$searcher.pagesize = 1000
$searcher.searchscope = "subtree"

# set ldap property list, comma seperated powershell array (ie. "adspath", "cn")
$propertylist = "adspath"

foreach ($property in $propertylist)
{
    $searcher.propertiestoload.add($property) | out-null
}

#find computer path
$searcher.filter = $computerfilter
$findcp = $searcher.findone()
$computerpath = $findcp.properties.adspath

#find group path
$searcher.filter = $groupfilter
$findgp = $searcher.findone()
$grouppath = $findgp.properties.adspath

# group object
$group = [adsi]"$grouppath"

# add computer group
$group.add("$computerpath")
$group.setinfo()

roy, ended doing creating wrapper script. my wrapper contained various variable data pass main script. run specified credentials, create credential , use invoke-command pass credential:

$credentials = new-object -typename system.management.automation.pscredential -argumentlist $user,$pwsecure
invoke-command -filepath "$($path)myscript.ps1" -argumentlist "my group", "otherdata" -credential $credentials



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2