Solved

Export two Powershell commands output merged into Excel formatted?

Posted on 2014-07-22
10
1,193 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
ID: 40212543
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
ID: 40212562
@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
ID: 40212587
lmao sorry
it was an user Excel issue I just had to expand the cell
0
 

Author Comment

by:garryshape
ID: 40212596
Thanks that really helps!!!! Definitely
0
 

Author Comment

by:garryshape
ID: 40212616
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:garryshape
ID: 40212630
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
ID: 40212651
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
ID: 40212657
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
ID: 40212666
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
ID: 40212690
awesome! thanks so much !
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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 …
Find out what you should include to make the best professional email signature for your organization.
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

930 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

9 Experts available now in Live!

Get 1:1 Help Now