Microsoft Exchange Server PowerShell Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

Transferring files through remote shell connections

Since the Exchange 2013 Management Shell commands are executed through a remote PowerShell session, importing and exporting files require a new special syntax. There are a handful of shell cmdlets that require this. 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 we are about to import a certificate to the client access server. We can import the file using the Get-Content cmdlet, using a syntax similar to the following:

[byte[]]$data = Get-Content -Path ".\ExportedCert.pfx" ` -Encoding Byte ` -ReadCount 0
$password = Get-Credential
Import-ExchangeCertificate –FileData $data –Password 
$password.Password

In this example, the file data is first read into a variable called $data. The certificate import is done using the Import-ExchangeCertificate cmdlet by assigning the $data variable as a value to the –FileData parameter.

How it works...

When you launch the Exchange 2013 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 the input through parameters is 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 could see that we first stored the data file 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-DlpPolicyCollection: This is used to import DLP policy collections into the organization
  • Import-DlpPolicyTemplate: This is used to import DLP policy templates into the organization
  • Import-ExchangeCertificate: This is used to import certificates
  • Import-JournalRuleCollection: This is used to import a collection of journal rules
  • Import-RecipientDataProperty: This is used to import photos or audio into Active Directory
  • Import-TransportRuleCollection: This allows you to import a collection of transport rules
  • Import-UMPrompt: This is used to import custom audio files into the UM feature

This is a good example of how remote PowerShell sessions have changed things in Exchange 2010/2013. For example, if you have worked with the shell in Exchange 2007, you may remember the Import-ExchangeCertificate cmdlet. This cmdlet is 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 tdawson-Picture | %{ 
 $_.FileData | Add-Content C:\pics\tdawson.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 reconstruct the image to an external file.

When dealing with cmdlets that import or export data, make sure that 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