Reputation: 583
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
}
Upvotes: 3
Views: 6493
Reputation: 33698
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"
Upvotes: 2
Reputation: 1864
You need to use the arguments text box to pass your parameters into the script (either inline or script file).
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.
Upvotes: 3
Reputation: 59020
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
Upvotes: 0