Microsoft Exchange 2010 PowerShell Cookbook
上QQ阅读APP看书,第一时间看更新

Transferring files through remote shell connections

Since the Exchange 2010 Management Shell commands are executed through a remote PowerShell session, importing and exporting files requires a new special syntax. There are a handful of shell cmdlets that require this, and in this recipe we'll take a look at the syntax that needs to be used to transfer files through a remote shell connection.

How to do it...

Let's say that you are creating an Edge subscription to the hub transport servers in the default Active Directory site. After generating your XML subscription file on the Edge server, you can import the file using the New-EdgeSubscription cmdlet, using syntax similar to the following:

[byte[]]$data = Get-Content -Path "C:\Edge.xml" `
-Encoding Byte `
-ReadCount 0

New-EdgeSubscription -FileData $data -Site Default-First-Site

In this example, the file data is first read into a variable called $data. The subscription is then created using the New-EdgeSubscription cmdlet by as signing the $data variable as a value to the –FileData parameter.

How it works...

When you launch the Exchange 2010 Management Shell, special commands called proxy functions are imported into your local shell session. These proxy functions represent the compiled cmdlets that are actually installed on your Exchange server. When you run these commands, any data required for input through parameters are transferred through a remote connection from your machine to the server and the command is then executed. Since the commands are actually running on the server and not on your machine, we cannot use a local path for files that need to be imported.

In the previous example, you can see that we first stored the file data in a variable. What we are doing here is reading the file content into the variable using the Get-Content cmdlet in order to create a byte-encoded object. This variable is then assigned to the cmdlet's -FileData parameter, which requires a byte-encoded value.

There are a number of Exchange Management Shell cmdlets that include a -FileData parameter used to provide external files as input:

  • Import-ExchangeCertificate: Used for importing certificates
  • Import-JournalRuleCollection: Imports a collection of journal rules
  • Import-RecipientDataProperty: Used for importing photos into Active Directory
  • Import-TransportRuleCollection: Allows you to import a collection of transport rules
  • New-EdgeSubscription: Imports an Edge subscription file

This is a good example of how remote PowerShell sessions have changed things in Exchange 2010. For example, if you have worked with the shell in Exchange 2007, you may remember the Import-ExchangeCertificate cmdlet. This cmdlet used to accept a local file path when importing a certificate into a server, but, due to the new remoting functionality, the commands used to perform this task have changed, even though the cmdlet name is still the same.

There's more...

We also have to take remote shell connections into consideration when exporting data. For example, let's say that we need to export the user photo associated with a mailbox from Active Directory. The command would look something like this:

Export-RecipientDataProperty -Identity dsmith -Picture | %{ 
  $_.FileData | Add-Content C:\pics\dsmith.jpg -Encoding Byte
}

When using the Export-RecipientDataProperty cmdlet with the -Picture switch parameter, the photo can be retrieved from the FileData property of the object returned. The photo data is stored in this property as a byte array. In order to export the data, we need to loop through each element stored in this property and use the Add-Content cmdlet to re-construct the image to an external file.

When dealing with cmdlets that import or export data, make sure you utilize the help system. Remember, you can run Get-Help <cmdlet name> -Examples with any of these cmdlets to determine the correct syntax.

See also

  • Using the Help System in Chapter 1, PowerShell Key Concepts
  • Manually Configuring Remote PowerShell Connections