Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass parameters to PowerShell script while building from VSTS

I am running the below PowerShell Script to deploy csv file data into Azure table storage. But The below parameters are different for different environment in the azure.Suppose the below script can be deployed to any environment but the below parameters will be varied as per the environment.So I want to pass the below parameters to the script while running from the PowerShell task in VSTS.How to accomplish task.Please help me out on this.So

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

PowerShell Script:

   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

}
like image 313
PRAVEEN PDBR Avatar asked Nov 25 '25 09:11

PRAVEEN PDBR


1 Answers

You need to use the arguments text box to pass your parameters into the script (either inline or script file). enter image description here

Your script would need to look like this:

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
    }

Each of your variables will either need to be defaulted or passed in as arguments. In your example, you would look something like the following in the text box:

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

The box is expecting you input the arguments exactly as you would if you were calling the PowerShell script from the command line.

like image 184
tj-cappelletti Avatar answered Nov 28 '25 06:11

tj-cappelletti