我有一个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: 为每个环境创建多个Powershell脚本 创建了1个基于菜单的powershell脚本,该脚本调用其他脚本并执行如下操作:为Dev选择1,为UAt选择2,为PROD选择3,此方法有效,但无效。
方法2:
我想合并所有脚本,并且只为所有环境使用一个脚本,并且基于select应该允许我创建存储帐户。只有订阅和资源组更改,其余Powershell的所有结构均保持不变。
我尝试使用GET函数Commandlet,它选择了但仍会引发错误
[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
输出:
如果查看资源组,则无法提供资源组的选择选项。
订阅:<它返回订阅名称>
ResourceGroup:Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup Microsoft.Azure.Commands.ResourceManager.Cmd
lets.SdkModels.PSResourceGroup Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup Microsoft.Azure.Commands.Res
ourceManager.Cmdlets.SdkModels.PSResourceGroup
答案 0 :(得分:0)
您提出的是一种有趣的方法。我可能会使用一个输入参数来定义要在哪个环境中完成工作,然后有一个条件块为该环境设置动态变量。每个环境都会有一些重复的初始化代码,但是主代码块仍然是统一的。