![]() |
VOOZH | about |
The CData Cmdlets for Bing Ads 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 Bing Ads.
The Cmdlets are not only a PowerShell interface to Bing Ads, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Bing Ads data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Bing Ads. To access Bing Ads data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Bing Ads.
Once you have acquired the necessary connection properties, accessing Bing Ads data in PowerShell can be enabled in three steps.
The Bing Ads APIs use the OAuth 2 standard. To authenticate, you will need valid Bing Ads OAuth credentials and obtain a developer token. See the Getting Started section in the Bing Ads data provider help documentation for an authentication guide.
Install the module:
Install-Module BingAdsCmdlets
Connect:
$bingads = Connect-BingAds -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -AccountId "$AccountId" -CustomerId "$CustomerId" -DeveloperToken "$DeveloperToken" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$campaignid = "234505536" $adgroups = Select-BingAds -Connection $bingads -Table "AdGroups" -Where "CampaignId = `'$CampaignId`'" $adgroups
You can also use the Invoke-BingAds cmdlet to execute SQL commands:
$adgroups = Invoke-BingAds -Connection $bingads -Query 'SELECT * FROM AdGroups WHERE CampaignId = @CampaignId' -Params @{'@CampaignId'='234505536'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Bing Ads\lib\System.Data.CData.BingAds.dll")
Connect to Bing Ads:
$conn= New-Object System.Data.CData.BingAds.BingAdsConnection(" OAuthClientId=MyOAuthClientId; OAuthClientSecret=MyOAuthClientSecret; CallbackURL=http://localhost:portNumber; AccountId=442311; CustomerId=5521444; DeveloperToken=11112332233;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the BingAdsDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Name from AdGroups"
$da= New-Object System.Data.CData.BingAds.BingAdsDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.id $_.name
}
Update-BingAds -Connection $BingAds -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table AdGroups -Id "MyId"
$cmd = New-Object System.Data.CData.BingAds.BingAdsCommand("UPDATE AdGroups SET CampaignId='234505536' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.BingAds.BingAdsParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-BingAds -Connection $BingAds -Table AdGroups -Columns @("Id", "Name") -Values @("MyId", "MyName")
$cmd = New-Object System.Data.CData.BingAds.BingAdsCommand("INSERT INTO AdGroups (CampaignId) VALUES (@myCampaignId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.BingAds.BingAdsParameter("@myCampaignId","234505536")))
$cmd.ExecuteNonQuery()
Remove-BingAds -Connection $BingAds -Table "AdGroups" -Id "MyId"
$cmd = New-Object System.Data.CData.BingAds.BingAdsCommand("DELETE FROM AdGroups WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.BingAds.BingAdsParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Bing Ads Data Provider to get started:
Download NowLearn more:
👁 Bing Ads IconAn easy-to-use database-like interface for .NET applications access to live Bing Ads data (Campaigns, Ads, Customers, and more).