VOOZH about

URL: https://www.cdata.com/kb/tech/xml-ado-powershell.rst

⇱ Automate XML Integration Tasks from PowerShell


Automate XML Integration Tasks from PowerShell

👁 Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access XML data from PowerShell? This article demonstrates how to utilize the XML Cmdlets for tasks like connecting to XML data, automating operations, downloading data, and more.

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

PowerShell Cmdlets or ADO.NET Provider?

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

Once you have acquired the necessary connection properties, accessing XML data in PowerShell can be enabled in three steps.

Connecting to Local or Cloud-Stored (Box, Google Drive, Amazon S3, SharePoint) XML Files

CData Drivers let you work with XML files stored locally and stored in cloud storage services like Box, Amazon S3, Google Drive, or SharePoint, right where they are.

Setting connection properties for local files

Set the URI property to local folder path.

Setting connection properties for files stored in Amazon S3

To connect to XML file(s) within Amazon S3, set the URI property to the URI of the Bucket and Folder where the intended XML files exist. In addition, at least set these properties:

  • AWSAccessKey: AWS Access Key (username)
  • AWSSecretKey: AWS Secret Key

Setting connection properties for files stored in Box

To connect to XML file(s) within Box, set the URI property to the URI of the folder that includes the intended XML file(s). Use the OAuth authentication method to connect to Box.

Dropbox

To connect to XML file(s) within Dropbox, set the URI proprerty to the URI of the folder that includes the intended XML file(s). Use the OAuth authentication method to connect to Dropbox. Either User Account or Service Account can be used to authenticate.

SharePoint Online (SOAP)

To connect to XML file(s) within SharePoint with SOAP Schema, set the URI proprerty to the URI of the document library that includes the intended XML file. Set User, Password, and StorageBaseURL.

SharePoint Online REST

To connect to XML file(s) within SharePoint with REST Schema, set the URI proprerty to the URI of the document library that includes the intended XML file. StorageBaseURL is optional. If not set, the driver will use the root drive. OAuth is used to authenticate.

Google Drive

To connect to XML file(s) within Google Drive, set the URI property to the URI of the folder that includes the intended XML file(s). Use the OAuth authentication method to connect and set InitiateOAuth to GETANDREFRESH.

The property is the controlling property over how your data is represented into tables and toggles the following basic configurations.

  • Document (default): Model a top-level, document view of your XML data. The data provider returns nested elements as aggregates of data.
  • FlattenedDocuments: Implicitly join nested documents and their parents into a single table.
  • Relational: Return individual, related tables from hierarchical data. The tables contain a primary key and a foreign key that links to the parent document.

See the Modeling XML Data chapter for more information on configuring the relational representation. You will also find the sample data used in the following examples. The data includes entries for people, the cars they own, and various maintenance services performed on those cars.

PowerShell

  1. Install the module:

    Install-Module XMLCmdlets
  2. Connect:

    $xml = Connect-XML -URI "$URI" -DataModel "$DataModel"
    
  3. Search for and retrieve data:

    $[ personal.name.last ] = "Roberts"
    $people = Select-XML -Connection $xml -Table "people" -Where "[ personal.name.last ] = `'$[ personal.name.last ]`'"
    $people
    

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

    $people = Invoke-XML -Connection $xml -Query 'SELECT * FROM people WHERE [ personal.name.last ] = @[ personal.name.last ]' -Params @{'@[ personal.name.last ]'='Roberts'}
    

ADO.NET

  1. Load the provider's assembly:

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

     
    $conn= New-Object System.Data.CData.XML.XMLConnection("URI=C:/people.xml;DataModel=Relational;")
    $conn.Open()
    
  3. Instantiate the XMLDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT [ personal.name.first ], [ personal.name.last ] from people"
    
    $da= New-Object System.Data.CData.XML.XMLDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
    	Write-Host $_.[ personal.name.first ] $_.[ personal.name.last ]
    }
     

Update XML Data

PowerShell

Update-XML -Connection $XML -Columns @('[ personal.name.first ]','[ personal.name.last ]') -Values @('My[ personal.name.first ]', 'My[ personal.name.last ]') -Table people -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.XML.XMLCommand("UPDATE people SET [ personal.name.last ]='Roberts' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.XML.XMLParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert XML Data

PowerShell

Add-XML -Connection $XML -Table people -Columns @("[ personal.name.first ]", "[ personal.name.last ]") -Values @("My[ personal.name.first ]", "My[ personal.name.last ]") 

ADO.NET

$cmd = New-Object System.Data.CData.XML.XMLCommand("INSERT INTO people ([ personal.name.last ]) VALUES (@my[ personal.name.last ])", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.XML.XMLParameter("@my[ personal.name.last ]","Roberts")))
$cmd.ExecuteNonQuery()

Delete XML Data

PowerShell

Remove-XML -Connection $XML -Table "people" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.XML.XMLCommand("DELETE FROM people WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.XML.XMLParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject