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
#WORKS
get-expUserGroup -identity ‘test’
#DOES NOT WORK
‘test’ | get-expUserGroup
Function Get-expUserGroup{
#Add Comment Based Help
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True,ValuefromPipeline = $True, ValueFromPipeLineByPropertyname = $True)]
[String[]]$identity
)
Begin{}
Process{
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
$obj
} # inner foreach
} # outter foreach
} # Process
End{}
} # 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> ‘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
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.