Thursday, February 5, 2015

Everyday Powershell - Part 30 - Beware of reinventing the wheel

Hmmm bit of a funny one today... So I spent a few minutes writing this;

function get-recursegroupmembers{
    [CmdletBinding()]
    param
    (
        [Parameter(mandatory=$true,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [string]$groupname
    )
    $users = @()
    $groups = get-adgroupmember $groupname
    foreach($object in $groups){
        if ($object.objectclass -eq "group"){
            get-recursegroupmembers $object
        }
        if ($object.objectclass -eq "user"){
            $users += $object
        }
    }
    $users
}

When I realised that this would do just fine;
get-adgroupmember $group -Recursive

So yeah, lesson for the day is don't spend valuable time writing stuff when the problem has already been solved.

1 comment:

  1. I always stress reading and re-reading help and examples. Even I will re-read help and discover something I didn't know about or pay attention to before.

    ReplyDelete