PowerShell remoting not working for Exchange mailbox size?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

PowerShell remoting not working for Exchange mailbox size?

Post by ITEngineer »

Hi All,

I'm running the below script from Remoting (Laptop with PowerShell ISE to On-premise Exchange) to get the mailbox size and the permissions, then Export to .CSV file:

Code: Select all

$DataPath = "C:\mbxresults.csv"
$UserMailboxes = Get-Mailbox -ResultSize Unlimited | Where-Object { $_.ObjectClass -notmatch '(SystemAttendantMailbox|ExOleDbSystemMailbox)' -and $_.RecipientTypeDetails -ne "DiscoveryMailbox" }

 ForEach ($mailboxes in $UserMailboxes) {
    $UPN = $mailboxes.UserPrincipalName
    $MailboxPermission = Get-MailboxPermission -Identity $UPN | Where-Object {("$($_.AccessRights)".Split(', ') -contains 'FullAccess') -and (-not $_.IsInherited) -and ($_.User.toString() -ne "NT AUTHORITY\SELF") -and ($_.User.toString() -notlike '*Discovery Management*')}
    $MailboxStats = Get-MailboxStatistics -Identity $UPN
    $User = Get-User -Identity $UPN
    [pscustomobject]([ordered]@{
        Name = $mailboxes.Name
        PrimarySmtpAddress = $_.PrimarySmtpAddress
        UPN = $UPN
        Alias = $mailboxes.alias
        OU = $mailboxes.organizationalUnit
        Server = $MailboxStats.ServerName
        Database = $MailboxStats.DatabaseName
        TotaItemSize = [Math]::Round([Int64]($MailboxStats).TotalItemSize.Value.ToString().Split("(")[1].Split()[0].Replace(",","") / 1MB, 2)
        Notes = $User.Notes
        RecipientTypeDetails = $_.RecipientTypeDetails.ToString()
        FullAccess = ($MailboxPermission | ForEach-Object {$_.User.ToString()}) -join ', '
    })
} | Sort-Object -Property TotaItemSize | Export-Csv -NoTypeInformation -Path $DataPath
But somehow it doesn't properly?

Here's the error message:

Code: Select all

At C:\Temp\Get-Perm&Size.ps1:22 char:3
+ } | Sort-Object -Property TotaItemSize | Export-Csv -NoTypeInformatio ...
+   ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : EmptyPipeElement

You cannot call a method on a null-valued expression.
At line:9 char:2
+     [pscustomobject]([ordered]@{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:9 char:2
+     [pscustomobject]([ordered]@{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:9 char:2
+     [pscustomobject]([ordered]@{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:9 char:2
+     [pscustomobject]([ordered]@{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Thanks in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell remoting not working for Exchange mailbox size?

Post by jvierra »

You are trying to pipe output but there is no pipeline. "foreach()" does not output to a pipeline.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked