Ready to get started?

Learn more about the CData ADO.NET Provider for USPS or download a free trial:

Download Now

Automate USPS Integration Tasks from PowerShell

Are you looking for a quick and easy way to access USPS data from PowerShell? We show how to use the Cmdlets for USPS and the CData ADO.NET Provider for USPS to connect to USPS data and synchronize, automate, download, and more.

The CData Cmdlets for USPS are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to USPS.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the USPS API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete USPS data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for USPS. To access USPS data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for USPS.

After obtaining the needed connection properties, accessing USPS data in PowerShell consists of three 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.

PowerShell

  1. Install the module:

    Install-Module USPSCmdlets
  2. Connect:

    $usps = Connect-USPS -PostageProvider "$PostageProvider" -RequestId "$RequestId" -Password "$Password" -AccountNumber "$AccountNumber"
  3. Search for and retrieve data:

    $senderid = "25" $senders = Select-USPS -Connection $usps -Table "Senders" -Where "SenderID = `'$SenderID`'" $senders

    You can also use the Invoke-USPS cmdlet to execute SQL commands:

    $senders = Invoke-USPS -Connection $usps -Query 'SELECT * FROM Senders WHERE SenderID = @SenderID' -Params @{'@SenderID'='25'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for USPS\lib\System.Data.CData.USPS.dll")
  2. Connect to USPS:

    $conn= New-Object System.Data.CData.USPS.USPSConnection("PostageProvider=ENDICIA; RequestId=12345; Password='abcdefghijklmnopqr'; AccountNumber='12A3B4C'") $conn.Open()
  3. Instantiate the USPSDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT FirstName, Phone from Senders" $da= New-Object System.Data.CData.USPS.USPSDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.firstname $_.phone }

Update USPS Data

PowerShell

Update-USPS -Connection $USPS -Columns @('FirstName','Phone') -Values @('MyFirstName', 'MyPhone') -Table Senders -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.USPS.USPSCommand("UPDATE Senders SET SenderID='25' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.USPS.USPSParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert USPS Data

PowerShell

Add-USPS -Connection $USPS -Table Senders -Columns @("FirstName", "Phone") -Values @("MyFirstName", "MyPhone")

ADO.NET

$cmd = New-Object System.Data.CData.USPS.USPSCommand("INSERT INTO Senders (SenderID) VALUES (@mySenderID)", $conn) $cmd.Parameters.Add(new System.Data.CData.USPS.USPSParameter("@mySenderID","25")) $cmd.ExecuteNonQuery()

Delete USPS Data

PowerShell

Remove-USPS -Connection $USPS -Table "Senders" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.USPS.USPSCommand("DELETE FROM Senders WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.USPS.USPSParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()