Solved

powershell export-csv

Posted on 2011-03-11
8
1,330 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
Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

 

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

624 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