Cmdlets for Microsoft Bing

Build 24.0.9060

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 Bing 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 BingCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module BingCmdlets;

You can then use the Connect-Bing cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-Bing -APIKey 'MyAPIKey'

Connecting to Microsoft Bing

To connect to Microsoft Bing, set the APIKey. To obtain the API key, sign into Microsoft Cognitive Services and register for the Bing Search APIs.

After you register, a list of endpoints and two keys are generated; you may use either one for the APIKey. You can use the APIVersion property to specify the API version you are connecting to. The possible values are 'V5' and 'V7'. The default value is 'V7'.

Retrieving Data

The Select-Bing cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-Bing -Connection $conn -Table "WebSearch" -Columns @("URL, Title") -Where "SearchTerms='Microsoft'"
The Invoke-Bing 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-Bing -Connection $conn -Table WebSearch -Where "SearchTerms = 'Microsoft'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myWebSearchData.csv -NoTypeInformation

You will notice that we piped the results from Select-Bing 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-Bing -APIKey 'MyAPIKey'
PS C:\> $row = Select-Bing -Connection $conn -Table "WebSearch" -Columns (URL, Title) -Where "SearchTerms = 'Microsoft'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "WebSearch",
  "Columns":  [

  ],
  "URL":  "MyURL",
  "Title":  "MyTitle"
} 

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060