Link to home
Start Free TrialLog in
Avatar of SAM IT
SAM IT

asked on

empty pipe element is not allowed - powershell

Below script is working fine. when script executes by added | export-csv getting error "empty pipe element is not allowed"

Thanks in advance

Import-Module ActiveDirectory
$users = Get-Content "C:\input.txt"

foreach ($SamAccountName in $users)
	{ Get-ADPrincipalGroupMembership -Identity $SamAccountName |
		Select-Object -Property name, distinguishedname, @{n='samaccountname'; e={$SamAccountName}}} | Export-Csv -NoTypeInformation c:\1.csv

Open in new window

SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM IT
SAM IT

ASKER

not output is fine. for example if i specify 100 users in input file the output will be only for one user. I have added -append after .csv output not luck
I didn't quite understand what you said in your comment, can you amplify the information?
Avatar of SAM IT

ASKER

Output which I am getting is getting over writing.

I have added -append after export-csv not to overwrite, still getting error.

Using powershell version 20.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM IT

ASKER

Thanks a lot. Thanks for explanation
Avatar of SAM IT

ASKER

Hi obda,


I am new to powershell. I have basic question to you.

In powershell alias I found that ForEach-Object and foreach both are same. foreach is just an alias for foreach-object.

Is there any difference for foreach and foreach-object? and I did not understand ou'd need to wrap the statement into a subexpression can you explain in easier way to understated
There's a "ForEach-Object" cmdlet, and the "ForEach" statement. To make things more complicated (or easier, when it comes to typing ...), there's a "ForEach" alias for "ForEach-Object".
When PowerShell finds a "ForEach", it determines by the context/syntax used ("ForEach ($v In $Whatever) {}" vs. "Get-Whatever | ForEach {}") whether the statement or the alias is used.
The "ForEach" statement can not be used directly as part of a pipeline; only if you put the whole thing into a subexpression "$(...)" will you be able to use the output generated by a "ForEach" statement subsequently in a pipeline.
The subexpression forces PowerShell to evaluate its content and pumps the output into the pipeline.
Without using the pipeline, you'd have to add the output to the file in each loop iteration, using "-Append". But the "Export-Csv -Append" is only available since PS 3.0 (you should upgrade, btw.; seriously, PS 2.0 dates back to 2009), and it would be slow because the file would need to be opened and closed for each append.
Then using the statement is, as mentioned before, resource intensive, because it needs to store all of the input data before iterating. Using the pipeline, one object at a time is read and processed.
So to sum it up in pseudo code, you could do
Slow and memory intensive:
ForEach ($v in $Whatever) {  ## Statement! PS 3.0 or later required for -Append
	Get-Whatever $_ | Export-Csv C:\Temp\foo.csv -Append -NoTypeInformation
}

Open in new window

Using a Subexpression, memory intensive:
$(
	ForEach ($v in $Whatever) {  ## Statement inside a subexpression!
		Get-Whatever $_
	}
) | Export-Csv C:\Temp\foo.csv -NoTypeInformation

Open in new window

"The PowerShell Way":
$Whatever | ForEach-Object {  ## Cmdlet
	Get-Whatever $_
} | Export-Csv C:\Temp\foo.csv -NoTypeInformation

Open in new window

The last version would work with ForEach instead of ForEach-Object as well, and it would use the alias, so the ForEach-Object cmdlet.
But note that "best PS practice" dictates that aliases should only be used interactively, when you want to save keystrokes. Inside a script, you should always use the full cmdlet names.
Reason: an alias can be redefined; you don't know if that might have happened if you run the script on a different machine or with a different user.
Avatar of SAM IT

ASKER

Thanks jose.

Thanks a lot OBDA for better explaination to my question.