0

I need to make a call using Azure REST API to get the list of databases and delete the particular database if exits.

If somebody could give me a shell script that would be helpful. I tried this to list Azure SQL Databases on a given server

 wget -U --no-check-certificate    https://management.core.windows.net:8443/  subscriptionID/services/sqlservers/servers/serverName/p1y/databases? contentview=generic
--2016-01-22 22:15:57--  https://management.core.windows.net:8443/${subscriptionID}/services/sqlservers/servers/${serverName}/databases?contentview=generic
Resolving management.core.windows.net (management.core.windows.net)...xx.xxx.xxx.xxx
Connecting to management.core.windows.net (management.core.windows.net)|xx.xxx.xxx.xxx|:8443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2016-01-22 22:15:57 ERROR 403: Forbidden.

Note: I am not allowed to install AZURE CLI on the server. AZURE CLI makes my work easy :(

Dheeraj
  • 297
  • 1
  • 5
  • 17
  • I don't agree with this question being closed. It's not too broad. The question has a specific answer based on a documented Azure API (along with SDK's and PowerShell/CLI implementations that wrap the API) related to Azure's SQL Database service. The OP asked for solving this via PowerShell, and it's been answered as such. – David Makogon Jan 23 '16 at 04:25
  • You gave _an_ answer. But the OP has made clear your answer doesn't suit them. Indeed, this is a hallmark of "too broad": there are other possible answers, and too many for it to be practical to narrow the question down without more information. The OP needs to a) be more specific about whatever constraints he may have, and b) show **what he has tried already**. As stated, this is a "gimme teh codz" question. – Peter Duniho Jan 23 '16 at 05:29
  • Now I posted what I tried, Could somebody remove hold on my question and answer it!!! – Dheeraj Jan 23 '16 at 19:11

2 Answers2

1

Azure PowerShell already has commands built-in for doing this, and these commands implement the functionality exposed by the REST API (API docs here if you decide to go that route directly). Your question mentioned wanting to use PowerShell, so...

To enumerate SQL Database servers: Get-AzureSqlDatabaseServer

To enumerate database within a server: Get-AzureSqlDatabase passing a credentials context.

To remove a database: Remove-AzureSqlDatabase

With this, you should be able to stitch together a proper PowerShell script to enumerate and delete databases. For each command, you can execute get-help to see the details of needed parameters.

I answer server/database enumeration more fully in response to a different (not quite duplicate) question, here.

Community
  • 1
  • 1
David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • Thanks David for your quick response :) Actually Azure PowerShell is not installed on the server from which I want to give the request, Is there any way to use wget or something? – Dheeraj Jan 22 '16 at 22:54
  • You can make direct REST calls, but not sure if wget will cut it for you, given the headers you need to set up, etc. Or you can install Azure PowerShell cmdlets on your server... – David Makogon Jan 22 '16 at 23:30
  • I am not allowed to install Azure PowerShell cmdlets on the server. I tried using wget but it throws me following error " HTTP request sent, awaiting response... 403 Forbidden", Could you please give me more information :) – Dheeraj Jan 22 '16 at 23:34
0

Using the C# management libraries you can get access from c#:

 <package id="Microsoft.Azure.Management.Sql" version="0.43.0-prerelease" targetFramework="net46" />

and code like:

  var client = new SqlManagementClient();
  var db = client.Databases.List("resourceGroup","serverName").Databases.First();

Is this sufficient for what you are looking for. It is using the ARM Rest API.

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283