I want to create a Pipeline that is copying files from one Azure subscription to the next one in one Azure DevOps Pipeline.
For this I have setup my pipeline yaml and my two powershell scripts where the parameters are created via logging commands and passed to the second powershell task as arguments.
Pipeline.yaml
trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- task: AzurePowerShell@5
inputs:
ScriptType: filePath
ScriptPath: '$(System.DefaultWorkingDirectory)/Task.ps1'
azureSubscription: 'Sub1'
azurePowerShellVersion: latestVersion
displayName: 'Task 1'
- task: AzurePowerShell@5
inputs:
ScriptType: filePath
ScriptPath: '$(System.DefaultWorkingDirectory)/Task2.ps1'
azureSubscription: 'Sub2'
azurePowerShellVersion: latestVersion
ScriptArguments: >
-DestAcc "$(DestAcc)"
-DestKey "$(DestKey)"
-DestContext "$(DestContext)"
-DestStorageContainer "$(DestStorageContainer)"
displayName: 'Task 2'
Task1.ps1
$location="germanywestcentral"
$rg="rg1"
$StorageAccountName="storage1"
$StorageSku="Standard_ZRS"
$StorageKind="StorageV2"
$StorageContainer="blobcontainer"
$StorageAccount=Get-AzStorageAccount -ResourceGroupName $rg -Name $StorageAccountName
$storageAccountKey=(Get-AzStorageAccountKey -ResourceGroupName $rg -Name $StorageAccountName).Value[0]
$StorageAccountContext=$StorageAccount.Context
Write-Host "##vso[task.setvariable variable=DestStorageContainer;]$StorageContainer"
Write-Host "##vso[task.setvariable variable=DestKey;]$storageAccountKey"
Write-Host "##vso[task.setvariable variable=DestContext;]$StorageAccountContext"
Write-Host "##vso[task.setvariable variable=DestAcc;]$StorageAccountName"
Task2.ps1
param (
[string] $DestStorageContainer,
[string] $DestUrl,
[string] $DestKey,
[object] $DestContext,
[string] $DestAcc
)
$subscriptionId=(Get-AzSubscription).Id
$resourceGroupName ="rg2"
$diskName = "DD"
$DestFileName="Filename"
$DestAcc=$DestAcc
$DestContainer=$DestStorageContainer
$DestKey=$DestKey
$DestContext=$DestContext
$sasExpiryDuration="3600"
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $DestContainer -DestContext $DestContext -DestBlob $DestFileName
Now the problem is that the parameter $DestContext will be formatted to type string instead of being the type "Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext". The whole error message in the pipeline is the following:
Cannot convert the "Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext" value of type "System.String" to type "Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext".
I then started to work on the problem and installed the "Az.Stroage" module to the runner while execution the powershell task. But this did not solve the issue as I am setting the type of the object inside the PARAM section and not after the import of "Az.Storage" module.
Does anybody know if this is possible? Is there a way that I can pass the Azure Storage context object to the next task via task variable? Or change the type from string to the Azure Storage context type?
Thank you!
CodePudding user response:
Test with the same PowerShell and reproduce the same issue.
When you set the value in Pipeline variable, it will show as String type.
I'm afraid there is no method to keep the object form in a variable.
To meet your requirement, I suggest that you can add the command in Task2.ps1 to get the context in the same task.
New-AzStorageContext -StorageAccountName "ContosoGeneral" -StorageAccountKey "< Storage Key for ContosoGeneral ends with == >"
Here is an example:
param (
[string] $DestStorageContainer,
[string] $DestUrl,
[string] $DestKey,
[object] $DestContext,
[string] $DestAcc
)
$subscriptionId=(Get-AzSubscription).Id
$resourceGroupName ="rg2"
$diskName = "DD"
$DestFileName="Filename"
$DestAcc=$DestAcc
$DestContainer=$DestStorageContainer
$DestKey=$DestKey
$sasExpiryDuration="3600"
$Context = New-AzStorageContext -StorageAccountName "ContosoGeneral" -StorageAccountKey "< Storage Key for ContosoGeneral ends with == >"
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $DestContainer -DestContext $DestContext -DestBlob $Context
You can use the Storage Account key to access the storage account in different subscription.