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
  • What format do you have in your csv file?
    Monday, April 24, 2017 3:47 PM
  • Good Day

    Please try this

    $Computers = Import-Csv c:\scripts\Computers.csv
    foreach ($Computer in $Computers) {
    $ToClient = Get-AdComputer -Identity $Computer
    $FromClient = Get-ADComputer -Identity $FromComputer -Properties MemberOf
    Foreach ($Group in $FromClient.MemberOf){
    $GroupObj = Get-ADGroup -Identity $Group
    Add-ADGroupMember -Identity $GroupObj -Members $ToClient}
    }

    Regards

    Tuesday, April 25, 2017 3:38 PM
  • Hi Peter,

    >>Foreach ($Group in $Client.MemberOf)

    This line has issue, $client is not defined!!!

    Best regards,

    Andy



    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Wednesday, April 26, 2017 6:24 AM
    Moderator