SOLVED

Exchange Online PowerShell - Invoke-Command with Select-Object inside scriptblock

%3CLINGO-SUB%20id%3D%22lingo-sub-333802%22%20slang%3D%22en-US%22%3EExchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333802%22%20slang%3D%22en-US%22%3E%3CP%3E%3CEM%3EUpdate%3A%20I'm%20all%20good%20now%3A).%26nbsp%3B%20Had%20to%20up-to-snuff-ize%20my%20PowerShell%20remoting%20understanding.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20get%20help%20with%20something.%26nbsp%3B%20I've%20read%20this%3A%3CA%20title%3D%22You%20Had%20Me%20at%20EHLO%3A%20Running%20PowerShell%20cmdlets%20for%20large%20numbers%20of%20users%20in%20Office%20365%22%20href%3D%22https%3A%2F%2Fblogs.technet.microsoft.com%2Fexchange%2F2015%2F11%2F02%2Frunning-powershell-cmdlets-for-large-numbers-of-users-in-office-365%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ERunning%20PowerShell%20cmdlets%20for%20large%20numbers%20of%20users%20in%20Office%20365%3C%2FA%3E%26nbsp%3B...%3C%2FP%3E%3CP%3ESome%20things%20to%20note%3A%3C%2FP%3E%3CUL%3E%3CLI%3Ethe%20comments%20are%20closed%20there...%3C%2FLI%3E%3CLI%3E(I%20believe%20%3CSTRONG%3E%3CEM%3E%26lt%3B--%3A%20no%20longer%3A)%3C%2FEM%3E%3C%2FSTRONG%3E)%20the%20article%20demonstrates%20impossible%20actions.%3C%2FLI%3E%3C%2FUL%3E%3CP%3EVery%20simply%3A%20The%20%3CEM%3ESelect-Object%3C%2FEM%3E%26nbsp%3Bcmdlet%20is%20not%20allowed%20to%20be%20used%20inside%20your%20Invoke-Command%20script%20block%20when%20you're%20connected%20to%20Exchange%20Online.%26nbsp%3B%20Only%20exported%20commands%20from%20the%20temporary%20module%20are%20allowed.%26nbsp%3B%20So%20that%20entire%20article%20and%20the%20StartRobustCloudCommand%20script%20completely%20miss%20the%20%22Data%20Return%22%20issue%20that%20that%20post%20is%20supposedly%20addressing.%20(%3CSTRONG%3E%3CEM%3E...or%3A%3C%2FEM%3E%3C%2FSTRONG%3E)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAm%20I%20missing%20something%3F%20(%3CSTRONG%3E%3CEM%3E%26lt%3B--%3B%20Yup%2C%20I%20typed%20'select'%2C%20not%20'Select-Object'.%3C%2FEM%3E%3C%2FSTRONG%3E)%26nbsp%3B%20Here's%20a%20simple%20example%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%26gt%3BInvoke-Command%20-Session%20(Get-PSSession)%20-ScriptBlock%20%7BGet-MailboxPermission%20gray%20%7Cselect%20Identity%7D%3CBR%20%2F%3EThe%20term%20'select'%20is%20not%20recognized%20as%20the%20name%20of%20a%20cmdlet%2C%20function%2C%20script%20file%2C%20or%20operable%3CBR%20%2F%3Eprogram.%20Check%20the%20spelling%20of%20the%20name%2C%20or%20if%20a%20path%20was%20included%2C%20verify%20that%20the%20path%20is%3CBR%20%2F%3Ecorrect%20and%20try%20again.%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20ObjectNotFound%3A%20(select%3AString)%20%5B%5D%2C%20CommandNotFoundException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20CommandNotFoundException%3CBR%20%2F%3E%2B%20PSComputerName%20%3A%20outlook.office365.com%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-333802%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHybrid%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-360435%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-360435%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20wanted%20to%20touch%20back%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3Bto%20say%20thanks%20for%20your%20help.%26nbsp%3B%20I've%20been%20busy%20since%20the%20day%20I%20opened%20this%20post%2C%20figuring%20our%20how%20to%20use%20Invoke-Command%20both%20with%20%24args%20and%20%24Using%20(cosmetically%2C%20I%20found%20use%20cases%20for%20using%20both).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20updated%20my%20mailbox%20delegations%20script%20so%20that%20it's%20almost%20exclusively%20using%20Invoke-Command%2C%20in%20case%20it's%20of%20any%20value%20for%20you%20or%20anyone%20else%20who%20stumbles%20onto%20this%20thread%3A%3C%2FP%3E%3CP%3E%3CA%20title%3D%22https%3A%2F%2Fgithub.com%2FJeremyTBradshaw%2FPowerShell%2Fblob%2Fmaster%2FGet-MailboxTrustee.ps1%22%20href%3D%22https%3A%2F%2Fgithub.com%2FJeremyTBradshaw%2FPowerShell%2Fblob%2Fmaster%2FGet-MailboxTrustee.ps1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGet-MailboxTrustee.ps1%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20have%20a%20lot%20of%20mailboxes%20to%20query%2C%20you%20may%20find%20the%20script's%20Write-Progress%20bars%20entertaining%20if%20nothing%20else.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-334493%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-334493%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20always%20filter%20the%20output%20client%20side.%20Or%20for%20the%20case%20of%20Get-MailboxPermission%2C%20use%20the%20-User%20parameter.%20The%20%24using%20PowerShell%20modifier%20is%20a%20great%20way%20to%20pass%20variables%20inside%20remote%20sessions%2C%20I%20have%20few%20examples%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fwww.michev.info%2FBlog%2FPost%2F1687%2Fusing-variables-with-invoke-command-in-remote-powershell-sessions-in-exchange-online%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.michev.info%2FBlog%2FPost%2F1687%2Fusing-variables-with-invoke-command-in-remote-powershell-sessions-in-exchange-online%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-334252%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-334252%22%20slang%3D%22en-US%22%3E%3CP%3E..Actually%20-%20nevermind%2C%20I%20get%20it%20now.%26nbsp%3B%20Only%20the%20commands%20that%20are%20available%20in%20the%20temporarily%20module%20are%20available%2C%20which%20includes%20Select-Object%2C%20but%20not%20Where-Object%2C%20and%20NoLanguage%20mode%20means%20no%20custom%20properties%2C%20etc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20means%2C%20Get-MailboxPermission%20using%20Invoke-Command%20is%20not%20a%20good%20plan%20since%20I'd%20have%20to%20pull%20back%20all%20permissions%20this%20way%2C%20then%20sort%20through%20them%20and%20filter%20just%20the%20FullAccess%20ones%20on%20my%20side.%26nbsp%3B%20(%3CEM%3E%3CSTRONG%3E%26lt%3B--%3A%20I'm%20completely%20wrong%20here.%26nbsp%3B%20Worst-case%2C%20we%20can%20make%20two%20passes%20with%20Get-MailboxPermission%2FGet-ADPermission%2FGet-RecipientPermission.%26nbsp%3B%20Once%20to%20get%20the%20index%20of%20all%20ACE's%20in%20the%20ACL%20with%20matching%20AccessRights%2FInheritance%2FAllow%7CDeny%20criteria%2C%20then%20a%20second%20time%20to%20expand%20just%20the%20users%20for%20those%20ACE's.%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3E%3CSTRONG%3ENot%20bad%20at%20all!%3Aok_hand%3A%3C%2Fimg%3E%26nbsp%3B%20And%20no%20reason%20Invoke-Command%20would%20not%20be%20a%20good%20plan%20after%20all.%3C%2FSTRONG%3E%3C%2FEM%3E%3Athumbs_up%3A%3C%2Fimg%3E)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3ERemaining%20comments%20removed%3A%20no%20longer%20relevant.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-334216%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-334216%22%20slang%3D%22en-US%22%3E%3CP%3EWell%20now%2C%20I'm%20back%20with%20a%20follow-on%20question.%26nbsp%3B%20Having%20now%20used%20the%20proper%20cmdlet%20name%20Select-Object%20I%20notice%20I%20can't%20create%20custom%20properties.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWorks%20in%20Exchange%20on-premises%20PS%20remoting%3A%3C%2FP%3E%3CPRE%3E%26gt%3BInvoke-Command%20-Session%20%24exPSSession%20-ScriptBlock%20%7BGet-MailboxPermission%20Gray%20%7C%20Select-Object%20%40%7Bn%3D't'%3Be%3D%7B%24_.User.SecurityIdentifier%7D%7D%7D%3C%2FPRE%3E%3CP%3EBut%20doesn't%20work%20in%20Exchange%20Online%20PS%20remoting%3A%3C%2FP%3E%3CPRE%3E%26gt%3BInvoke-Command%20-Session%20%24exops%20-ScriptBlock%20%7BGet-MailboxPermission%20-Identity%20Ross%20%7C%20Select-Object%20-Property%20%40%7BName%3D'MbxObjectGuid'%3BExpression%3D%7B%24_.Identity.ObjectGuid%7D%7D%7D%3CBR%20%2F%3EThe%20syntax%20is%20not%20supported%20by%20this%20runspace.%20This%20can%20occur%20if%20the%20runspace%20is%20in%20no-language%3CBR%20%2F%3Emode.%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20ParserError%3A%20(Get-MailboxPerm...ty.ObjectGuid%7D%7D%3AString)%20%5B%5D%2C%20ParseExc%3CBR%20%2F%3Eeption%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ScriptsNotAllowed%3CBR%20%2F%3E%2B%20PSComputerName%20%3A%20outlook.office365.com%26nbsp%3B%3C%2FPRE%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3Bdo%20you%20know%20if%20this%20means%20I'd%20have%20to%20run%20Get-MailboxPermission%20multiple%20times%20in%20order%20to%20then%20do%20Select-Object%20-ExpandProperty%20against%20each%20property%20I'm%20trying%20to%20expand%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-333922%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333922%22%20slang%3D%22en-US%22%3EThanks%20very%20much%20-%20I%20was%20stuck%20in%20my%20own%20way%20with%20that%20one!%3CBR%20%2F%3E%3CBR%20%2F%3ETo%20confirm%20about%20the%20user%20being%20returned%20as%20UPN.%20What%20happens%20when%20the%20trustee%20is%20a%20group%3F%3CBR%20%2F%3E%3CBR%20%2F%3EI%20was%20%2F%20am%20hoping%20to%20avoid%20needing%20to%20do%20any%20further%20lookups%20by%20using%20those%20nested%20properties%20that%20would%20be%20included%20otherwise%20(e.g.%20with%20the%20snapin).%3CBR%20%2F%3E%3CBR%20%2F%3EBut%20I%20can%20do%20select-object%20and%20work%20with%20that!%20Im%20happy%20to%20have%20a%20solution%20on%20day%201%20of%20my%20post.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-333900%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333900%22%20slang%3D%22en-US%22%3E%3CP%3EFew%20points.%20First%2C%20Select-Object%20and%20select%20are%20two%20different%20things%2C%20because%20of%20the%20NoLanguage%20mode%2C%20as%20in%20-%20no%20aliases%20are%20allowed%20either.%20So%20you%20need%20to%20write%20it%20%22properly%22%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%23%20Invoke-Command%20-Session%20(Get-PSSession%20-id%2019)%20%7B%20Get-Mailbox%20vasil%20%7C%20select%20PrimarySmtpAddress%20%7D%0AThe%20term%20'select'%20is%20not%20recognized%20as%20the%20name%20of%20a%20cmdlet%2C%20function%2C%20script%20file%2C%20or%20operable%20program.%20Check%20the%20spelling%20of%20the%20name%2C%20or%20if%20a%20path%20was%20included%2C%20verify%20that%20the%20path%20is%20correct%0Aand%20try%20again.%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20ObjectNotFound%3A%20(select%3AString)%20%5B%5D%2C%20CommandNotFoundException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20CommandNotFoundException%0A%20%20%20%20%2B%20PSComputerName%20%20%20%20%20%20%20%20%3A%20outlook.office365.com%0A%0A%23%20Invoke-Command%20-Session%20(Get-PSSession%20-id%2019)%20%7B%20Get-Mailbox%20vasil%20%7C%20Select-Object%20PrimarySmtpAddress%20%7D%0A%0APrimarySmtpAddress%20PSComputerName%20%20%20%20%20%20%20%20RunspaceId%0A------------------%20--------------%20%20%20%20%20%20%20%20----------%0Avasil%40michev.info%20%20outlook.office365.com%2058472f81-7f34-4e40-8ea7-486a0be474e7%3C%2FPRE%3E%0A%3CP%3ENext%2C%20Get-MailboxPermission%20in%20ExO%20returns%20the%20UPN%20of%20the%20user%2C%20which%20is%20%22unique%20enough%22%20and%20you%20can%20get%20the%20GUID%20by%20querying%20based%20on%20the%20UPN%20if%20needed.%20I'd%20agree%20the%20full%20object%20would%20be%20better.%20It%20does%20return%20the%20SID%20though%3A%3C%2FP%3E%0A%3CPRE%3E%23%20Invoke-Command%20-Session%20(Get-PSSession%20-id%2019)%20%7B%20Get-MailboxPermission%20sharednew%20-User%20vasil%20%7C%20Select-Object%20-ExpandProperty%20User%20%7D%20-HideComputerName%0A%0A%0ARunspaceId%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%2058472f81-7f34-4e40-8ea7-486a0be474e7%0ASecurityIdentifier%20%20%20%20%20%20%20%20%20%20%3A%20S-1-5-21-3675944716-2045640655-299186705-3762784%0AReturnUrlTokenEncodedString%20%3A%20False%0ARawIdentity%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20vasil%40michev.info%3C%2FPRE%3E%0A%3CP%3EYou%20can%20apply%20the%20same%20method%20to%20get%20the%20ObjectGUID%20for%20the%20Identity%20value.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20mailbox%20folder%20permission%20entries%20returning%20a%20full%20ADRecipient%20object%20is%20something%26nbsp%3BI%20probed%20the%20PG%20few%20years%20back%2C%20never%20got%20a%20reply%20whether%20it's%20intentional%20of%20bug.%20Most%20of%20the%20time%20you%20can%20get%20enough%20properties%20via%20Invoke-Command%20though.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-333828%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333828%22%20slang%3D%22en-US%22%3E%3CP%3ELast%20point.%26nbsp%3B%20I'm%20not%20sure%20why%20but%20objects%20returned%20from%20Get-MailboxFolderPermission%20are%20different.%26nbsp%3B%20The%20User%20property%20is%20more%20than%20just%20a%20string%3C%2FP%3E%3CP%3E%3CEM%3E(%20I%20think%20I%20can%20guess%20why%20on%20this%20one%20now%3A%20it%20might%20be%20because%20there%20are%20usually%20very%20many%20folder%20permissions%2C%20so%20exposing%20.ADRecipient%20is%20a%20smart%20move%20to%20avert%20further%20lookups.)%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EGet-MailboxFolderPermission%20%26lt%3BUser%26gt%3B%3A%5C%26lt%3BFolder%26gt%3B%20%7C%20select%20%40%7Bn%3D'TrusteeExchangeGuid'%3Be%3D%7B%24_.User.ADRecipient.ExchangeGuid%7D%7D%3C%2FPRE%3E%3CP%3E%3CEM%3ERemoved%20comment%20-%20no%20longer%20relevant.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-333804%22%20slang%3D%22en-US%22%3ERe%3A%20Exchange%20Online%20PowerShell%20-%20Invoke-Command%20with%20Select-Object%20inside%20scriptblock%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333804%22%20slang%3D%22en-US%22%3E%3CP%3E%3CEM%3ERemoved%20%2F%20no%20longer%20relevant.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

Update: I'm all good now:).  Had to up-to-snuff-ize my PowerShell remoting understanding.

 

Hello,

 

I'm trying to get help with something.  I've read this:Running PowerShell cmdlets for large numbers of users in Office 365 ...

Some things to note:

  • the comments are closed there...
  • (I believe <--: no longer:)) the article demonstrates impossible actions.

Very simply: The Select-Object cmdlet is not allowed to be used inside your Invoke-Command script block when you're connected to Exchange Online.  Only exported commands from the temporary module are allowed.  So that entire article and the StartRobustCloudCommand script completely miss the "Data Return" issue that that post is supposedly addressing. (...or:)

 

Am I missing something? (<--; Yup, I typed 'select', not 'Select-Object'.)  Here's a simple example:

 

>Invoke-Command -Session (Get-PSSession) -ScriptBlock {Get-MailboxPermission gray |select Identity}
The term 'select' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
+ CategoryInfo : ObjectNotFound: (select:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : outlook.office365.com

 

8 Replies
Highlighted

Removed / no longer relevant.

Highlighted

Last point.  I'm not sure why but objects returned from Get-MailboxFolderPermission are different.  The User property is more than just a string

( I think I can guess why on this one now: it might be because there are usually very many folder permissions, so exposing .ADRecipient is a smart move to avert further lookups.) 

 

Get-MailboxFolderPermission <User>:\<Folder> | select @{n='TrusteeExchangeGuid';e={$_.User.ADRecipient.ExchangeGuid}}

Removed comment - no longer relevant.

Highlighted
Solution

Few points. First, Select-Object and select are two different things, because of the NoLanguage mode, as in - no aliases are allowed either. So you need to write it "properly":

 

# Invoke-Command -Session (Get-PSSession -id 19) { Get-Mailbox vasil | select PrimarySmtpAddress }
The term 'select' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
    + CategoryInfo          : ObjectNotFound: (select:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : outlook.office365.com

# Invoke-Command -Session (Get-PSSession -id 19) { Get-Mailbox vasil | Select-Object PrimarySmtpAddress }

PrimarySmtpAddress PSComputerName        RunspaceId
------------------ --------------        ----------
vasil@michev.info  outlook.office365.com 58472f81-7f34-4e40-8ea7-486a0be474e7

Next, Get-MailboxPermission in ExO returns the UPN of the user, which is "unique enough" and you can get the GUID by querying based on the UPN if needed. I'd agree the full object would be better. It does return the SID though:

# Invoke-Command -Session (Get-PSSession -id 19) { Get-MailboxPermission sharednew -User vasil | Select-Object -ExpandProperty User } -HideComputerName


RunspaceId                  : 58472f81-7f34-4e40-8ea7-486a0be474e7
SecurityIdentifier          : S-1-5-21-3675944716-2045640655-299186705-3762784
ReturnUrlTokenEncodedString : False
RawIdentity                 : vasil@michev.info

You can apply the same method to get the ObjectGUID for the Identity value.

 

The mailbox folder permission entries returning a full ADRecipient object is something I probed the PG few years back, never got a reply whether it's intentional of bug. Most of the time you can get enough properties via Invoke-Command though.

Highlighted
Thanks very much - I was stuck in my own way with that one!

To confirm about the user being returned as UPN. What happens when the trustee is a group?

I was / am hoping to avoid needing to do any further lookups by using those nested properties that would be included otherwise (e.g. with the snapin).

But I can do select-object and work with that! Im happy to have a solution on day 1 of my post.
Highlighted

Well now, I'm back with a follow-on question.  Having now used the proper cmdlet name Select-Object I notice I can't create custom properties.

 

Works in Exchange on-premises PS remoting:

>Invoke-Command -Session $exPSSession -ScriptBlock {Get-MailboxPermission Gray | Select-Object @{n='t';e={$_.User.SecurityIdentifier}}}

But doesn't work in Exchange Online PS remoting:

>Invoke-Command -Session $exops -ScriptBlock {Get-MailboxPermission -Identity Ross | Select-Object -Property @{Name='MbxObjectGuid';Expression={$_.Identity.ObjectGuid}}}
The syntax is not supported by this runspace. This can occur if the runspace is in no-language
mode.
+ CategoryInfo : ParserError: (Get-MailboxPerm...ty.ObjectGuid}}:String) [], ParseExc
eption
+ FullyQualifiedErrorId : ScriptsNotAllowed
+ PSComputerName : outlook.office365.com 

@Vasil Michev do you know if this means I'd have to run Get-MailboxPermission multiple times in order to then do Select-Object -ExpandProperty against each property I'm trying to expand?

Highlighted

..Actually - nevermind, I get it now.  Only the commands that are available in the temporarily module are available, which includes Select-Object, but not Where-Object, and NoLanguage mode means no custom properties, etc.

 

This means, Get-MailboxPermission using Invoke-Command is not a good plan since I'd have to pull back all permissions this way, then sort through them and filter just the FullAccess ones on my side.  (<--: I'm completely wrong here.  Worst-case, we can make two passes with Get-MailboxPermission/Get-ADPermission/Get-RecipientPermission.  Once to get the index of all ACE's in the ACL with matching AccessRights/Inheritance/Allow|Deny criteria, then a second time to expand just the users for those ACE's.

 

Not bad at all!:ok_hand:  And no reason Invoke-Command would not be a good plan after all.:thumbs_up:)

 

Remaining comments removed: no longer relevant.

Highlighted

You can always filter the output client side. Or for the case of Get-MailboxPermission, use the -User parameter. The $using PowerShell modifier is a great way to pass variables inside remote sessions, I have few examples here: https://www.michev.info/Blog/Post/1687/using-variables-with-invoke-command-in-remote-powershell-sess...

Highlighted

Just wanted to touch back @Vasil Michev to say thanks for your help.  I've been busy since the day I opened this post, figuring our how to use Invoke-Command both with $args and $Using (cosmetically, I found use cases for using both).

 

I have updated my mailbox delegations script so that it's almost exclusively using Invoke-Command, in case it's of any value for you or anyone else who stumbles onto this thread:

Get-MailboxTrustee.ps1

 

If you have a lot of mailboxes to query, you may find the script's Write-Progress bars entertaining if nothing else.