none
Formatting output from PowerShell RRS feed

  • Question

  • Hello All

    I'm not expecting someone to write this for me but would really appreciate some pointers on how I should perhaps be doing this or where I should be looking for guidance.

    I have a simple PowerShell script using a loop that retrieves the permission for Default and Anonymous on an Exchange folder for each user. I'd like to output it, initially to the console, in a neat table something like this :

    Alias                 Default Permissions    Anonymous Permissions
    -----                  -----------------------    -----------------------------
    MarcoPolo         {Reviewer}                {None}
    DonaldTrump     {None}                     {AvailabilityOnly}

    TIA

    Andrew


    Il y a 10 sortes de personnes. Celles qui comprennent le binaire et celles qui ne le comprennent pas.

    Friday, March 10, 2017 9:11 AM

Answers

  • Nice try but here is a PowerShell way to do this more easily:

    Get-Mailbox -RecipientTypeDetails UserMailbox |
    	ForEach-Object{
    		$cal = $_.alias + ':\Calendar'
    		[pscustomobject]@{
    			Name = $_.Name
    			DefPerms = (Get-MailboxFolderPermission $cal -U Default).AccessRights -join '.'
    			AnonPerms = (Get-MailboxFolderPermission $cal -U Anonymous).AccessRights -join '.'
    		}
    	}
    
    


    \_(ツ)_/

    Friday, March 10, 2017 3:31 PM

All replies

  • You forgot to post your script.  ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Friday, March 10, 2017 9:48 AM
  • I didn't forget, I was just looking for pointers but if someone wants to modify the script I'd still like an explanation on how it's achieved please.

    foreach($user in Get-Mailbox -RecipientTypeDetails UserMailbox) {

    $cal = $user.alias+":\Calendar"

    Get-MailboxFolderPermission -Identity $cal -User Default
    Get-MailboxFolderPermission -Identity $cal -User Anonymous


    Il y a 10 sortes de personnes. Celles qui comprennent le binaire et celles qui ne le comprennent pas.

    Friday, March 10, 2017 9:58 AM
  • Use a custom object to collect the data.


    \_(ツ)_/

    Friday, March 10, 2017 10:09 AM
  • I'm ashamed to say this has taken me most of the day and I'll need to have a lie down now but it seems to work.

    I'm also pretty sure I've butchered it together so if anyone wants to critique and point out any drastic bodges I'd appreciate it.

    $boxCalPerms = @()
    foreach($user in Get-Mailbox -RecipientTypeDetails UserMailbox)
      {
        $cal = $user.alias+":\Calendar"
        $boxCalPerm = New-Object System.Object
        $boxCalPerm | Add-Member -type NoteProperty -name Name -value $user.Name
        $boxCalPerm | Add-Member -type NoteProperty -name DefPerms -value (Get-MailboxFolderPermission -Identity $cal -User Default | format-list AccessRights | Out-String).Replace("AccessRights : ", "").Split('{}')[1]
        $boxCalPerm | Add-Member -type NoteProperty -name AnonPerms -value (Get-MailboxFolderPermission -Identity $cal -User Anonymous | format-list AccessRights | Out-String).Replace("AccessRights : ", "").Split('{}')[1]
        $boxCalPerms += $boxCalPerm
      }
    $boxCalPerms



    Il y a 10 sortes de personnes. Celles qui comprennent le binaire et celles qui ne le comprennent pas.

    Friday, March 10, 2017 3:17 PM
  • Why not take advantage of the pipeline:


    Get-Mailbox -RecipientTypeDetails UserMailbox | ForEach-Object { [PSCustomObject] @{ "Name" = $_.Name "DefPerms" = whatever "AnonPerms" = whatever } }



    -- Bill Stewart [Bill_Stewart]

    Friday, March 10, 2017 3:24 PM
    Moderator
  • Nice try but here is a PowerShell way to do this more easily:

    Get-Mailbox -RecipientTypeDetails UserMailbox |
    	ForEach-Object{
    		$cal = $_.alias + ':\Calendar'
    		[pscustomobject]@{
    			Name = $_.Name
    			DefPerms = (Get-MailboxFolderPermission $cal -U Default).AccessRights -join '.'
    			AnonPerms = (Get-MailboxFolderPermission $cal -U Anonymous).AccessRights -join '.'
    		}
    	}
    
    


    \_(ツ)_/

    Friday, March 10, 2017 3:31 PM
  • Nice try

    I think you're being generous with the word "Nice" but I appreciate the pointer and happy that I got to working version and learnt some stuff along the way.

    Your version is great and I'll be using it though the output needs to be wider but that's for another day.

    Thanks for all the replies.


    Il y a 10 sortes de personnes. Celles qui comprennent le binaire et celles qui ne le comprennent pas.

    Friday, March 10, 2017 3:50 PM
  • Once you sit down and actually learn PowerShell you will learn about the formatting commands and how to get the output you want.


    \_(ツ)_/

    Friday, March 10, 2017 6:30 PM