Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Bing Ads Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Bing Ads data from PowerShell? This article demonstrates how to utilize the Bing Ads Cmdlets for tasks like connecting to Bing Ads data, automating operations, downloading data, and more.
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.
PowerShell Cmdlets or ADO.NET Provider?
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 you will need to obtain a developer token. See the Getting Started section in the Bing Ads data provider help documentation for an authentication guide.
PowerShell
-
Install the module:
Install-Module BingAdsCmdlets
-
Connect:
$bingads = Connect-BingAds -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -AccountId "$AccountId" -CustomerId "$CustomerId" -DeveloperToken "$DeveloperToken"
-
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'}
ADO.NET
-
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 Bing Ads Data
PowerShell
Update-BingAds -Connection $BingAds -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table AdGroups -Id "MyId"
ADO.NET
$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()
Insert Bing Ads Data
PowerShell
Add-BingAds -Connection $BingAds -Table AdGroups -Columns @("Id", "Name") -Values @("MyId", "MyName")
ADO.NET
$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()
Delete Bing Ads Data
PowerShell
Remove-BingAds -Connection $BingAds -Table "AdGroups" -Id "MyId"
ADO.NET
$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