Aug 132020
 

今晚学习了一下使用PowerShell创建资源,我主要是用来创建

  1. 资源组
  2. 存储账号
  3. Data Factory
  4. key vault

这些资源如果你是手工来创建,一个头疼的地方就是区域,很多时候是要求区域一致才能使用。创建的时候,很容易搞错。

使用脚本创建,比较头疼的地方,就是服务的名字,存储账号,就只能使用小写字母和数字,对于key vault来说,你创建的key,删除后,会保留90天,你再次创建相同的名字,会失败。

假设我们需要创建Dev,QA,Prod,三个环境,那么基本运行下面的脚本就可以。

目前key vault 来存储密钥,你就只能是手工来完成。

参考文章

https://www.alexvolok.com/2020/adf-devops-generate-az-cli/

把下面两个文件创建出来,登录Azure门户,直接进入命令行,上传2个文件,直接运行

./Create-Environment.ps1

一切都应该很顺利,如果名字没有重复的。

adf.json 这个文件,你是不需要做任何的修改。你需要调整的是 Create-Environment.ps1  这个文件。就前面几行,看一下就明白了。

你还有一种办法覆盖脚本的变量。就是在运行的命令的时候,指定参数

# Development, Staging and Production:
.\Create-Environment.ps1 -EnvironmentName "DevOps2020" -Stage "Dev" -Location "West US"
.\Create-Environment.ps1 -EnvironmentName "DevOps2020" -Stage "Stg" -Location "West US"
.\Create-Environment.ps1 -EnvironmentName "DevOps2020" -Stage "Prod" -Location "West US"

大家可以看一下输出的效果

PS /home/chen> .\Create-Environment.ps1 -EnvironmentName "DevOps2020" -Stage "Dev" -Location "West US"

Name                           Value
----                           -----
ResourceGroupName              RG-DevOps2020-Dev
ADFName                        ADF-DevOps2020-Dev
KeyVaultName                   KV-DevOps2020-Dev
StorageName                    adlsdevops2020dev

Create a new resource group: RG-DevOps2020-Dev
Location    Name
----------  -----------------
westus      RG-DevOps2020-Dev
Creating a new key vault account: KV-DevOps2020-Dev
Location    Name               ResourceGroup
----------  -----------------  -----------------
westus      KV-DevOps2020-Dev  RG-DevOps2020-Dev
Creating a new storage account: adlsdevops2020dev
AccessTier    CreationTime                      EnableHttpsTrafficOnly    IsHnsEnabled    Kind       Location    Name               PrimaryLocation    ProvisioningState    ResourceGroup      StatusOfPrimary
------------  --------------------------------  ------------------------  --------------  ---------  ----------  -----------------  -----------------  -------------------  -----------------  -----------------
Hot           2020-08-12T18:42:27.314196+00:00  True                      True            StorageV2  westus      adlsdevops2020dev  westus             Succeeded            RG-DevOps2020-Dev  available
Name    ResourceGroup      State      Timestamp                         Mode
------  -----------------  ---------  --------------------------------  -----------
adf     RG-DevOps2020-Dev  Succeeded  2020-08-12T18:42:55.241918+00:00  Incremental

创建 adf.json

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": {
            "defaultValue": "myv2datafactory",
            "type": "String"
        },
        "location": {
            "defaultValue": "East US",
            "type": "String"
        },
        "apiVersion": {
            "defaultValue": "2018-06-01",
            "type": "String"
        }
    },
    "resources": [
        {
            "type": "Microsoft.DataFactory/factories",
            "apiVersion": "[parameters('apiVersion')]",
            "name": "[parameters('name')]",
            "location": "[parameters('location')]",
            "identity": {
                "type": "SystemAssigned"
            },
            "properties": {}
        }
    ]
}

创建 Create-Environment.ps1

# Step 1: Input parameters
param([String]$EnvironmentName = "ABB",`
      [String]$Stage = "Dev",`
      [String]$Location = "westeurope"`
      )
$OutputFormat = "table"  # other options: json | jsonc | yaml | tsv
# internal: assign resource names
$ResourceGroupName = "RG-$EnvironmentName-$Stage"
$ADFName = "ADF-$EnvironmentName-$Stage"
$KeyVaultName ="KV-$EnvironmentName-$Stage"
$StorageName = "adls$EnvironmentName$Stage".ToLower().Replace("-","")
Get-Variable ResourceGroupName, ADFName, KeyVaultName, StorageName | Format-Table



# Step 2: Create a Resource Group
if (-Not (az group list --query "[].{Name:name}" -o table).Contains($ResourceGroupName))
{
    "Create a new resource group: $ResourceGroupName"
    az group create --name $ResourceGroupName --location $Location --output $OutputFormat
}`
else
{
   "Resource Group: $ResourceGroupName already exists"
}


# Step 3a: Create a Key Vault
if (-Not (az keyvault list --resource-group $ResourceGroupName ` --query "[].{Name:name}" -o table).Contains($KeyVaultName))
{
    Write-Host "Creating a new key vault account: $KeyVaultName"
    az keyvault create `
       --location $Location `
       --name $KeyVaultName `
       --resource-group $ResourceGroupName `
       --output $OutputFormat
}`
else
{
    "Key Vault: Resource $KeyVaultName already exists"
}


# Step 3b: Create a Storage Account
if (-Not (az storage account list --resource-group $ResourceGroupName --query "[].{Name:name}" -o table).Contains($StorageName))
{
       Write-Host "Creating a new storage account: $StorageName"

       az storage account create `
       --name $StorageName `
        --resource-group $ResourceGroupName `
        --location $Location `
        --sku "Standard_LRS" `
        --kind "StorageV2" `
        --enable-hierarchical-namespace $true  `
        --output $OutputFormat
}`
else
{
    "Storage: Account $StorageName already exists"
}

# Step 4: Create a Data Factory
az deployment group create `
  --resource-group $ResourceGroupName `
  --template-file "./adf.json" `
  --parameters name=$ADFName location=$Location `
  --output $OutputFormat

  One Response to “PowerShell Create Resource”

  1. 牛逼,就怕bug出来

 Leave a Reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.