Solved

Export two Powershell commands output merged into Excel formatted?

Posted on 2014-07-22
10
1,278 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 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

738 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