none
Exchange remote PowerShell - Problems using Invoke-Command and core commands RRS feed

  • Question

  • I am having some problems doing Invoke-Command session to a Exchange 2010 (windows server 2012). I am not getting any error running the script, but the export get failed status, and in the export details, I can see there is some problems with the variable $LastDayOfMonth. Can you help me how to work around? I dont think it is wrong date format, because $FirstDayOfMonth works fine, if I use that twice in the content filter. 
    Also for some reason I cannot do core command like Get-Date and Import-Module in invoke-command. If I want date, I need to do like this.  { [System.DateTime]::Now.ToString() }

    ContentFilter                 : ((Received -ge '01-12-2016 00:00:00') -and (Received -le '31-12-2016 00:00:00'))
    TotalQueuedDuration           : 00:00:03
    TotalInProgressDuration       : 00:00:00
    FailureCode                   : -2146233088
    FailureType                   : InvalidContentFilterPermanentException
    Message : Error: ContentFilter is invalid. The value "31-12-2016 00:00:00" could not be converted to type System.DateTime. --> The value "31-12-2016 00:00:00" could not be converted to type System.DateTime.

    Function Show-Menu {
    	Param ([string]$Title = 'Menu')
    	cls
    	Write-Host "================ $Title ================"
    	
    	Write-Host ""     
    	Write-Host "1: Archive .PST"
    	Write-Host "Q: Quit."
    }
    
    $ExchangeServer = "server01"
    $Credential = Get-Credential -Message "Enter Domain\username"
    $SessionParams = @{
    	ConfigurationName	= 'Microsoft.Exchange'
    	ConnectionURI		= "http://$ExchangeServer/powershell/"
    	Authentication		= 'Kerberos'
    	Credential			= $Credential
    }
    $Session = New-PSSession @SessionParams
    
    Do {
    	Show-Menu
    	$Input = Read-Host "Choose one"
    	Switch ($Input) {
    		'1' {
    			cls
    			$PstPath = "\\server01\c$\temp"
    			$Mailbox = Read-Host "Enter mailbox"
    
    			New-Item $PstPath\$Mailbox -Type Directory -Force
    			
                $ArchiveDate = Read-Host "Enter month/year (m-yyyy)"
    			$Month, $Year = $ArchiveDate.Split('-')
    			$FirstDayOfMonth = Get-Date -Date "$($Year)-$($Month)-1 00:00:00"
    			$LastDayOfMonth = $FirstDayOfMonth.AddMonths(1).AddDays(-1)
    
    			Invoke-Command -Session $Session -ArgumentList $PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth.ToString('yyyy-MM-dd'), $LastDayOfMonth.ToString('yyyy-MM-dd') -ScriptBlock {
    				Param($PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth, $LastDayOfMonth)
    				New-MailboxExportRequest -ContentFilter {(Received -ge $FirstDayOfMonth) -and (Received -le $LastDayOfMonth)} -Mailbox $Mailbox -FilePath $PstPath\$Mailbox\$ArchiveDate.pst
    			}
    
    		}
    		'q' {
    			Remove-PSSession $Session
    		}
    		default {
    			Write-Warning "Unknown choice '$_' - try again."
    		}
    	}
    	pause
    } Until ($Input -eq 'q')
    Tuesday, April 11, 2017 5:18 AM

Answers

  • As the error states, you date format is wrong.

    :The value "31-12-2016 00:00:00" could not be converted to type System.DateTime. --> The value "31-12-2016 00:00:00" could not be converted to type System.DateTime."

    If we do this we get the same error:

    D:\scripts> [datetime]"31-12-2016 00:00:00"
    Cannot convert value "31-12-2016 00:00:00" to type "System.DateTime". Error: "String was not recognized as a valid
    DateTime."
    At line:1 char:1
    + [datetime]"31-12-2016 00:00:00"

    However, this works:

    D:\scripts> [datetime]"12-31-2016 00:00:00"
    
    Saturday, December 31, 2016 12:00:00 AM

    Simple datw format issue.  Your server's locale is US-en and you are giving a European date string.

    "m-yyyy" can be done like this:

    D:\scripts> ([datetime]"1-2011").AddDays(-1)
    
    Friday, December 31, 2010 12:00:00 AM


    \_(ツ)_/


    • Edited by jrv Tuesday, April 11, 2017 5:32 AM
    • Marked as answer by sundonald Tuesday, April 11, 2017 10:44 AM
    Tuesday, April 11, 2017 5:29 AM

All replies

  • As the error states, you date format is wrong.

    :The value "31-12-2016 00:00:00" could not be converted to type System.DateTime. --> The value "31-12-2016 00:00:00" could not be converted to type System.DateTime."

    If we do this we get the same error:

    D:\scripts> [datetime]"31-12-2016 00:00:00"
    Cannot convert value "31-12-2016 00:00:00" to type "System.DateTime". Error: "String was not recognized as a valid
    DateTime."
    At line:1 char:1
    + [datetime]"31-12-2016 00:00:00"

    However, this works:

    D:\scripts> [datetime]"12-31-2016 00:00:00"
    
    Saturday, December 31, 2016 12:00:00 AM

    Simple datw format issue.  Your server's locale is US-en and you are giving a European date string.

    "m-yyyy" can be done like this:

    D:\scripts> ([datetime]"1-2011").AddDays(-1)
    
    Friday, December 31, 2010 12:00:00 AM


    \_(ツ)_/


    • Edited by jrv Tuesday, April 11, 2017 5:32 AM
    • Marked as answer by sundonald Tuesday, April 11, 2017 10:44 AM
    Tuesday, April 11, 2017 5:29 AM
  • Something strange is going on with the date format.

    I tried to just enter all data and do it like this with US-en time

    Invoke-Command -Session $Session -ScriptBlock { New-MailboxExportRequest -ContentFilter {(Received -ge '03/01/2017') -and (Received -le '03/25/2017')} -Mailbox testuser -FilePath "\\server\c$\temp\user\test.pst" }

    Fail again:

    ContentFilter                 : ((Received -ge '01-03-2017 00:00:00') -and (Received -le '25-03-2017 00:00:00'))
    ContentFilterLanguage         :
    BadItemLimit                  : 0
    BadItemsEncountered           : 0
    QueuedTimestamp               : 11-04-2017 08:46:44
    StartTimestamp                :
    LastUpdateTimestamp           : 11-04-2017 08:46:50
    CompletionTimestamp           :
    SuspendedTimestamp            :
    OverallDuration               : 00:00:08
    TotalSuspendedDuration        :
    TotalFailedDuration           : 00:00:03
    TotalQueuedDuration           : 00:00:05
    TotalInProgressDuration       : 00:00:00
    TotalStalledDueToHADuration   :
    TotalTransientFailureDuration :
    MRSServerName                 :
    EstimatedTransferSize         : 0 B (0 bytes)
    EstimatedTransferItemCount    : 0
    BytesTransferred              : 0 B (0 bytes)
    BytesTransferredPerMinute     :
    ItemsTransferred              : 0
    PercentComplete               : 0
    PositionInQueue               :
    FailureCode                   : -2146233088
    FailureType                   : InvalidContentFilterPermanentException
    FailureSide                   :
    Message                       : Error: ContentFilter is invalid. The value "25-03-2017 00:00:00" could not be converted to type System.DateTime. --> The value "25-03-2017 00:00:00" could not be converted to                           type System.DateTime.
    FailureTimestamp              : 11-04-2017 08:46:50


    • Edited by sundonald Tuesday, April 11, 2017 6:58 AM
    Tuesday, April 11, 2017 6:54 AM
  • Sounds like you need to fix your Exchange or Exchange is set to European time locale.

    Try this:

    $sb = {
    	$filter = {(Received -ge '01/03/2017') -and (Received -le '25/03/2017')}
    	New-MailboxExportRequest -ContentFilter $filter -Mailbox testuser -FilePath "\\server\c$\temp\user\test.pst"
    }
    Invoke-Command -Session $Session -ScriptBlock $sb
    

    If that works then you need to convert your local dates to a European format.


    \_(ツ)_/

    Tuesday, April 11, 2017 7:12 AM
  • If I do like that I get error in my own powershell

    The provided ContentFilter value is invalid. ContentFilter is invalid. The value "25/03/2017" could not be converted to type System.DateTime. --> The value "25/03/2017" could not be converted to type System.DateTime.
        + CategoryInfo          : InvalidArgument: ((Received -ge '...e '25/03/2017'):String) [], ContentFilterInvalidPermanentException
        + FullyQualifiedErrorId : E3E296C6
        + PSComputerName        : server

    Tuesday, April 11, 2017 7:22 AM
  • The main issue is that you should be importing the remote session and not invoking it.

    Assuming the local system is en-US.

    $sb = {
    $filter
    = {(Received -ge ([datetime]'03/01/2017')) -and (Received -le ([datetime]'3/25/2017'))}
    New-MailboxExportRequest -ContentFilter $filter -Mailbox testuser -FilePath "\\server\c$\temp\user\test.pst"
    }
    Invoke-Command -Session $Session -ScriptBlock $sb


    \_(ツ)_/


    • Edited by jrv Tuesday, April 11, 2017 8:03 AM
    Tuesday, April 11, 2017 8:02 AM
  • Sorry :/

    The provided ContentFilter value is invalid. ContentFilter is invalid. Invalid filter syntax. For a description of the filter parameter syntax see the command help.
    "(Received -ge ([datetime]'03/01/2017')) -and (Received -le ([datetime]'3/25/2017'))" at position 15. --> Invalid filter syntax. For a description of the filter parameter syntax see the command help.
    "(Received -ge ([datetime]'03/01/2017')) -and (Received -le ([datetime]'3/25/2017'))" at position 15.
        + CategoryInfo          : InvalidArgument: ((Received -ge (...e]'3/25/2017')):String) [], ContentFilterInvalidPermanentException
        + FullyQualifiedErrorId : CD63D22D
        + PSComputerName        : server

    Tuesday, April 11, 2017 8:36 AM
  • What version if Exchange?

    How much have you used Exchange CmdLets before?


    \_(ツ)_/

    Tuesday, April 11, 2017 8:39 AM
  • Exchange 2010 on a windows 2012 server. I have done some cmdlets before, but im consider myself a newbie.
    I just wanted to see if I could make a script for remote instead. I had it working doing Import-PSSession, but then it makes en extra session "Creating a new session for implicit remoting of New-MailboxExportRequest command" instead of using the first one, so i thought that invoke-command was the way to go.
    Tuesday, April 11, 2017 8:51 AM
  • I recommend reviewing the documentation on using the CmdLets.  Invoke is not the correct method as I noted much earlier.


    \_(ツ)_/

    Tuesday, April 11, 2017 9:58 AM
  • After looking online i notice other having same problems when you are not having Exchange regional in US english. 

    "There is a bug when using the New-MailboxExportRequest powershell script in Exchange 2010 and stating a content filter using dates in non-US format. The New-MailboxExportRequest cmdlet will only recognize dates in US formats"

    Tuesday, April 11, 2017 10:37 AM