Solved

powershell export-csv

Posted on 2011-03-11
8
1,320 Views
Last Modified: 2012-05-11
Hello,

I am trying to put a bunch of user into csv and send it via email, but It only adds me one user into it, I am not sure why is it caused, any idea?

Many thanks
Get-QADUser -Enabled -SearchRoot 'xxx'| 
 %{  if (($_.manager -eq $null) -or ($_.accountexpires -eq $null)) `
 {
 
  $_ |  select name,samaccountname,@{N='Manager';E={$_.manager}  },@{N='AccountExpires';E={($_.accountexpires)}} | 
#out-file -filepath C:\subcontractorsExport.txt -encoding ASCII 
 Export-Csv -Path C:\subcontractorsExport.csv -Delimiter ";"
}
}
#Send-MailMessage -From "xx" -To "xx" -Smtpserver "server" -Subject "Export of Subcontractors" -Body "Export of subcontractors without manager or accountexpires" -Attachments "C:\subcontractorsExport.csv"

Open in new window

0
Comment
Question by:Radim88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35106543
You shoul put the export outside of the ForEach.
And I do not really understand why do you need that select-object?
My solution is attached.
Get-QADUser -SearchRoot 'xxx' -enabled -LdapFilter '(!(&(manager=*)(accountExpires=9223372036854775807)))' | Export-Csv -Path C:\subcontractorsExport.csv -Delimiter ";"

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 35106553
And maybe you might want to add the -notypeinformation switch to the export.
Get-QADUser -enabled -LdapFilter '(!(&(manager=*)(accountExpires=9223372036854775807)))' | Export-Csv -NoTypeInformation -Path C:\subcontractorsExport.csv -Delimiter ";"

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 35106592
Sorry, this is the right solution:
Get-QADUser -SearchRoot 'xxx' -enabled -LdapFilter '(!(&(manager=*)(accountExpires=9223372036854775807)))' | 
select-object name, samaccountname, manager, accountexpires | Export-Csv -NoTypeInformation -Path C:\subcontractorsExport.csv -Delimiter ";"

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Radim88
ID: 35106593
Hi,
It should be something like this, strange that out-file works.

Get-QADUser -Enabled -SearchRoot 'xx'|  %{          if (($_.manager -eq $null) -or ($_.accountexpires -eq $null)) `
 {
 
  $_ |  select name,samaccountname,@{N='Manager';E={ if($_.manager -eq $null){write 'empty'}else{$_.manager | get-qaduser | select -ExpandProperty name }}  },@{N='AccountExpires';E={((get-date ($_.accountexpires)).adddays(-1)).Tostring(“dd.MM.yyyy”) }} | Export-Csv -Path C:\subcontractorsExport.csv -Delimiter ";"
#out-file -filepath C:\subcontractorsExport.txt -encoding ASCII
# Export-Csv -Path C:\subcontractorsExport.csv -Delimiter ";"
}
}
0
 
LVL 13

Accepted Solution

by:
soostibi earned 125 total points
ID: 35107501
Put your export at the very end!

Get-QADUser -Enabled -SearchRoot 'xx'|  %{          if (($_.manager -eq $null) -or ($_.accountexpires -eq $null)) `
 {
 
  $_ |  select name,samaccountname,@{N='Manager';E={ if($_.manager -eq $null){write 'empty'}else{$_.manager | get-qaduser | select -ExpandProperty name }}  },@{N='AccountExpires';E={((get-date ($_.accountexpires)).adddays(-1)).Tostring(“dd.MM.yyyy”) }} | Export-Csv -Path C:\subcontractorsExport.csv -Delimiter ";"
#out-file -filepath C:\subcontractorsExport.txt -encoding ASCII
# }
} | Export-Csv -Path C:\subcontractorsExport.csv -Delimiter ";"

Out-File works as it appends lines to the file, but the export-csv rewrites.

And consider replacing your first line to:
Get-QADUser -SearchRoot 'xxx' -enabled -LdapFilter '(!(&(manager=*)(accountExpires=9223372036854775807)))'

It's much more efficient.
0
 

Author Comment

by:Radim88
ID: 35107574
Thanks thats it:-), and whatabout that -ldapfilter you are mentioning. Its the same like that mine if:?
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35107585
Yes, it should be the same, but it filter runs on the DC side, not in the PowerShell process.
0
 

Author Closing Comment

by:Radim88
ID: 35107609
thx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question