3

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

}
7
  • There is an "arguments"-field on the powershell task where you can add your parameters. Your script doesn't declare any parameters so sou might want to change that Commented Dec 5, 2017 at 10:20
  • Can I have the syntax to send the above parameters which I mentioned to the powershell script in VSTS Argument list ? Commented Dec 5, 2017 at 10:25
  • -parameterName $parameterValue. But as is said, your script does not yet declare parameters Commented Dec 5, 2017 at 10:29
  • Hi DJ, I am not understanding what you are saying.To The above script I want to pass parameters like subscriptionname,storageacc name,keyresource groupname...To send these parameters to the above script ,as you said under argument field,I will pass -subscriptionname abcd ,-storageaccname def,-key 123456...But how can I declare these parameters in the script to accept the arguments list from the argument filed.Please tell me or modify my script and send to me if possible and while passing arguments,how can I separate/send multiple arguments fro m the argument field .Please help me. Commented Dec 5, 2017 at 11:00
  • Hi DJ, Now I have updated my script.Please look at the script.And I am passing the arguments like -subscriptionName "my subscription" -resourceGroupName "praveen" -storageAccountName "pdbr" -location "india" -StorageAccountKey "123456"....But its failing with the below error: ##[error]New-AzureStorageContext : Cannot validate argument on parameter 'StorageAccountName'. The argument is null or empty. Please take a look and help me on this.Whether the above script is defined the parameters correctly or not.if not please update my script. Commented Dec 5, 2017 at 12:02

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I am agree to this.But my question is whether the line in the above script is correct or not.How can I format this line as the parameters storageaccnam and staorageaccountkey I am passing from the argument field but here i am getting the error: ##[error]New-AzureStorageContext : Cannot validate argument on parameter 'StorageAccountName'. The argument is null or empty. Please check this line---->>> $ctx = New-AzureStorageContext $StorageAccountName $StorageAccountKey <--- whether I correctly written in the script correct or not.
##[error]New-AzureStorageContext : Cannot validate argument on parameter 'StorageAccountName'. The argument is null or empty.
Its not taking storage account name from the argument section.please check my script and if possible modify where it required.I am suspecting at this line:---> $ctx = New-AzureStorageContext $StorageAccountName $StorageAccountKey
Hi DJ, Is is possible to make those changes in my script so that I will understand where exactly the changes needs to be happened. Since, i am not aware of power shell,I am not able to understand what your example saying
I've updated my answer to include what your script should look like. I would suggest reading up on the differences between function and cmdlet, it will help you to better understand how this task works.
|
2

Some parameters are not used in your script, such as $subscriptionName, $resourceGroupName, you can check whether they are needed.

Refer to this code to add parameters:

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

}

Specify the parameters' value in PowerShell task (Arguments input box)

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

4 Comments

@Thanks a lot MSFT. Now I could able to insert all my entities.But Is there any way that I can hard code these arguments in VSTS ? Because I have 7 environments ,so everytime I need to deploy tables after the webapp deployment is done.So I will be executing the scripts at the release definition by publishing all the scripts from the VSTS siurce repo to release definition.So After web app deploy I am adding this powershell task in release definition and will run.But any way to hard code all these arguments.Please help me out.
@PRAVEEN I'd suggest asking separate questions in new forum threads. Thus, forum readers may recognize questions and answers easily, so feel free to open a new thread for the new issue.
Thanks @MSFT.I have opened a new thread.Please help me on this @ stackoverflow.com/questions/47669594/…
A side question, I see the type for $table isn't mentioned in the parameter list. Should it not have a type? From my side, I don't know the type name, so asking.
0

Your script doesn't take any parameters. You have a function in your script that takes parameters. A param block at the top of your script, outside of any functions, will make your script take parameters.

Ex:

param($A)

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

Foo -B $A

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.