使用 Powershell 和 AzureResourceManager 模式创建 SQL Azure 服务器

Creating SQL Azure Server using Powershell with AzureResourceManager Mode

似乎您只能使用 AzureServiceManagement 模式通过 Azure 上的 Powershell 创建一个 SQL 服务器。这会为我在名为 "Default-SQL-WestUS" 的默认资源组中创建一个新服务器。但是我怎样才能将它放入现有的资源组中,以便我可以将它与其他相关资源一起管理?

当我尝试使用 AzureResourceManagement 时,Cmndlet 在 -Name 参数上阻塞:

New-AzureResource -Name "testName" -Location "West US" -ResourceGroupName "TestGroupName" -ResourceType Microsoft.Sql/servers -ApiVersion "2014-04-01"

当我 运行 以上时,我得到:

'' is an invalid name because it contains a NULL character or an invalid unicode character.

我还删除了 -Name 参数(认为 Azure 应该像使用 ServiceManager 一样为我创建这个名称):

New-AzureResource -Location "West US" -ResourceGroupName "TestGroupName" -ResourceType Microsoft.Sql/servers -ApiVersion "2014-04-01"

但这只是提示我输入一个名字,我得到了同样的错误。我也尝试为名称使用空值并得到以下结果:

Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

是否有更好的方法在 Azure 的资源组中创建新的 SQL 服务器?

提前致谢!

-- 更新:--

看起来您必须?使用 AzureServiceManagement Mode 创建一个 Sql服务器。您不能 link 将 Sql 服务器连接到资源组 (这会在未来出现吗?).

但是应该能够将Sql数据库添加到ResourceManager。所以我切换到 ServiceManagement 模式,创建了一个 Sql 服务器,切换回 ResourceManagement 模式,创建了一个资源组,我已经将我的数据库创建脚本更新为 link 服务器和资源组作为如下:

New-AzureResource –Name "TestDBName" –ResourceType Microsoft.Sql/servers/databases –ResourceGroup "TestGroupName" –Location "West US" -ApiVersion "2014-04-01" –ParentResource servers/[servername]

在许多在线示例中,我认为这是执行此操作的正确方法,但出于某种原因,我现在只收到一个一般性错误,没有提示我出了什么问题

New-AzureResource : : An error occurred while processing this request.

我希望能够在 Azure 上创建一个 Sql 服务器到现有资源组中,然后通过 PowerShell 将数据库添加到服务器。我将继续尝试对我的脚本进行不同的修改以使其正常工作,但我感觉好像碰壁了。非常感谢(并加分)任何能提供帮助的人!

下面是有关如何使用 PS cmdlet 创建 SQL 服务器资源的工作示例。问题是您缺少一些强制性参数,例如服务器的管理员凭据等等。

New-AzureResource -Name server1 -ResourceGroupName "Default-SQL-WestUS" -Location "West US" -ResourceType "Mirosoft.Sql/servers" -ApiVersion 2014-04-01 -PropertyObject @{administratorLogin="admin"; administratorLoginPassword="Passw@rd01"; version="12.0"}

您还可以使用带有 New-AzureResourceGroup cmdlet 的模板来创建服务器。请参阅下面的模板。

{
  "$schema": "http://schemas.management.azure.com/deploymentTemplate?api-version=2014-04-01-preview",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "administratorLogin": {
      "type": "string"
    },
    "administratorLoginPassword": {
      "type": "securestring"
    },
    "serverLocation": {
      "type": "string"
    },
    "serverName": {
        "type": "string"
    }
  },
  "resources": [
    {
      "apiVersion": "2014-04-01-preview",
      "location": "[parameters('serverLocation')]",
      "name": "[parameters('serverName')]",
      "properties": {
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
        "version": "12.0"

      },
      "resources": [
        {
          "apiVersion": "2014-04-01-preview",
          "dependsOn": [
            "[concat('Microsoft.Sql/servers/', parameters('serverName'))]"
          ],
          "location": "[parameters('serverLocation')]",
          "name": "AllowAllWindowsAzureIps",
          "properties": {
            "endIpAddress": "131.107.255.255",
            "startIpAddress": "131.107.0.0"
          },
          "type": "firewallrules"
        }
      ],
      "type": "Microsoft.Sql/servers"
    }
  ]
}

如果你正在使用Azure PowerShell 1.0 or later, and you have the AzureRM.Sql module installed, then you can use New-AzureRmSqlServer来实现这个。

有关详细信息,请参阅 official Azure docs

# Login to your Azure account
Add-AzureRmAccount

# Select an appropriate subscription
Select-AzureRmSubscription -SubscriptionId 4cac86b0-1e56-bbbb-aaaa-000000000000

# Create a new resource group and put a SQL server in it
# It will prompt you for the credentials that you want to set
# for the SQL administrator account
New-AzureRmResourceGroup -Name "yourresourcegroup" -Location "East US" | `
New-AzureRmSqlServer -ServerName "yoursqlservername" -Location "East US" `
-ServerVersion "12.0"

请注意,这不会配置任何防火墙规则或数据库。您将需要使用 New-AzureRmSqlServerFirewallRuleNew-AzureRmSqlDatabase 来做到这一点。