i have the following Situation: Im trying to create a PS Module to export ADUsers to an excel file as .xlsx file, as a own function. When i pipe a Get-ADUser command to my Parameter, i do not get the whole users, i do only get the last one. I have declared the inputvariable as ADAcount Object and also as array..
Hier is my Code in extra short format:
Function Export-Excel
{
[cmdletBinding()]
param(
[parameter(ValueFromPipeline)]
[Microsoft.ActiveDirectory.Management.ADAcount[]] $Users
)
Write-Host $Users.count
...........
}
When i now call the function via powershell it looks like:
Get-ADUser -Filter * -Properties * | Export-Excel
# Output: 1
# And this one is an ADUser Object, the last created one / the last one in the array...
# When i do this:
$user = Get-ADUser -Filter * -Properties *
Write-Host $user.count
# Output: 2500
I hope you understand my problem :)
Thanks a lot and have nice christmas time :)
CodePudding user response:
If you want to pipe all your user objects, import and work with the total (for reporting) you'll need to use the Begin
, Process
, and End
keywords as detailed in get-help about_functions
, for example:
Function Export-Excel {
[cmdletBinding()]
param(
[parameter(ValueFromPipeline)]
[Microsoft.ActiveDirectory.Management.ADUser[]]$Users
)
begin {
$usersArray = @()
}
process {
foreach ($user in $Users) {
$usersArray = $user
}
}
end {
Write-Host $usersArray.count
}
}
Get-ADUser -Filter * -Properties * | Export-Excel
CodePudding user response:
I did do, a bit, research, and found: Powershell Array parameter from pipline
Then I changed your script, a little, because I do not have an AD:
Function EE
{
[cmdletBinding()]
param(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
#[Microsoft.ActiveDirectory.Management.ADAcount[]] $Users
[string[]]$Users
)
process {
write-Host $Users
}
}
Get-LocalUser | EE
The explanation is: The "process" block is run once for each item in the pipeline.
This will get me a list of all my local users.