none
Powershell Copy Computer group membership to multiple computers.

    Question

  • Hi All,

    I was wondering if you could help. I have a script that will copy one computers membership to another, but I am after something that will take a computers groups and then copy them to a list of machines in a CSV file. I have a script that will do it from one to another, is there a way I can amend it for a CSV file?

     Param(

        [Parameter(Mandatory=$True)]

        [String] $FromComputer,

        [Parameter(Mandatory=$True)]

        [String] $ToComputer

    )

    Process

    {

        Import-Module ActiveDirectory

        $ToClient = Get-AdComputer -Identity $ToComputer

        $FromClient = Get-ADComputer -Identity $FromComputer -Properties MemberOf

        Foreach ($Group in $Client.MemberOf)

        {

            $GroupObj = Get-ADGroup -Identity $Group

            Add-ADGroupMember -Identity $GroupObj -Members $ToClient

            

        }

        

    Friday, April 21, 2017 2:03 PM

All replies

  • $comps = import-csv yourcsvfile foreach ($comp in $comps){

    $tocomputer = $comp yourfunction }

    You might need to use $comps.name or similar, refer to your csv file.
    Friday, April 21, 2017 2:09 PM
  • Thanks for the reply but I am obviously missing something. I have added your text it looks as though it has worked but it is not updating the machines computer account.

     Param(

        [Parameter(Mandatory=$True)]

        [String] $FromComputer,

    )

    Process

    {

        Import-Module ActiveDirectory

        $comps = import-csv C:\scripts\Priestman.csv

        foreach ($comp in $comps.Name){

        $tocomputer = $comp

    }

        $ToClient = Get-AdComputer -Identity $toComputer

        $FromClient = Get-ADComputer -Identity $FromComputer -Properties MemberOf

        Foreach ($Group in $Client.MemberOf)

        {

            $GroupObj = Get-ADGroup -Identity $Group

            Add-ADGroupMember -Identity $GroupObj -Members $ToClient

            

        }

       

    Friday, April 21, 2017 3:03 PM