Get-ADuser in a function

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #6191

    by dafinch at 2012-10-17 04:46:34

    I'm fairly new to powershell and i'm trying to learn how to put together a function with parameters. The parameters seem to be working correctly but i'm having an issue with the actual Get-ADuser. If I try my example below the Get-ADuser works outside of the function but it doesnt inside the function. I've verified that the $ADGroupName contains the name of the group i'm passing into the function. But the get-adgroup inside the function returns nothing.

    I've got powershell v3 installed. Any help on this would be appreciated.

    Import-Module activedirectory
    Function Show-GroupMembers {
    Param(
    [Parameter(Mandatory=$True)]
    [string[]]$ADGroupName
    )

    $ADGroupName
    get-adgroup -filter {name -like $ADGroupName}
    }

    $ADGroupName = "testgroup"

    Show-GroupMembers $ADGroupName

    get-adgroup -filter {name -like $ADGroupName}
    #^--this works

    by DonJ at 2012-10-17 08:23:55

    Couple things. If you're going to use [Parameter()], add [CmdletBinding()] prior to your Param block:


    [CmdletBinding()]
    Param()

    Your other problem is that you've declared $ADGroupName as an array; the -filter parameter doesn't want an array. You can't use -like against an array, either. Inside your function, you'd need to enumerate through that array and deal with just one array item at a time.

    by dafinch at 2012-10-17 09:51:45

    Thanks! That fixed it. Once I put my Get-ADGroup in a foreach it worked perfectly.

    Thanks again

You must be logged in to reply to this topic.