Solved

powershell export-csv

Posted on 2011-03-11
8
1,282 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
  • 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now