PowerShell Scripting to Replicate USPS Data to MySQL

Write a simple PowerShell script to replicate USPS data to a MySQL database.

The CData Cmdlets for USPS offer live access to USPS data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for USPS and the CData Cmdlets for MySQL in PowerShell to replicate USPS data to a MySQL database.

After obtaining the needed connection properties, accessing USPS data in PowerShell and preparing for replication consists of four basic steps.

To authenticate with USPS, set the following connection properties.

  • PostageProvider: The postage provider to use to process requests. Available options are ENDICIA and STAMPS. If unspecified, this property will default to ENDICIA.
  • UseSandbox: This controls whether live or test requests are sent to the production or sandbox servers. If set to true, the Password, AccountNumber, and StampsUserId properties are ignored.
  • StampsUserId: This value is used for logging into authentication to the Stamps servers. This value is not applicable for Endicia and is optional if UseSandbox is true.
  • Password: This value is used for logging into Endicia and Stamps servers. If the postage provider is Endicia, this will be the pass phrase associated with your postage account. It is optional if UseSandbox is true.
  • AccountNumber: The shipper's account number. It is optional if UseSandbox is true.
  • PrintLabelLocation: This property is required to use the GenerateLabels or GenerateReturnLabels stored procedures. This should be set to the folder location where generated labels should be stored.

The Cache Database

Many of the useful task available from USPS require a lot of data. To ensure this data is easy to input and recall later, utilize a cache database to make requests. Set the cache connection properties in order to use the cache:

  • CacheLocation: The path to the cache location, for which a connection will be configured with the default cache provider. For example, C:\users\username\documents\uspscache

As an alternative to CacheLocation, set the combination of CacheConnection and CacheProvider to configure a cache connection using a provider separate from the default.

Collecting USPS Data

  1. Install the module:

    Install-Module USPSCmdlets
  2. Connect to USPS:

    $usps = Connect-USPS -PostageProvider $PostageProvider -RequestId $RequestId -Password $Password -AccountNumber $AccountNumber
  3. Retrieve the data from a specific resource:

    $data = Select-USPS -Connection $usps -Table "Senders"

    You can also use the Invoke-USPS cmdlet to execute pure SQL-92 statements:

    $data = Invoke-USPS -Connection $usps -Query 'SELECT * FROM Senders WHERE SenderID = @SenderID' -Params @{'@SenderID'='25'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting USPS Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the USPS data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the USPS resource (Senders) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Senders" -Columns $columns -Values $values }

You have now replicated your USPS data to a MySQL database. This gives you freedom to work with USPS data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to USPS and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-USPS -Connection $usps -Table "Senders" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Senders" -Columns $columns -Values $values }
  • If you wish to replicate the USPS data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-USPS cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}