我尝试在部署资源组时将标签分配给资源组。我还使用定义了变量的 ini 文件。
这是我的两个函数,读取 ini 文件并创建资源组
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=(.*)" # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
function Create-ResourceGroup {
param (
[string] $rgName,
[string] $saName,
[hashtable] $tags
)
az group create --name $rgName --location "westeurope" --tags $tags
}
我有一个单独的文件,我在其中调用此函数,如下所示:
. ".\functions.ps1"
# Get variables from the INI file
$iniFilePath = "../configuration/variables.ini"
$iniContent = Get-IniContent $iniFilePath
# Get today's date
$today = Get-Date -Format yyyy-MM-dd
$trainerName = $iniContent["project"]["trainer"]
$projectName = $iniContent["project"]["name"]
$tags = @{
"CreationDate" = $today
"Trainer" = $trainerName
"Project" = $projectName
}
# Iterate over the trainees section
foreach ($key in $iniContent["trainees"].Keys) {
# Get the trainee name
$traineeName = $iniContent["trainees"][$key]
$names = $traineeName -split ' '
$lastname = ($traineeName -split ' ')[1].Substring(0, [Math]::Min(5, $traineeName.IndexOf(" "))).ToLower()
# RG name
$resourceGroupName = "rg-academy-databricks-$lastname"
# SA name
$storageName = "stacademy$lastname"
# DB name
$databricksName = "dbs-academy-course-$lastname-001"
# Call function to create resources
Create-ResourceGroup -rgName $resourceGroupName -saName $storageName -tags $tags
}
代码不会失败,但我定义的标签没有通过,而是我看到 System.Collections.Hashtable :如下图所示。我确实尝试了泼溅并替换--tags $tags
为--tags @tags
但确实抛出了错误Missing an argument for parameter 'tags'