Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

problem adding computer to groups

$
0
0

Hi Guys,

I have a question about this powershell script.

I wanted to try AD and Arrays in powershell. Goal: move a computer in multiple AD groups.

What am i doing wrong:

# Get parameters, if not specified ask for them or set default value.
param ([string] $ComputerName)

$ADGroupList = "SCCM.Monday.Patch.2300-2400", "Adm.SCCM.Patch.DTA","Adm.SCCM.SCEP"
$ComputerName = "O-Server01"


# Set LDAP Search Parameters to 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 for powershell array (ie. "adspath", "cn")
$PropertyList = "adspath"

foreach ($Property in $PropertyList)
{
     $Searcher.PropertiesToLoad.Add($Property) | Out-Null
}

foreach ($ADGroup in $ADGroupList){
    #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

    # Get Group Object
    $Group = [ADSI]"$GroupPath"

    # Add Computer to Group
    $Group.Add("$ComputerPath")
    $Group.SetInfo()
  }

Kind regards,

Andre


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>