Ready to get started?

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

Download Now

Automate Magento Integration Tasks from PowerShell

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

The CData Cmdlets for Magento 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 Magento.

Cmdlets or ADO.NET?

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

After obtaining the needed connection properties, accessing Magento data in PowerShell consists of three basic steps.

Magento uses the OAuth 1 authentication standard. To connect to the Magento REST API, you will need to obtain values for the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties by registering an app with your Magento system. See the "Getting Started" section in the help documentation for a guide to obtaining the OAuth values and connecting.

You will also need to provide the URL to your Magento system. The URL depends on whether you are using the Magento REST API as a customer or administrator.

  • Customer: To use Magento as a customer, make sure you have created a customer account in the Magento homepage. To do so, click Account -> Register. You can then set the URL connection property to the endpoint of your Magento system.

  • Administrator: To access Magento as an administrator, set CustomAdminPath instead. This value can be obtained in the Advanced settings in the Admin menu, which can be accessed by selecting System -> Configuration -> Advanced -> Admin -> Admin Base URL.

    If the Use Custom Admin Path setting on this page is set to YES, the value is inside the Custom Admin Path text box; otherwise, set the CustomAdminPath connection property to the default value, which is "admin".

PowerShell

  1. Install the module:

    Install-Module MagentoCmdlets
  2. Connect:

    $magento = Connect-Magento -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -Url "$Url"
  3. Search for and retrieve data:

    $style = "High Tech" $products = Select-Magento -Connection $magento -Table "Products" -Where "Style = `'$Style`'" $products

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

    $products = Invoke-Magento -Connection $magento -Query 'SELECT * FROM Products WHERE Style = @Style' -Params @{'@Style'='High Tech'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.Magento.MagentoConnection("OAuthClientId=MyConsumerKey;OAuthClientSecret=MyConsumerSecret;CallbackURL=http://127.0.0.1:33333;Url=https://mymagentohost.com;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the MagentoDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, Price from Products" $da= New-Object System.Data.CData.Magento.MagentoDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.price }

Update Magento Data

PowerShell

Update-Magento -Connection $Magento -Columns @('Name','Price') -Values @('MyName', 'MyPrice') -Table Products -Id "MyEntityId"

ADO.NET

$cmd = New-Object System.Data.CData.Magento.MagentoCommand("UPDATE Products SET Style='High Tech' WHERE EntityId = @myEntityId", $conn) $cmd.Parameters.Add(new System.Data.CData.Magento.MagentoParameter("@myEntityId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Magento Data

PowerShell

Add-Magento -Connection $Magento -Table Products -Columns @("Name", "Price") -Values @("MyName", "MyPrice")

ADO.NET

$cmd = New-Object System.Data.CData.Magento.MagentoCommand("INSERT INTO Products (Style) VALUES (@myStyle)", $conn) $cmd.Parameters.Add(new System.Data.CData.Magento.MagentoParameter("@myStyle","High Tech")) $cmd.ExecuteNonQuery()

Delete Magento Data

PowerShell

Remove-Magento -Connection $Magento -Table "Products" -Id "MyEntityId"

ADO.NET

$cmd = New-Object System.Data.CData.Magento.MagentoCommand("DELETE FROM Products WHERE EntityId=@myEntityId", $conn) $cmd.Parameters.Add(new System.Data.CData.Magento.MagentoParameter("@myEntityId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()