not able to export result in csv - powershell

$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
Get-Mailbox -Identity $b | Select-Object name, Database
}
$b | Export-Csv C:\xyz\result.csv 

Open in new window


It gives output as ""PSPath","PSParentPath","PSChildName","PSDrive","PSProvider","ReadCount","Length.. and so on"
jack jonesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
You're exporting b, which comes from your file. It's breaking the interpreter a bit.
Get-Content C:\xyz\test.txt | ForEach-Object {
    Get-Mailbox -Identity $_ | Select-Object name, Database
} | Export-Csv C:\xyz\result.csv 

Open in new window

0
jack jonesAuthor Commented:
Supply values for the following parameters:
Process[0]:
Anyway you could modify my script not able to get yours..
0
Chris DentPowerShell DeveloperCommented:
$results = @()
$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
    $results += Get-Mailbox -Identity $b | Select-Object name, Database
}
$results | Export-Csv C:\xyz\result.csv

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jack jonesAuthor Commented:
now the script is giving error operation could not be performed
0
Chris DentPowerShell DeveloperCommented:
You're going to have to share a bit more detail because the errors you're getting and the code I'm posting are not joining up.

What, exactly, does the error message say and where is it being thrown?
0
jack jonesAuthor Commented:
what my script does is it gets all user from list and find their mailboxes and shows results.
I want this result in csv
0
Chris DentPowerShell DeveloperCommented:
I'm quite capable of reading the script and determining its intent. I've given you two versions which will achieve that result. The scripts aren't long, there's not a huge margin for error here.

I'm unclear how you're managing to end up with errors. You'll have to share more if I'm to help further.
0
jack jonesAuthor Commented:
$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
Get-Mailbox -Identity $b | ft name, Database
}
$b | Export-Csv C:\xyz\result.csv 

Open in new window


I have modified my script it gives output in the console however it generates a csv file with no output in it
0
Chris DentPowerShell DeveloperCommented:
That's because "$b" is a single entry (the last entry) from Get-Content. It is *not* the result of the Get-Mailbox command. You'll get lots to the console and nothing of note to the file.

If you want to export, you either need:

To send the output you're generating for the console to a file (via Export-Csv)
Get-Content C:\xyz\test.txt | ForEach-Object {
    Get-Mailbox -Identity $_ | Select-Object name, Database
} | Export-Csv C:\xyz\result.csv

Open in new window


Or:

To declare, then add results to an array. The array is then sent to Export-Csv:
$results = @()
$a = Get-Content C:\xyz\test.txt
foreach($b in $a)
{
    $results += Get-Mailbox -Identity $b | Select-Object name, Database
}
$results | Export-Csv C:\xyz\result.csv

Open in new window

These samples should work if you use them exactly as they are. If you start messing around with the position of opening braces (as in the first example), you'll get prompted for the thing that was missing (the process block) because commands don't understand brace style choices.

If you cannot build the examples yourself, don't tweak them except for the obvious file names. If you cannot work with the error messages yourself, include them in the thread exactly as they are, don't paraphrase or give your own partial interpretation.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jack jonesAuthor Commented:
Im getting this ou put in csv
#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
"ClassId2eaqwere99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"

May be some with exchange expertise can solve this ...
0
Chris DentPowerShell DeveloperCommented:
You've added format-list or format-table...
0
jack jonesAuthor Commented:
format-table ft ... i tried both but none succeed
0
Chris DentPowerShell DeveloperCommented:
That's why its broken now. You cannot send output created by the format commands to export-csv.
0
jack jonesAuthor Commented:
Yes thats what i m looking to solve.
0
Chris DentPowerShell DeveloperCommented:
You showed code with a bug caused by you passing the wrong value to Export-Csv.

I gave you two solutions which deal with that.

At no point have I used Format-*, so why are you?

I cannot help you, you change the things you're given, then complain they don't work.

Good luck.
0
jack jonesAuthor Commented:
Both didnt work :(
0
Jeff GloverSr. Systems AdministratorCommented:
I hate to ask this but are you running this on an Exchange server or at least opening a  remote powershell session to one? I.E. loading the Exchange modules? I only ask this because, out of curiosity, I tried both of the scripts that Chris posted and they run like a champ ). As long as your text file is just  a list of users, you have valid paths, and have the Exchange module loaded in powershell these run perfectly.
0
jack jonesAuthor Commented:
Yes i have loaded an exchange module in powershell ..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.