Lima
Lima

Reputation: 1211

Execute a cmdlet within a ForEach query

I am attempting to run the following PowerShell command against my Exchange 2010 SP1 server:

$colItems = Get-Mailbox -Filter {office -eq "ExportPST"}
ForEach($objItem in $colItems)
{
New-MailboxExportRequest -Mailbox $objItem -FilePath \\server\share$\"$objItem".pst
}

When I do this I receive the error:

Couldn't locate a database suitable for storing this request. + CategoryInfo : InvalidArgument: (domain.com....d/John Doe:MailboxOrMailUserIdParameter) [New-M ailboxExportRequest], MailboxDatabase...manentException + FullyQualifiedErrorId : 9322CB6D,Microsoft.Exchange.Management.RecipientTasks.NewMailboxExportRequest

What I am attempting to do is search across my AD users, locate users that have the string ExportPST in the Office field, and then export the command New-MailboxExportRequest to export the entire contents of the user's mailbox to a PST for each user returned.

When I add a Write-Host to the above, the outputed values are correct:

New-MailboxExportRequest -Mailbox jdoe -FilePath " \\server\share$\John Doe.pst"

The output also includes an extra space between the -FilePath " and \, which I imagine will cause an issue once the above issue has been resolved, is there a way to contactenate strings without it adding this extra space?

Thanks,

Matt

Upvotes: 0

Views: 2401

Answers (2)

Andy Arismendi
Andy Arismendi

Reputation: 52619

If $objItem is a string you can do:

-FilePath "\\server\share$\${objItem}.pst"

You can tell if it's a string by doing this:

$objItem.GetType().FullName

Even if it's not a string it still might work if the objects ToString() method provides the value you need because PowerShell will automatically call the ToString() method of the object and put it place in your string. So if this gives you the value you want: $objItem.ToString() then you may also use it. If there is a property however like $objItem.Name that you have to access then you will need to use string concatenation , a subexpression or .NET string formatting.

  • Concatenation: -FilePath ("\\server\share$\" + $objItem.Name + ".pst")
  • Subexpression: -FilePath "\\server\share$\$($objItem.Name).pst"
  • .NET string formatting: -FilePath ("\\server\share$\{0}.pst" -f $objItem.Name)

Upvotes: 0

user189198
user189198

Reputation:

I think your quoting is a bit off. Double-quoted string expansion works when you completely surround the string, not just the variable part.

$colItems = Get-Mailbox -Filter {office -eq "ExportPST"}
ForEach($objItem in $colItems)
{
  New-MailboxExportRequest -Mailbox $objItem -FilePath "\\server\share$\$objItem.pst"
}

Here's an alternative syntax which uses .NET string formatting / replacement:

$colItems = Get-Mailbox -Filter {office -eq "ExportPST"}
ForEach($objItem in $colItems)
{
  New-MailboxExportRequest -Mailbox $objItem -FilePath ('\\server\share$\{0}.pst' -f $objItem)
}

To make troubleshooting / debugging easier, you could assign the file path to a variable, and then pass the variable into the -FilePath parameter. That way you can see exactly which value is getting passed in.

$colItems = Get-Mailbox -Filter {office -eq "ExportPST"}
ForEach($objItem in $colItems)
{
  $FilePath = '\\server\share$\{0}.pst' -f $objItem;
  Write-Host -Object ('$FilePath value is: {0}' -f $FilePath);
  New-MailboxExportRequest -Mailbox $objItem -FilePath $FilePath;
}

Upvotes: 2

Related Questions