PowerShell remoting not working for Exchange mailbox size?

Ask your Windows 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.
Locked
User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

PowerShell remoting not working for Exchange mailbox size?

Post by ITEngineer » Thu Sep 27, 2018 7:23 pm

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

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell remoting not working for Exchange mailbox size?

Post by jvierra » Thu Sep 27, 2018 9:04 pm

You are trying to pipe output but there is no pipeline. "foreach()" does not output to a pipeline.

Locked