Solved

Export two Powershell commands output merged into Excel formatted?

Posted on 2014-07-22
10
1,177 Views
Last Modified: 2014-07-22
I'm trying to figure out how I can output results from Powershell into Excel file based on the following.

I have a script below that reads Exchange alises from a txt file then loops through them and runs a command

$path = "C:\list.txt"
$thelist = @(Get-Content $path)
Foreach ($name in $thelist)
{
get-mailbox -identity $name | select Alias,ForwardingAddress
get-mailboxpermission -identity $name | select User | Sort User
}

Open in new window


The 1st command's output would output onto a row a column with the user's Alias, and then a column with the user's ForwardingAddress if one is present.

The 2nd command (getting mailbox permissions), won't run unless I comment out the first command for some reason.

IS there a way to get both cmdlets to be able to run for each user I have imported from the txt list, and then merge the output of both into the same file under their respective columns?

So that'd I'd have an output like:
Alias                               ForwardingAddress                           Permissions
------                               ---------------------------                           -------------------
bobjones                                                                                   Sally Reynolds
                                                                                                   John Smith
sarajSmith                 forwardedtobob@domain.com            Mike Rodgers
0
Comment
Question by:garryshape
  • 7
  • 3
10 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
The below should work.  It makes use of what is known as a calculated property.
$path = "C:\list.txt"
@(Get-Content $path) | ForEach `
{
    $name = $_
    Get-Mailbox -identity $name | select Alias,ForwardingAddress,
                                         @{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User | Sort) -join "`n"}}
} | Export-CSV output.csv -notype

Open in new window

0
 

Author Comment

by:garryshape
Comment Utility
@footech

Thanks for reply.

It appears the Alias and Forwarding column populates correctly, but under the "Permissions" column it just has the text "NT" in each cell.
I may have confused but what I mean is that in the "Permissions" collumn, I'm trying to get a list of those users which have mailbox permissions to the alias (identity)
0
 

Author Comment

by:garryshape
Comment Utility
lmao sorry
it was an user Excel issue I just had to expand the cell
0
 

Author Comment

by:garryshape
Comment Utility
Thanks that really helps!!!! Definitely
0
 

Author Comment

by:garryshape
Comment Utility
I added an 'r before 'n and it does the carriage return so it's perfect now for me
Thanks again!

Get-Mailbox -identity $name | select Alias,ForwardingAddress,
                                         @{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User | Sort) -join "'r`n"}}
} | Export-CSV output.csv -notype
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:garryshape
Comment Utility
Not sure if I should make a new request but,
do you know how I could omit/filter out repeating users from the "Permissions" column?
So if "Domain\Bob" is shown all throughout every mailbox in the permissions column, is there a way to delete it out via Powershell's query/output process? Or should I just do a string replacement trick in Excel?
0
 

Author Comment

by:garryshape
Comment Utility
Yeah it looks like for the Permissions column, it's putting it out as one long string in quotations, with carriage returns in between I think
0
 
LVL 39

Assisted Solution

by:footech
footech earned 500 total points
Comment Utility
You can use the -Unique parameter with Select-Object.
$path = "C:\list.txt"
@(Get-Content $path) | ForEach `
{
    $name = $_
    Get-Mailbox -identity $name | select Alias,ForwardingAddress,
                                         @{n="Permissions";e={(Get-MailboxPermission -identity $name | Select -expand User -Unique | Sort) -join "`r`n"}}
} | Export-CSV output.csv -notype

Open in new window

0
 
LVL 39

Assisted Solution

by:footech
footech earned 500 total points
Comment Utility
In order to output it to a .CSV, it really can't be anything other than a string.  That's why I had to use the -join operator to combine all the elements into one string.
0
 

Author Closing Comment

by:garryshape
Comment Utility
awesome! thanks so much !
0

Featured Post

Want to promote your upcoming event?

Is your company attending an event or exhibiting at a trade show soon? Are you speaking at a conference? Spread the word by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

Join & Write a Comment

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
how to add IIS SMTP to handle application/Scanner relays into office 365.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

10 Experts available now in Live!

Get 1:1 Help Now