azure - 在单个 Azure 存储脚本中为各种环境传递多个参数
问题描述
我有一个 powershell 脚本,它为给定的订阅创建存储和 blob 帐户,效果很好。订阅名称,资源组随着 DEV、UAT、PROD 等不同环境不断变化
我的模板/代码的结构:
param(
[string] $subscriptionName ="ABC",
[string] $resourceGroupName = "XYZ",
[string] $resourceGroupLocation ="westus",
[string] $templateFilePath = "template.json",
[string] $parametersFilePath = "parameters.json"
)
Function RegisterRP {
Param(
[string]$ResourceProviderNamespace
)
Write-Host "Registering resource provider '$ResourceProviderNamespace'";
Register-AzureRmResourceProvider -ProviderNamespace $ResourceProviderNamespace;
}
$ErrorActionPreference = "Stop"
$confirmExecution = Read-Host -Prompt "Hit Enter to continue."
if($confirmExecution -ne '') {
Write-Host "Script was stopped by user." -ForegroundColor Yellow
exit
}
# sign in
Write-Host "Logging in...";
Login-AzureRmAccount;
# select subscription
Write-Host "Selecting subscription '$subscriptionName'";
Select-AzureRmSubscription -SubscriptionName $subscriptionName;
# Register RPs
$resourceProviders = @("microsoft.storage");
if($resourceProviders.length) {
Write-Host "Registering resource providers"
foreach($resourceProvider in $resourceProviders) {
RegisterRP($resourceProvider);
}
}
#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
Write-Host "Resource group '$resourceGroupName' does not exist. To create a new resource group, please enter a location.";
if(!$resourceGroupLocation) {
$resourceGroupLocation = Read-Host "resourceGroupLocation";
}
Write-Host "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
}
else{
Write-Host "Using existing resource group '$resourceGroupName'";
}
# Start the deployment
Write-Host "Starting deployment...";
if(Test-Path $parametersFilePath) {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -Name $deploymentName -TemplateFile $templateFilePath -TemplateParameterFile $parametersFilePath -storageAccounts_name $storageAccountName
} else {
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -Name $deploymentName -TemplateFile $templateFilePath; -storageAccounts_name $storageAccountName
}
方法 1:为每个 denvironment 创建多个 powershell 脚本 创建 1 个基于菜单的 powershell 脚本,该脚本调用其他脚本并执行如下:为 Dev 选择 1,为 UAt 选择 2,为 PROD 选择 3,这种方法有效但无效。
方法2:
我想组合所有脚本,只为所有环境提供一个脚本,并且基于 select 应该允许我创建存储帐户。只有订阅和资源组更改才能使 powershell 的所有结构保持不变。
我尝试使用 GET 函数命令行开关,它选择但仍然抛出错误
[string] $subscriptionName = Get-AzureSubscription,
[string] $resourceGroupName = Get-AzureRmLocation,
如果我尝试使用基于数组的方法来使用它,例如传递下面的值,我将无法理解如何将这些基于数组的值传递给代码并让它工作。
$environment=@('DEV','TEST','QA','PROD')
$resourcegroupname = @('test','test1','test2','test3')
$subscriptionName = @('devsub1','devsub2','test3','prod4')
我正在尝试使用以下方法调用函数:
$environment[0]
$subscriptionName[0]
如果我单独执行它,它会返回如下值,但是如何将这些值传递给我的脚本以创建存储帐户?
DEV
devsub1
如果有人之前遇到过此类情况,并且您可以帮助更改上述代码并提供经过测试的代码,这将是非常有帮助的,请寻求专家帮助。
方法 3:
$subscription = @(Get-AzureRmSubscription)
$resourcegroup = @(Get-AzureRmResourceGroup)
$Environment = @('DEV','TEST','QA','PROD')
$resourceGroupName = $resourcegroup | Out-GridView -PassThru -Title 'Pick the environment'
$subscriptionName = $subscription | Out-GridView -PassThru -Title 'Pick the subscription'
Write-Host "Subscription:" $subscriptionName
Write-Host "ResourceGroup:" $resourcegroup
输出:
如果您查看资源组,它无法为资源组提供选择选项。
订阅:<它返回订阅名称>
资源组:Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup Microsoft.Azure.Commands.ResourceManager.Cmd let.SdkModels.PSResourceGroup Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels。 PSResourceGroup Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup
解决方案
What you are proposing is an interesting approach. I would likely an input parameter that defines which environment the work will be done in, and then have a conditional block that sets the dynamic variables for that environment. There would be some duplication of initialization code for each environment, but the main code block would still be unified.
推荐阅读
- android - 应用程序未启动
- java - 打包为 EXE 的 java 程序出现“错误调用方法”和“无法启动 JVM”错误
- php - 在mysqli数组php中查找最接近的值
- python - 具有可训练权重的自定义/Lambda 层
- css - 如何使输入/文本占据 Bootstrap 4 中 div 的整个宽度?
- android - Android:画布 drawBitmap 不起作用
- embedded - For SPI communication on the STM32F407-Discovery board, what is the implication of setting the software slave management bit?
- wpf - RichTextBox in DataTemplate not filling
- emacs - Aquamacs 中的 Agda 模式
- java - Java - 使用在同一台机器的不同端口运行的 Rest 服务的最佳方式是什么?