📘 Azure PowerShell 部署练习笔记
🧩 模板参数设定与变量构建
1 2 3
| $templateFile = "azuredeploy.json" $today = Get-Date -Format "MM-dd-yyyy" $deploymentName = "addstorage-" + "$today"
|
🚀 执行部署命令
1 2 3 4
| New-AzResourceGroupDeployment ` -Name $deploymentName ` -ResourceGroupName "<你的资源组名>" ` -TemplateFile $templateFile
|
如需传入模板参数:
✅ 方法一:使用参数对象传参
1 2 3 4 5 6 7 8 9
| $parameters = @{ storageName = "storage00001" }
New-AzResourceGroupDeployment ` -Name $deploymentName ` -ResourceGroupName "<你的资源组名>" ` -TemplateFile $templateFile ` -TemplateParameterObject $parameters
|
✅ 方法二:使用参数文件传参
创建参数文件 azuredeploy.parameters.json:
1 2 3 4 5 6 7 8 9
| { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "storageName": { "value": "storage00001" } } }
|
调用部署命令:
1 2 3 4 5
| New-AzResourceGroupDeployment ` -Name $deploymentName ` -ResourceGroupName "<你的资源组名>" ` -TemplateFile $templateFile ` -TemplateParameterFile "azuredeploy.parameters.json"
|
🛠️ 常见错误与解决方案
| 错误代码 |
原因说明 |
解决办法 |
StorageAccountAlreadyTaken |
存储账户名称已被占用 |
修改名称或使用 uniqueString()[^1] |
AccountNameInvalid |
存储名称非法,如含 - 或超过 24 个字符 |
仅用小写字母+数字,3~24 字符 |
AuthorizationFailed |
当前账户权限不足 |
检查角色分配或重新登录 |
InvalidTemplateDeployment |
模板结构或参数验证失败 |
检查模板 JSON 格式与参数引用 |
NoRegisteredProviderFound |
API版本或资源位置不被支持 |
更新 apiVersion 或更改区域 |
📎 模板中参数使用方式(ARM JSON)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| "parameters": { "storageName": { "type": "string", "minLength": 3, "maxLength": 24 } }, "resources": [{ "name": "[parameters('storageName')]", "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2021-04-01", "location": "[resourceGroup().location]", "sku": { "name": "Standard_LRS" }, "kind": "StorageV2", "properties": {} }], "outputs": { "storageAccountName": { "type": "string", "value": "[parameters('storageName')]" } }
|
🔡 PowerShell 特殊符号说明
反引号 `(行续写)
- 用于行续写,表示当前命令未结束;
- 必须放在行尾,后面不能有空格 。
1 2 3
| New-AzResourceGroupDeployment ` -Name "demo" ` -TemplateFile "file.json"
|
"双引号" vs '单引号'
| 类型 |
是否解析变量 |
示例 |
"双引号" |
✅ |
"Hello $name" => 变量替换 |
'单引号' |
❌ |
'Hello $name' => 原样输出 |
📦 SKU 是什么?
- SKU(Stock Keeping Unit)是唯一标识产品的编码,在 Azure 中也表示服务套餐;
- 示例:存储账户中的 SKU 是
"Standard_LRS" 表示标准冗余存储。
🎞️ 什么是硬解码?
- 使用 GPU/VPU 等硬件进行视频或音频解码;
- 优点:高效、节能、减轻 CPU 负担;
- 适合播放高清视频或嵌入式设备实时解码。
[^1]: uniqueString() 是 ARM 模板内置函数,可生成唯一字符串,避免资源命名冲突。