Link to home
Start Free TrialLog in
Avatar of Scotch Tech
Scotch TechFlag 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


Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

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

Avatar of Scotch Tech

ASKER

still prompts for Identity

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

I'd try on my test one :) (it's 2016)
2016
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}}
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}}
it then prompts:

cmdlet Get-MailboxFolderStatistics at command pipeline position 1
Supply values for the following parameters:
Identity:
$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

but the variable $mailboxes needs to be defined
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

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

This will not work becuase it can't get Retention Policy from MailboxFodlerStatistics, it needs to get it from Mailbox.
I believe we need to combine the two, in order to grab info from both and export it into one output.
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

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

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

thanks.
Unfortunately that filter does not work with wildcards, thats why i was using get-mailboxdatabase usdb*
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

somethings not working with script
[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
Somehow a opening curly brace got lost when copying in the code here ... I've changed my comment above accordingly.
This seems to work. I'm running under scheduled task now. I'll keep you updated.
thanks
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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial