Solved

Export two Powershell commands output merged into Excel formatted?

Posted on 2014-07-22
10
1,224 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

803 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