2017-12-05 10 views
0

아래 PowerShell 스크립트를 실행하여 Azure 테이블 저장소에 CSV 파일 데이터를 배포합니다. 하지만 아래의 매개 변수는 하늘빛의 환경에 따라 다릅니다. 아래의 스크립트는 모든 환경에 배포 할 수 있지만 환경에 따라 아래 매개 변수가 달라질 수 있습니다. 스크립트에서 실행하는 동안 아래 매개 변수를 전달하고 싶습니다. VSTS.How에서 PowerShell을 작업은 this.SoVSTS에서 빌드하는 동안 PowerShell 스크립트에 매개 변수를 전달하는 방법

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

PowerShell을 스크립트에 나를 도와 task.Please 달성하기 :

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 

} 
+0

지정 여기서 매개 변수를 추가 할 수 있습니다. 스크립트가 매개 변수를 선언하지 않아서 sou가 변경하려고 할 수 있습니다. –

+0

위에서 언급 한 매개 변수를 VSTS 인수 목록의 powershell 스크립트로 보낼 수 있습니까? – PRAVEEN

+0

-parameterName $ parameterValue 그러나 당신의 스크립트는 아직 매개 변수를 선언하지 않았습니다. –

답변

0

일부 매개 변수는 같은 $subscriptionName, $resourceGroupName로, 당신은 그들이 필요 여부를 확인할 수 스크립트에서 사용되지 않습니다.

매개 변수를 추가하려면이 코드를 참조하십시오

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 

} 

하면 PowerShell을 작업에 "인자"-field가있다 PowerShell을 작업 (인수 입력 상자)의 매개 변수 '값을

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

@Thanks 많이 MSFT. 이제 모든 엔티티를 삽입 할 수있었습니다. 그러나 VSTS에서 이러한 인수를 하드 코딩 할 수있는 방법이 있습니까? 웹 애플리케이션 배포 후 테이블을 배포해야 할 때마다 7 가지 환경이 있으므로 매번 VSTS siurce 저장소의 모든 스크립트를 게시하여 정의를 릴리스 할 때 스크립트를 실행합니다. 웹 응용 프로그램 배포 후 릴리스 정의에서이 powershell 태스크를 추가하고 실행할 것입니다. 그러나 이러한 모든 인수를 하드 코딩하는 방법은 없습니다. 제발 도와주십시오. – PRAVEEN

+0

@PRAVEEN 새 포럼 스레드에서 별도의 질문을하는 것이 좋습니다. 따라서 포럼 독자는 질문과 답변을 쉽게 인식 할 수 있으므로 새로운 문제에 대한 새로운 글을 자유롭게 열어보십시오. –

+0

Thanks @ MSFT.I 새 스레드를 열었습니다.이 페이지에서 @ 도와주세요. https://stackoverflow.com/questions/47669594/what-is-the-best-way-to-hardcode-arguments-to-the- PowerShell-Script-in-Vsts-Rele – PRAVEEN

1

당신은 스크립트로 매개 변수를 전달하는 인수 텍스트 상자를 사용합니다 (인라인 또는 스크립트 파일). 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 
    } 

당신의 변수의 각 기본값 또는 인수로 전달해야합니다 중 하나. 귀하의 예제에서는 텍스트 상자에 다음과 같이 보일 것입니다 : 당신이 명령 줄에서 PowerShell 스크립트를 호출 한 경우처럼 상자가 인수 정확하게에게 입력을 기대하고있다

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

.

+0

나는이 의견에 동의한다. 그러나 위의 스크립트의 행이 맞는지 아닌지에 대한 나의 질문이다.이 행을 인수 storageaccnam 및 staorageaccountkey로 형식화 할 수있는 방법은 인수 필드에서 전달하지만 여기에서는 오류가 발생한다. ## [오류] New-AzureStorageContext : 매개 변수 'StorageAccountName'에 대한 인수의 유효성을 검사 할 수 없습니다. 인수가 널이거나 비어 있습니다. 이 줄을 확인하십시오 ---- >>> $ ctx = New-AzureStorageContext $ StorageAccountName $ StorageAccountKey <--- 스크립트에 올바르게 쓰여 있는지 여부. – PRAVEEN

+0

## [오류] New-AzureStorageContext : 매개 변수 'StorageAccountName'에 대한 인수의 유효성을 검사 할 수 없습니다. 인수가 널이거나 비어 있습니다. – PRAVEEN

+0

그것의 스토리지 계정 이름을 인수 section.please에서 내 스크립트를 확인하고 가능한 경우 수정하십시오. 나는이 라인에서 의심 스럽네요 : ---> $ ctx = New-AzureStorageContext $ StorageAccountName $ StorageAccountKey – PRAVEEN

0

스크립트는 매개 변수를 사용하지 않습니다. 매개 변수를 취하는 스크립트에는 함수이 있습니다. 스크립트의 맨 위에있는 어떤 함수 바깥에있는 param 블록은 스크립트가 매개 변수를 취하도록 만듭니다.

예 :

param($A) 

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

Foo -B $A