Avatar of Scotch Tech
Scotch Tech
Flag for United States of America asked on

Need help with powershell script

trying to get this script to go through each mailbox and run the cmd for each. But testing one mailbox it keeps prompting for Identity:



get-mailbox alias | foreach {Get-MailboxFolderStatistics | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) }
| ft -AutoSize identity,date,{$mailboxes.retentionpolicy}}

Open in new window


PowershellExchange

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
Jose Gabriel Ortega Castro

Hello Scotch!

Can you clarify what are u willing to do on each mailbox?

try this:

Get-Mailbox -resultsize unlimited | Foreach{ 
   Get-MailboxFolderStatistics | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date

Open in new window

You are not definingc orrectly the retentionpolicy var it should be something like 
@{ Name = '';  Expression = {}}

Open in new window


so try:

Get-Mailbox -resultsize unlimited | Foreach{
   Get-MailboxFolderStatistics | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date,@{ Name = 'RetentionPolicy';  Expression = {$mailboxes.retentionpolicy}}

Open in new window

Scotch Tech

ASKER
still prompts for Identity

Jose Gabriel Ortega Castro

OK, are you trying to do this on what version of exchange?

I'd try on my test one :) (it's 2016)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Scotch Tech

ASKER
2016
Scotch Tech

ASKER
so this is what I'm doing:

$Mailboxes=get-mailbox alias
Get-Mailbox -resultsize unlimited | Foreach{
   Get-MailboxFolderStatistics | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date,@{ Name = 'RetentionPolicy';  Expression = {$mailboxes.retentionpolicy}}
Scotch Tech

ASKER
wait, i mean this:

$Mailboxes=get-mailbox alias
$mailboxes  | Foreach{
   Get-MailboxFolderStatistics | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date,@{ Name = 'RetentionPolicy';  Expression = {$mailboxes.retentionpolicy}}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Scotch Tech

ASKER
it then prompts:

cmdlet Get-MailboxFolderStatistics at command pipeline position 1
Supply values for the following parameters:
Identity:
Jose Gabriel Ortega Castro

$Mailboxes=get-mailbox alias => this line is bad.

Try this one:
Get-Mailbox -Resultsize Unlimited | Foreach{
   Get-MailboxFolderStatistics -identity $_.alias | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date,@{ Name = 'RetentionPolicy';  Expression = {$mailboxes.retentionpolicy}}}

Open in new window

 Or this one It's the same

Get-Mailbox -Resultsize Unlimited | Get-MailboxFolderStatistics | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date,@{ Name = 'RetentionPolicy';  Expression = {$mailboxes.retentionpolicy}}

Open in new window

Scotch Tech

ASKER
but the variable $mailboxes needs to be defined
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Jose Gabriel Ortega Castro

You right:

Get-Mailbox -Resultsize Unlimited | Foreach{    Get-MailboxFolderStatistics -identity $_.alias | ? {$_.folderpath -like "/inbox/*" -and $_.date -gt (Get-Date).AddDays(-30) } | ft -autosize Identity,Date,@{ Name = 'RetentionPolicy';  Expression = {$_.retentionpolicy}}}

Open in new window

Qlemo

That's correct, but can be simplified:
Get-Mailbox -Resultsize Unlimited | Get-MailboxFolderStatistics |
  ? folderpath -like '/inbox/*' |
  ? date -gt (Get-Date).AddDays(-30) |
  ft -autosize Identity, Date, RetentionPolicy

Open in new window

Scotch Tech

ASKER
This will not work becuase it can't get Retention Policy from MailboxFodlerStatistics, it needs to get it from Mailbox.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Scotch Tech

ASKER
I believe we need to combine the two, in order to grab info from both and export it into one output.
Qlemo

Have been afraid of that. It is something I could not test easily, because we do not have a retention policy ;-).
Then we indeed need to combine results of both.
Get-Mailbox -Resultsize Unlimited |
  % {
    $mb = $_
    Get-MailboxFolderStatistics $mb |
      ? folderpath -like '/inbox/*' |
      ? date -gt (Get-Date).AddDays(-30) |
      Select Identity, Date, @{n='RetentionPolicy'; e={$mb.RetentionPolicy}}
  } |
  ft -autosize *

Open in new window

Scotch Tech

ASKER
thanks Qlemo! this seemed to work.
I ran on one database.

How how can I run on specific databases that start with USDB*

For example I tried modifiying the first line to:
get-mailboxdatabase usdb* | Get-Mailbox -resultsize unlimited |

but got this error:
Get-MailboxFolderStatistics : The session Session1, 81050677-9de3-4889-ac25-a682e8d829df, ffnypxchg01.ffhsj.com is not available to run commands.  The
session availability is Busy.
At line:4 char:5
+     Get-MailboxFolderStatistics $mb |
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ([PSSession]Session1:PSSession) [Invoke-Command], InvalidRunspaceStateException
    + FullyQualifiedErrorId : InvokeCommandCommandInvalidSessionAvailability,Microsoft.PowerShell.Commands.InvokeCommandCommand

Get-MailboxFolderStatistics : No valid sessions were specified.  Ensure you provide valid sessions that are in the Opened state and are available to

Your help has saved me hundreds of hours of internet surfing.
fblack61
Qlemo

I don't know for sure, but (remote) Exchange has some issues with multiple pipeline actions getting nested too deep. Sometimes two levels are ok, three are not. Since piplines in PowerShell are processed sequentially (that is, each object is passed down the pipeline until there is no further step to process, before the next object is retrieved), each Exchange cmdlet participating in the pipeline has to use own session "clones", and that might lead to strange error messages like the above.

Having said that, we are lucky here. Get-Mailbox itself allows for a direct database filter, removing the need for adding something to the pipeline:
Get-Mailbox -Database usdb* -Resultsize Unlimited |
  % {
    $mb = $_
    Get-MailboxFolderStatistics $mb |
      ? folderpath -like '/inbox/*' |
      ? date -gt (Get-Date).AddDays(-30) |
      Select Identity, Date, @{n='RetentionPolicy'; e={$mb.RetentionPolicy}}
  } |
  ft -autosize *

Open in new window

Scotch Tech

ASKER
thanks.
Unfortunately that filter does not work with wildcards, thats why i was using get-mailboxdatabase usdb*
Qlemo

Strange, because it works fine for me ...
In  that case:
$mbs = get-mailboxdatabase usdb* | Get-Mailbox -resultsize unlimited
$(foreach ($mb in $mbs) {
  Get-MailboxFolderStatistics $mb |
    ? folderpath -like '/inbox/*' |
    ? date -gt (Get-Date).AddDays(-30) |
    Select Identity, Date, @{n='RetentionPolicy'; e={$mb.RetentionPolicy}}
}) |
  ft -autosize *

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Scotch Tech

ASKER
somethings not working with script
Scotch Tech

ASKER
[PS] C:\Windows\system32>$mbs = get-mailboxdatabase usdb01 | Get-Mailbox -resultsize unlimited
[PS] C:\Windows\system32>$(foreach ($mb in $mbs) {
>>   Get-MailboxFolderStatistics $mb |
>>     ? folderpath -like '/inbox/*' |
>>     ? date -gt (Get-Date).AddDays(-30) |
>>     Select Identity, Date, @{n='RetentionPolicy'; e= $mb.RetentionPolicy}}
>> }) |
>>   ft -autosize *
At line:6 char:1
+ }) |
+ ~
Missing closing ')' in subexpression.
At line:6 char:1
+ }) |
+ ~
Unexpected token '}' in expression or statement.
At line:6 char:2
+ }) |
+  ~
Unexpected token ')' in expression or statement.
At line:6 char:4
+ }) |
+    ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInSubexpression
Qlemo

Somehow a opening curly brace got lost when copying in the code here ... I've changed my comment above accordingly.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Scotch Tech

ASKER
This seems to work. I'm running under scheduled task now. I'll keep you updated.
thanks
Scotch Tech

ASKER
this is great. But can you help me get it output to a file correctly. The below script is what I have but the columns are truncated, I don't get the Retention Policy Column when outputted to a file.

This is what it looks like:
Identity                                                                         Date            
--------                                                                         ----            
ffhsj.com/NY/Users and Groups/Grossman, Scott\Inbox\thisisAtest                  8/31/2021 12:2...
ffhsj.com/NY/Users and Groups/Bonilla, Astrid\Inbox\Rejections                   9/2/2021 4:13:...
ffhsj.com/NY/Users and Groups/Bonilla, Astrid\Inbox\Unsubmitted                  9/2/2021 4:13:...
ffhsj.com/NY/Users and Groups/Lopez-Long, Teresa\Inbox\Anna Legal assistant tips 9/10/2021 8:14...
ffhsj.com/NY/Users and Groups/Lopez-Long, Teresa\Inbox\Austin                    9/21/2021 11:4...
ffhsj.com/NY/Users and Groups/Lopez-Long, Teresa\Inbox\Kendall                   9/10/2021 8:17...
ffhsj.com/NY/Users and Groups/Lopez-Long, Teresa\Inbox\Local Counsel             9/14/2021 3:40...
ffhsj.com/NY/Users and Groups/Rivers, Tamiko\Inbox\Quick Step Settings           8/27/2021 12:5...
ffhsj.com/NY/Users and Groups/Rivers, Tamiko\Inbox\Santana Bianka                9/3/2021 2:58:...

Open in new window



This is the code I'm using:
$OutFileSubFolder = ("D:\Admin\" + (Get-Date).adddays(-1).tostring('MM-dd-yyyy')  + "InboxSubfolders.csv")
$mbs = get-mailboxdatabase usdb01 | Get-Mailbox -resultsize unlimited
$(foreach ($mb in $mbs) {
  Get-MailboxFolderStatistics $mb |
    ? folderpath -like '/inbox/*' |
    ? date -gt (Get-Date).AddDays(-30) |
    Select Identity, Date, @{n='RetentionPolicy'; e={$mb.RetentionPolicy}}
}) |
  ft -autosize * > $OutFileSubFolder

Open in new window

ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question