【问题标题】:How to Pass parameters to PowerShell script while building from VSTS从 VSTS 构建时如何将参数传递给 PowerShell 脚本
【发布时间】: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


【解决方案1】:

您需要使用参数文本框将参数传递到脚本(内联或脚本文件)。

您的脚本需要如下所示:

param (
    [string] $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
    }

您的每个变量都需要默认设置或作为参数传入。在您的示例中,您在文本框中将如下所示:

-subscriptionName "Tech Enabled Solutions" -$resourceGroupName "abc" -storageAccountName "defghi" -location "North Central US, South Central US" -StorageAccountKey "12345678

该框要求您完全按照从命令行调用 PowerShell 脚本时输入的参数。

【讨论】:

  • 我同意这一点。但我的问题是上述脚本中的行是否正确。如何将这一行格式化为参数 storageaccnam 和 staorageaccountkey 我从参数字段传递但是在这里我收到错误:##[error]New-AzureStorageContext:无法验证参数“StorageAccountName”的参数。参数为 null 或空。请检查这一行---->>> $ctx = New-AzureStorageContext $StorageAccountName $StorageAccountKey
  • ##[error]New-AzureStorageContext:无法验证参数“StorageAccountName”的参数。参数为 null 或为空。
  • 它没有从参数部分获取存储帐户名称。请检查我的脚本,如果可能的话,修改它需要的地方。我怀疑这一行:---> $ctx = New-AzureStorageContext $StorageAccountName $StorageAccountKey
  • 嗨 DJ,可以在我的脚本中进行这些更改,以便我了解需要在哪里进行更改。因为,我不知道电源外壳,我无法理解你的例子在说什么
  • 我已经更新了我的答案,包括你的脚本应该是什么样子。我建议阅读functioncmdlet 之间的区别,这将帮助您更好地了解此任务的工作原理。
【解决方案2】:

有些参数在你的脚本中没有用到,比如$subscriptionName$resourceGroupName,你可以查看是否需要。

参考此代码添加参数:

param(
[string] $subscriptionName,
 [string] $resourceGroupName,
 [string] $storageAccountName,
 [string] $location,
 [string] $StorageAccountKey
)
function Add-Entity()
{
 [CmdletBinding()]

 param
 (
 $table, 
 [string] $partitionKey, 
 [string] $RowKey, 
 [string] $Label_Usage,
 [string] $Label_Value,
 [string] $Usage_Location
)

 $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任务中指定参数的值(参数输入框)

-subscriptionName "Tech Enabled Solutions" -resourceGroupName "abc" -storageAccountName "defghi" -location "North Central US, South Central US" -StorageAccountKey "12345678"

【讨论】:

  • @非常感谢 MSFT。现在我可以插入我所有的实体。但是有什么方法可以在 VSTS 中硬编码这些参数?因为我有 7 个环境,所以每次我需要在 webapp 部署完成后部署表。所以我将通过从 VSTS siurce repo 发布所有脚本到发布定义来执行发布定义的脚本。所以在 web 应用部署之后我在发布定义中添加了这个 powershell 任务并将运行。但是任何硬编码所有这些参数的方法。请帮帮我。
  • @PRAVEEN 我建议在新的论坛主题中提出单独的问题。因此,论坛读者可以轻松识别问题和答案,因此请随时为新问题打开一个新线程。
  • 谢谢@MSFT。我打开了一个新线程。请帮助我@*.com/questions/47669594/…
  • 一个小问题,我看到参数列表中没有提到 $table 的类型。它不应该有类型吗?就我而言,我不知道类型名称,所以问。
【解决方案3】:

您的脚本不带任何参数。您的脚本中有一个带有参数的 函数。位于脚本顶部的param 块(在任何函数之外)将使您的脚本接受参数。

例如:

param($A)

function Foo {
param($B)
  Write-Output $B
}

Foo -B $A

【讨论】:

    最近更新 更多