【发布时间】:2026-01-09 21:20:04
【问题描述】:
我正在运行以下 PowerShell 脚本以将 csv 文件数据部署到 Azure 表存储中。但是对于 azure 中的不同环境,下面的参数是不同的。假设下面的脚本可以部署到任何环境,但是下面的参数会根据环境而变化。所以我想在运行时将下面的参数传递给脚本VSTS中的PowerShell任务。如何完成任务。请帮我解决这个问题。所以
**$subscriptionName = "Tech Enabled Solutions"
$resourceGroupName = "abc"
$storageAccountName = "defghi"
$location = "North Central US, South Central US"
$StorageAccountKey = "12345678"**
PowerShell 脚本:
function Add-Entity()
{
[CmdletBinding()]
param
(
$table,
[string] $partitionKey,
[string] $RowKey,
[string] $Label_Usage,
[string] $Label_Value,
[string] $Usage_Location,
[string] $subscriptionName,
[string] $resourceGroupName,
[string] $storageAccountName,
[string] $location,
[string] $StorageAccountKey
)
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("Label_Value",$Label_Value)
$entity.Properties.Add("Label_Usage",$Label_Usage)
$entity.Properties.Add("Usage_Location",$Usage_Location)
$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::InsertOrReplace($entity))
}
$tableName = "sampletable"
# Get a storage context
$ctx = New-AzureStorageContext $StorageAccountName $StorageAccountKey
# Get a reference to the table
$table = Get-AzureStorageTable -Name $tableName -Context $ctx -ErrorAction Ignore
$csv = Import-CSV "d:\a\1\s\DeploymentScripts\sampletable.csv"
ForEach ($line in $csv)
{
Add-Entity -Table $table -partitionKey $line.partitionkey -rowKey $line.RowKey -Label_Usage $line.Label_Usage -Label_Value $line.Label_Value -Usage_Location $line.Usage_Location
}
【问题讨论】:
-
powershell 任务上有一个“参数”字段,您可以在其中添加参数。你的脚本没有声明任何参数,所以你可能想改变它
-
我可以有语法将上面提到的参数发送到 VSTS 参数列表中的 powershell 脚本吗?
-
-parameterName $parameterValue。但如前所述,您的脚本尚未声明参数
-
嗨 DJ,我不明白你在说什么。到上面的脚本我想传递参数,如订阅名称,storageacc 名称,keyresource 组名......将这些参数发送到上面的脚本,如您在参数字段下说,我将通过 -subscriptionname abcd ,-storageaccname def,-key 123456 ...但是我如何在脚本中声明这些参数以接受来自参数字段的参数列表。请告诉我或修改我的脚本并在可能的情况下发送给我,在传递参数时,如何从参数字段中分离/发送多个参数。请帮助我。
-
嗨 DJ,现在我已经更新了我的脚本。请查看脚本。我正在传递参数,例如 -subscriptionName "my subscription" -resourceGroupName "praveen" -storageAccountName "pdbr" -location "印度”-StorageAccountKey “123456”....但它失败并出现以下错误:##[error]New-AzureStorageContext:无法验证参数“StorageAccountName”的参数。参数为 null 或空。请看一下并帮助我。上述脚本是否正确定义了参数。如果没有,请更新我的脚本。
标签: powershell azure-pipelines-build-task