I'm trying to get the following command into a program I am developing in PowerShell studio, I'm having some issues figuring out the escaping sequence here, and testing takes forever since I have to import the PST just to view the data :(
Consider the following command which works correctly from the shell...
New-MailboxExportRequest -Mailbox [email protected] -Name JobName -IncludeFolders "#Inbox#/*","#SentItems#" -ContentFilter {(Received -gt "06/10/2022") -and (Sent -gt "06/10/2022")} -FilePath "\\Server\Folder\MyPST.pst"
I am using variables inside program designer, so my actual code looks like this...
New-MailboxExportRequest -Mailbox $mailbox -Name $jobname -IncludeFolders $IncludeFolders -ContentFilter {(Received -gt "$RecDate") -and (Sent -gt "$SentDate")} -FilePath "$FilePath"
Basically I need to get the first code sample working using variables, however the -IncludeFolders and -ContentFilter parameters require some escaping I can't seem to figure out. Any and all help is much appreciated.
tl;dr:
You need to enclose your
-ContentFilterargument in"..."overall in order to support embedding variable values, which you can themselves enclose in embedded'...'quoting:No special syntax considerations apply with respect to using a variable with
-IncludeFolders: Just make sure that variable$IncludeFolderscontains an array of strings identifying the target folders; applied to your example:If the list of folders must be parsed from user input provided as a single string (
$textbox5.textin this example) do the following to convert this single string to an array of names:Read on for background information and caveats re
-ContentFilter.Note:
The
-ContentFilterinformation below also applies to other Exchange cmdlets that accept filters, such asGet-Recipientwith its-Filterparameter.It also applies analogously to the
-Filterparameter used with AD (Active Directory) cmdlets such asGet-ADUser, although the situation there is complicated by the fact that the AD provider does support evaluation of PowerShell variables, but only in simple cases - see this answer.New-MailboxExportRequest's-ContentFilterexpects a string argument, not a script block ({ ... }), which you've used in your question.When a script block is used as a string (converted to one), string interpolation (embedding the values of variables in the string) is not supported - a script block stringifies to its verbatim content (sans
{and}).Conceptually, use of a script block can give the mistaken impression that an arbitrary piece of PowerShell code may be passed, which is not the case:
-ContentFiltersupports a domain-specific syntax only that only emulates a limited set of PowerShell features, called OPath filter syntax.Therefore:
It's best to use a string argument with
-ContentFilterto begin with.Since you do need to embed variable values, you need string interpolation, via an expandable (double-quoted) string (
"..."), inside of which you may quote values with'...'for syntactical convenience.In case there is no need to embed variable values, use a verbatim (single-quoted) string (
'...'), inside of which you may quote values with"...", as in your question. (In effect, this is the equivalent of the (ill-advised) use of{ ... }).Applied to your example: The following embeds the (stringified) values of variables
$RecDateand$SentDatein your-ContentFilterargument:Caveats:
If the values of the variables to embed themselves contain
', use escaped embedded"..."quoting instead; e.g.,`"$var`"If a value could contain
'and/or"and you don't know which, you'd have to use an extra layer of up-front string interpolation on the PowerShell side to escape one of them, depending on the embedded quoting character chosen, using$(...), the subexpression operator, using a-replaceoperation:In this escaping you must technically satisfy the OPath filter syntax requirements; the linked help OPath topic suggests that, like in PowerShell,
''can be used to escape'inside a'...'string; e.g.:-ContentFilter "Body -like '$($var -replace "'", "''")*'"While, as stated, OPath filters do not support evaluating PowerShell variable references in general, the following automatic variables - which are conceptually constants - are recognized (the linked help topic calls them system values):
$true,$false$nullTherefore, if you use
"..."for the outer quoting (for string interpolation), the$in these variables must be escaped with`(the so-called backtick, PowerShell's escape character), to prevent PowerShell from replacing these variable references with their values up front:`$true,`$false`$null