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

}
PRAVEEN PDBR
  • 423
  • 1
  • 9
  • 25
  • 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 – D.J. Dec 05 '17 at 10:20
  • Can I have the syntax to send the above parameters which I mentioned to the powershell script in VSTS Argument list ? – PRAVEEN PDBR Dec 05 '17 at 10:25
  • -parameterName $parameterValue. But as is said, your script does not yet declare parameters – D.J. Dec 05 '17 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. – PRAVEEN PDBR Dec 05 '17 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. – PRAVEEN PDBR Dec 05 '17 at 12:02
  • you added parameters to the function in the script but not to the script – D.J. Dec 05 '17 at 12:14
  • How can I add parameters to the script?Please help me with the updated script.I am very new to PowerShell and VSTS .So please help me and understand. – PRAVEEN PDBR Dec 05 '17 at 12:41

3 Answers3

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.

Community
  • 1
  • 1
tj-cappelletti
  • 1,774
  • 12
  • 19
  • 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. – PRAVEEN PDBR Dec 05 '17 at 13:36
  • ##[error]New-AzureStorageContext : Cannot validate argument on parameter 'StorageAccountName'. The argument is null or empty. – PRAVEEN PDBR Dec 05 '17 at 13:36
  • 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 – PRAVEEN PDBR Dec 05 '17 at 13:37
  • 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 – PRAVEEN PDBR Dec 05 '17 at 14:45
  • So kindly, help me on this. At that particular line where I mentioned in the script, it's not taking the storage account name and storage key details and throwing the error. So please try to update the script according to my script please update. So that I will come to know – PRAVEEN PDBR Dec 05 '17 at 14:47
  • 1
    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. – tj-cappelletti Dec 05 '17 at 15:18
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"
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • @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 PDBR Dec 06 '17 at 07:17
  • @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. – starian chen-MSFT Dec 06 '17 at 07:46
  • Thanks @MSFT.I have opened a new thread.Please help me on this @ https://stackoverflow.com/questions/47669594/what-is-the-best-way-to-hardcode-arguments-to-the-powershell-script-in-vsts-rele – PRAVEEN PDBR Dec 06 '17 at 08:15
  • 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. – Shamim Hafiz - MSFT Nov 07 '18 at 01:03
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
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120