0

My Goal is to apply a retention policy to newly created mailboxes. Details from previous article here.

My Current Script code is located here for easy readability or below:

# Get Start Time for script timer
$startDTM = (Get-Date)

#Authenticate using cached credentials or re-prompt for credentials.
if (Test-Path C:\temp\mycred.xml) {
    $UserCredential = Import-CliXML C:\temp\mycred.xml}
else{
    Get-Credential | Export-CliXml C:\temp\mycred.xml
    $UserCredential = Import-CliXML C:\temp\mycred.xml}

#Connect to Exchange Server
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://munprdcasht04.exchange.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
    Import-PSSession $Session

#returns alias' for mailboxes where creation date is <= 7 days and
#resides on "ABC" or "DEF" server and has no retention policy applied
$NeedsRetentions = (Get-Mailbox -ResultSize Unlimited| Where-Object {
    ($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
    (($_.ServerName -like "*munprdmbxa*") -or ($_.ServerName -like "*wauprdexa*")) -and
    ($_.retentionpolicy -ne "PurgeDeletedItemsFolder_60days")} |
    ft -auto alias)  

ForEach ($NeedsRetention in $NeedsRetentions){
    set-mailbox -Identity $NeedsRetention -RetentionPolicy "PurgeDeletedItemsFolder_60days"
    }

# Get End Time
$endDTM = (Get-Date)

# Echo Time elapsed
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"

When I echo the $NeedsRetentions i receive the list of AD users that i need to apply a retention policy to. But for some reason, when i loop through the the variable list with the below loop it errors out saying that the -Identity is no valid.

ForEach ($NeedsRetention in $NeedsRetentions){
    set-mailbox -Identity $NeedsRetention -RetentionPolicy "PurgeDeletedItemsFolder_60days"
    }

To troubleshoot i reduced the loop to this in order to display the individual usernames being affected by the script:

ForEach ($NeedsRetention in $NeedsRetentions){
[System.Windows.Forms.MessageBox]::Show($NeedsRetention)
}

Doing this causes a message box to show up for every line in the variable but the string seems to be empty and thus the default message shows up on the message box.

enter image description here

Any ideas why $NeedsRetention isn't passing correctly into my loop task?

Community
  • 1
  • 1
mttp1990
  • 114
  • 1
  • 1
  • 8
  • The object you stored in `NeedsRetentions` are mailboxes, the variable you are trying to pass down to `-identity` is likely to be string. I don't have this module so I can't test, but you can give this a shot `set-mailbox -Identity ($NeedsRetention).identity -RetentionPolicy "PurgeDeletedItemsFolder_60days"` – Kai Zhao May 20 '16 at 20:01
  • @Kai Zhao That makes sense but when i make this change i receive the following error for every mailbox listed in the variable `Cannot bind argument to parameter 'Identity' because it is null. + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindingValidationException' + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Set-Mailbox + PSComputerName : munprdcasht04.exchange.com` – mttp1990 May 20 '16 at 20:17
  • `-identity` takes names right? what does `get-mailbox | get-member` look like? Should be a property there you can use. – Kai Zhao May 20 '16 at 20:21
  • @Kai Zhao In My experience `-identity` will take AD username, displayname, alias, or email address. There are a to n of properties listed for that command – mttp1990 May 20 '16 at 20:26
  • Any property like `email` `emailaddress` you can plugin as `($NeedsRetention).email` or `($NeedsRetention).emailaddress`? same idea, `displayname` etc would work too – Kai Zhao May 20 '16 at 20:29
  • @Kai Zhao those didn't work as well, i read up a little bit on these commands and as it turns out i should be able to run my query and pipe it into the Set-mailbox command like so: `Get-Mailbox | Set-mailbox -RetentionPolicy "PurgeDeletedItemsFolder_60days"` but even this is returning the error i pasted into the above comment. – mttp1990 May 20 '16 at 20:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112543/discussion-between-kai-zhao-and-mttp1990). – Kai Zhao May 20 '16 at 21:04

1 Answers1

1

The problem is that you saved the result from a Format-*-cmdlet (in this case Format-Table by using the alias ft).

Never save the results from Format-* cmdlets to a variable. Format-*-cmdlets are for GUI/visual presentation only and turns your useful mailbox-objects into special format-objects that are useless for anything else then console-/file-output.

Modify:

$NeedsRetentions = (Get-Mailbox -ResultSize Unlimited| Where-Object {
    ($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
    (($_.ServerName -like "*munprdmbxa*") -or ($_.ServerName -like "*wauprdexa*")) -and
    ($_.retentionpolicy -ne "PurgeDeletedItemsFolder_60days")} |
    ft -auto alias) 

To:

$NeedsRetentions = Get-Mailbox -ResultSize Unlimited| Where-Object {
    ($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
    (($_.ServerName -like "*munprdmbxa*") -or ($_.ServerName -like "*wauprdexa*")) -and
    ($_.retentionpolicy -ne "PurgeDeletedItemsFolder_60days")}

If you want to output the $NeedsRetentions-objects to the console before continuing with modifying the mailboxes, then do that as a separate command (without saving the format-output). Ex:

$NeedsRetentions | Format-Table -AutoSize
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • re-running now, fingers crossed – mttp1990 May 20 '16 at 21:49
  • Good luck. Btw. When developing scripts, run them against a few users only to test (ex by adding a filter for username). It saves time and does less damage if it doesn't work. – Frode F. May 20 '16 at 22:23
  • thank you, normally i run this type of stuff in a specific OU to limit impact to the environment. you will likely see me on her more often, so until next time... – mttp1990 May 20 '16 at 22:26
  • Btw. Didn't notice the alias property, so unless `set-mailbox` picks out the right identity property auto. (which I think it does), then just modify it to ex. `set-mailbox -identity $needsretention.alias .....` – Frode F. May 20 '16 at 22:31
  • I already had a loop in place to apply the change. Just passing the identity was the issue. – mttp1990 May 20 '16 at 22:53