今すぐお試しください!

製品の詳細Magento PowerShell Cmdlets を確認して、無償評価版をダウンロード:

今すぐダウンロード

PowerShell でMagento Data データをCSV に落としてみた

標準PowerShell cmdlets を使ってMagento テーブルにアクセス。

CData Cmdlets Module for Magento は、直感的なMagento データ連携を提供する標準cmdlet です。 本記事では、Magento Cmdlets を使ったサンプルを提供します。

Creating a Connection to Your Magento Data

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".

$conn = Connect-Magento  -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -Url "$Url"

Selecting Data

Follow the steps below to retrieve data from the Products table and pipe the result into to a CSV file:

Select-Magento -Connection $conn -Table Products | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myProductsData.csv -NoTypeInformation

You will notice that we piped the results from Select-Magento into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-Magento -Connection $conn -Table Products -Where "Style = High Tech" | Remove-Magento

Inserting and Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Magento, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyProductsUpdates.csv | %{
  $record = Select-Magento -Connection $Magento -Table Products -Where ("EntityId = `'"+$_.EntityId+"`'")
  if($record){
    Update-Magento -Connection $magento -Table Products -Columns ("Name","Price") -Values ($_.Name, $_.Price) -Where ("EntityId = `'"+$_.EntityId+"`'")
  }else{
    Add-Magento -Connection $magento -Table Products -Columns ("Name","Price") -Values ($_.Name, $_.Price)
  }
}

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!

 
 
ダウンロード