Share via

Setting retention dates using Powershell fails

Anonymous
2017-05-10T12:12:31+00:00

I'm facing a strange behavior regarding DateTime conversion when using the Set-Mailbox command to set retention settings on a client, having german culture settings. In my use case, this is done in a .NET exe using the classes from System.Management.Automation. The issue can be reproduced on a "regular" shell.

After creating an Exchange Online powershell connection, run the following commands for a user, that has RetentionHoldEnabled set to false:

$dtStart = New-Object System.DateTime -ArgumentList 2025,12,29,0,0,0

$dtEnd = New-Object System.DateTime -ArgumentList 2025,12,31,0,0,0

Set-Mailbox -Identity <user> -RetentionHoldEnabled $true -StartDateForRetentionHold $dtStart -EndDateForRetentionHold $dtEnd

When regional settings of the computer are set to "United States", the command runs fine. When set to "Germany", you'll get the following error:

Error on proxy command 'Set-Mailbox -Identity:'...' -RetentionHoldEnabled:$True -StartDateForRetentionHold:'29.12.2025 00:00:00' 

-EndDateForRetentionHold:'31.12.2025 00:00:00' -Confirm:$False -Force:$True' to server <...>.<...>.prod.outlook.com: Server version 15.01.1075.0000, Proxy method PSWS: 

Cmdlet error with following error message:

System.Management.Automation.ParentContainsErrorRecordException: Cannot process argument transformation on parameter 'StartDateForRetentionHold'. Cannot convert value "29.12.2025 00:00:00"

to type "System.DateTime". Error: "String was not recognized as a valid DateTime."..

    + CategoryInfo          : NotSpecified: (:) [Set-Mailbox], CmdletProxyException

    + FullyQualifiedErrorId : Microsoft.Exchange.Configuration.CmdletProxyException,Microsoft.Exchange.Management.RecipientTasks.SetMailbox

    + PSComputerName        : outlook.office365.com

As you can see, the command send to the server converts the DateTimes (dtStart/dtEnd) to string using the german culture. The remote machine then fails to parse the string back to a valid DateTime.

If you try to execute Set-Mailbox and pass the Datetime directly as string in the format yyyy-mm-dd it fails with the same error:

Set-Mailbox -Identity <Identity> -RetentionHoldEnabled $true -StartDateForRetentionHold "2025-12-29" -EndDateForRetentionHold "2025-12-31"

Error on proxy command 'Set-Mailbox -Identity:'<...>' -RetentionHoldEnabled:$True -StartDateForRetentionHold:'29.12.2025 00:00:00' 

-EndDateForRetentionHold:'31.12.2025 00:00:00' -Confirm:$False -Force:$True' to server <...>.<...>.prod.outlook.com: Server version 15.01.1075.0000, Proxy method PSWS: 

Cmdlet error with following error message:

System.Management.Automation.ParentContainsErrorRecordException: Cannot process argument transformation on parameter 'StartDateForRetentionHold'. Cannot convert value "29.12.2025 00:00:00"

to type "System.DateTime". Error: "String was not recognized as a valid DateTime."..

    + CategoryInfo          : NotSpecified: (:) [Set-Mailbox], CmdletProxyException

    + FullyQualifiedErrorId : Microsoft.Exchange.Configuration.CmdletProxyException,Microsoft.Exchange.Management.RecipientTasks.SetMailbox

    + PSComputerName        : outlook.office365.com

Things get strange when you do that operation in two steps

$dtStart = New-Object System.DateTime -ArgumentList 2025,12,29,0,0,0

$dtEnd = New-Object System.DateTime -ArgumentList 2025,12,31,0,0,0

Set-Mailbox -Identity <user> -RetentionHoldEnabled $true

Set-Mailbox -Identity <user> -StartDateForRetentionHold $dtStart -EndDateForRetentionHold $dtEnd

This works (almost) fine no matter if region is set to United States or Germany. The drawback of this "workaround" is, that sometimes the second "Set-Mailbox" does not recognize, that RetentionHoldEnabled was enabled and then fails with an error message, stating that RetentionHoldEnabled must be enabled before start- and enddate can be set.

The conversion error also occurs, when I try to set other properties along with start- and enddate (such as RetentionComment).

Am I missing something or am I doing something wrong? (Just to clarify, I've a workaround but the behavior seems very strange to me)

The client computer is btw. Windows 10 Enterprise 

PSVersion                      5.1.14393.1066

Microsoft 365 and Office | Subscription, account, billing | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-05-11T14:28:20+00:00

Hi StefanB82 

Thanks for the update. However, regarding the PowerShell script issue we have dedicated support team therefore I’d like to involve our Script Center support in finding the solution for you. Please post your question in this forum and our experts’ will focus on the requirement to get it resolved more efficiently. 

Your time and understanding will be highly appreciated.

Best regards,

Shyamal

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-05-11T06:29:51+00:00

    Hi Shyamal,

    thanks a lot. I can adjust my workaround in the .NET application. However, I'm just curious if the code that uses the DateTime instances is not supported or if it is considered a bug. Does this behavior also apply to other cmdlets that involve DateTimes?

    For reference, a short .NET snippet 

    using (var rsp = RunspaceFactory.CreateRunspace())

    {

    rsp.Open();

    // ... code to connect to office 365 ...

    var cmdSetMailbox = new Command("Set-Mailbox");

    cmdSetMailbox.Parameters.Add("Identity", "<user>");

    cmdSetMailbox.Parameters.Add("RetentionHoldEnabled", true);

    cmdSetMailbox.Parameters.Add("StartDateForRetentionHold", new DateTime(2025, 12, 29, 0, 0, 0));

    cmdSetMailbox.Parameters.Add("EndDateForRetentionHold", new DateTime(2025, 12, 30, 0, 0, 0));

    using (var pipeline = rsp.CreatePipeline())

    {

    pipeline.Commands.Add(cmdSetMailbox);

    pipeline.Invoke();

    if (pipeline.Error.Count > 0)

    {

    var errObj = pipeline.Error.ReadToEnd();

    throw new Exception(

    errObj.Select(o => o.ToString()).Aggregate((curr, next) => curr + "\n" + next));

    }

    }

    }

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-05-10T15:07:41+00:00

    Hi StefanB82 

    Use the short date format that's defined in the Regional Options settings on the computer where you're running the command. For example, if the computer is configured to use the short date format mm/dd/yyyy, enter 09/01/2015 to specify September 1, 2015. 

    You can enter the date only, or you can enter the date and time of day. If you enter the date and time of day, enclose the value in quotation marks ("), for example, "09/01/2015 5:00 PM".

    However, I have tested in my side its working fine with both United State and Germany regional settings in Windows 10 Enterprise. Here is the screenshot for your reference:

     

    For your reference: Set-Mailbox Parameters

    Best regards,

    Shyamal

    Was this answer helpful?

    0 comments No comments