如何在从VSTS构建时将参数传递给PowerShell脚本

时间:2017-12-05 07:29:51

标签: powershell azure-pipelines-build-task

我正在运行以下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

}

3 个答案:

答案 0 :(得分:2)

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

您的脚本需要如下所示:

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脚本时的方式输入参数。

答案 1 :(得分:1)

您的脚本中未使用某些参数,例如$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"

答案 2 :(得分:0)

您的脚本不带任何参数。您的脚本中有一个函数,它带有参数。脚本顶部的param块,在任何函数之外,将使您的脚本采用参数。

前:

param($A)

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

Foo -B $A