value from pipeline by value

I wrote this function to list just the groups from a user. I don’t see why I can’t pipe String values to the function if the identity parameter accepts values from pipeline by value. I must be missing something simple here

get-expUserGroup -identity ‘test’

‘test’ | get-expUserGroup

Function Get-expUserGroup{
  #Add Comment Based Help

  [Parameter(Mandatory = $True,ValuefromPipeline = $True, ValueFromPipeLineByPropertyname = $True)]

    foreach($user in $identity){
      $groups = get-aduser $user -Properties memberof | Select-Object -ExpandProperty memberof
      foreach($group in $groups){
        $split = $group -split ',*..='
        $properties = @{
                         Group = $split[1]
                         DistinguishedName = $group
        $obj = New-Object -TypeName PSObject -Property $properties
      } # inner foreach
    } # outter foreach
  } # Process


} # End Function

Can you be more specific about “does not work?” Do you get an error? Just nothing? I usually add some Write-Verbose statements to tell me what I’ve got -

Write-Verbose “Now on $user”

For example, just inside your first ForEach. You then run with -Verbose to activate that output.

Or, assuming you’re using the ISE or VS Code because why wouldn’t you, have you tried debugging this? Set a breakpoint on line 13 there, and hit F11 to step through one line at a time?

Or, have you tried using Trace-Command (there’s a lovely example in the help that shows how to enable pipeline diagnostics) to see what PowerShell is doing with the parameters?

PS C:\Users\kgreer> Get-expUserGropup -identity kgreer

DistinguishedName Group

CN=SCCM-Role-ReadAnalyst,OU=End User Security Groups,OU=IT Infrastructure,DC=domain,DC=com SCCM-Role-ReadAnalyst
CN=SR-EUS-RestoreADObjects,OU=Security Roles,OU=Access Groups,OU=IT Administration,DC=domain,DC=com SR-EUS-RestoreADObjects

PS C:\Users\kgreer> ‘kgreer’ | Get-expUserGropup
Get-expUserGropup : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input
or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:12

  • ‘kgreer’ | Get-expUserGropup
  •        ~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (kgreer:String) [Get-expUserGropup], ParameterBindingException
    • FullyQualifiedErrorId : InputObjectNotBound,Get-expUserGropup

I found the problem. I incorrectly named the funciton Get-expUserGropup instead of Get-expUserGroup. ISE remembered the old function and I used tab completion in the command window and did not realize it was the bad function in memory. Is there a way to clear ISE memory without closing it? I also use ISE Steroids.

Thank you for the reply.

Yeah, “not use ISE” has been my solution. VS Code is 1.3 million times better and doesn’t do that.