?
Solved

Export two Powershell commands output merged into Excel formatted?

Posted on 2014-07-22
10
Medium Priority
?
1,343 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
[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
  • 7
  • 3
10 Comments
 
LVL 40

Accepted Solution

by:
footech earned 2000 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

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
 

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 40

Assisted Solution

by:footech
footech earned 2000 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 40

Assisted Solution

by:footech
footech earned 2000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
Check out this step-by-step guide for using the newly updated Experts Exchange mobile app—released on May 30.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…
Suggested Courses

771 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