Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our AdobeCommerce Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module AdobeCommerceCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module AdobeCommerceCmdlets;
You can then use the Connect-AdobeCommerce cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-AdobeCommerce -User 'admin' -Password 'admin' -Url 'https://myadobecommercehost'
Connecting to AdobeCommerce
To connect, set the URL; for example, http://yoursitename/magento2, and optionally provide the StoreCode of the store you are connecting to, if more than one are present.
Authenticating to AdobeCommerce
To authenticate to AdobeCommerce server versions, you can use either the Basic or the Token AuthScheme.Basic
- AuthScheme Set this to Basic.
- User Set this to your user name in AdobeCommerce.
- Password Set this to your password in AdobeCommerce.
Access Token
- AuthScheme Set this to Token.
- AccessToken Set this to your AccessToken in AdobeCommerce.
To generate the AccessToken, Log in to the AdobeCommerce Stores > Settings > Configuration > Services > OAuth > Access Token.
Retrieving Data
The Select-AdobeCommerce cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-AdobeCommerce -Connection $conn -Table "Products" -Columns @("Price, Name") -Where "EntityId='238'"The Invoke-AdobeCommerce cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-AdobeCommerce -Connection $conn -Table Products -Where "EntityId = '238'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myProductsData.csv -NoTypeInformation
You will notice that we piped the results from Select-AdobeCommerce 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 object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-AdobeCommerce -User 'admin' -Password 'admin' -Url 'https://myadobecommercehost' PS C:\> $row = Select-AdobeCommerce -Connection $conn -Table "Products" -Columns (Price, Name) -Where "EntityId = '238'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Products", "Columns": [ ], "Price": "MyPrice", "Name": "MyName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-AdobeCommerce -Connection $conn -Table Products -Where "EntityId = '238'" | Remove-AdobeCommerce
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into AdobeCommerce, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyProductsUpdates.csv | %{ $record = Select-AdobeCommerce -Connection $conn -Table Products -Where ("EntityId = `'"+$_.EntityId+"`'") if($record){ Update-AdobeCommerce -Connection $conn -Table Products -Columns @("Price","Name") -Values @($_.Price, $_.Name) -Where "EntityId = `'$_.EntityId`'" }else{ Add-AdobeCommerce -Connection $conn -Table Products -Columns @("Price","Name") -Values @($_.Price, $_.Name) } }