gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:51:46 26 4
gpt4 key购买 nike

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

}

最佳答案

您需要使用参数文本框将参数传递到脚本(内联或脚本文件)中。 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 脚本时完全一样。

关于powershell - 从 VSTS 构建时如何将参数传递给 PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com