Link to home
Start Free TrialLog in
Avatar of Anthony Janson
Anthony JansonFlag for Hungary

asked on

Need Assistance with Powershell AD and SCCM script

Hiya all,

I have a block of Powershell that doesn't do what I want it to do, else I wouldn't be here. The aim is to import a list from a CSV file (called ADGroupToSCCM.csv) that has the AD group name and the SCCM Collection Name.

From there, the powershell script should extract the list of machines that's in the AD group, find the ResourceID and add the ResourceID through a Direct Membership Rule in SCCM Current Branch (1702).

So far, I got this (name masked). And it's working up to the point with Get-CMDevice -Name $ADMember | Select ResourceID. It just completely skips it. Running it manually does get the desired effect, but not automatically. It is run directly on the server.

Who can help unravel this mystery for me? Note that I am just a starter with Powershell and most of my experience comes from batch and VBS.

Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" 
Set-Location "CE1:"

Import-Module ActiveDirectory

$csvData = Import-CSV -Path C:\Users\hidden\Documents\ADGroupToSCCM.csv

foreach ($row in $csvData)
{
    $row.ADName
    $ADMembers = Get-ADGroupMember $row.ADName -Recursive | Select Name
    foreach ($ADMember in $ADMembers)
    {
    $ADMember
    Get-CMDevice -Name $ADMember | Select ResourceID
    $ResourceID
    Add-CMDeviceCollectionDirectMembershipRule -CollectionName $row.CollectionName -ResourceID $ResourceID -Force
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Anthony Janson

ASKER

That worked perfectly. Looks like I need to use more -ExpandProperty!

Thank you!