Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate WordPress Integration Tasks from PowerShell
Are you in search of a quick and easy way to access WordPress data from PowerShell? This article demonstrates how to utilize the WordPress Cmdlets for tasks like connecting to WordPress data, automating operations, downloading data, and more.
The CData Cmdlets for WordPress 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 WordPress.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to WordPress, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete WordPress data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Wordpress. To access WordPress data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Wordpress.
Once you have acquired the necessary connection properties, accessing WordPress data in PowerShell can be enabled in three steps.
To connect to WordPress, set the URL property and other authentication properties. WordPress supports Basic (User and Password) and OAuth2.0 authentication, though Basic is recommended for a testing environment only. To connect with OAuth you will need to register an app with WordPress.
See the Getting Started guide in the CData driver documentation for more information.
PowerShell
-
Install the module:
Install-Module WordPressCmdlets
-
Connect:
$wordpress = Connect-WordPress -Url "$Url"
-
Search for and retrieve data:
$id = "1" $categories = Select-WordPress -Connection $wordpress -Table "Categories" -Where "Id = `'$Id`'" $categories
You can also use the Invoke-WordPress cmdlet to execute SQL commands:
$categories = Invoke-WordPress -Connection $wordpress -Query 'SELECT * FROM Categories WHERE Id = @Id' -Params @{'@Id'='1'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Wordpress\lib\System.Data.CData.WordPress.dll")
-
Connect to WordPress:
$conn= New-Object System.Data.CData.WordPress.WordPressConnection("Url=http://www.yourwordpresshost.com;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the WordPressDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Name from Categories" $da= New-Object System.Data.CData.WordPress.WordPressDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.name }
Update WordPress Data
PowerShell
Update-WordPress -Connection $WordPress -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table Categories -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("UPDATE Categories SET Id='1' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.WordPress.WordPressParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert WordPress Data
PowerShell
Add-WordPress -Connection $WordPress -Table Categories -Columns @("Id", "Name") -Values @("MyId", "MyName")
ADO.NET
$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("INSERT INTO Categories (Id) VALUES (@myId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.WordPress.WordPressParameter("@myId","1")))
$cmd.ExecuteNonQuery()
Delete WordPress Data
PowerShell
Remove-WordPress -Connection $WordPress -Table "Categories" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("DELETE FROM Categories WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.WordPress.WordPressParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject