Search code examples
powershellazure-devopsazure-rm-template

How to provide file path of JSON file kept in azure devops git repository to PowerShell script.Getting file not found exception


enter image description hereIm trying to run this PowerShell script through azure release pipeline. Azure provides json file and PowerShell script to deploy a linked service or dataset of data factory. But the path of the json file is assumed is of local computer. But while executing the same through azure Devops release pipeline, how to provide the json file path in PowerShell script and execute the Devops release pipeline.

Powershell script -

Set-AzDataFactoryV2LinkedService -DataFactoryName "DemoADF" -ResourceGroupName "DemoRG" -Name "AzureStorageLinkedService" -DefinitionFile "./LinkedService.json"

LinkedService.json file -

{ "name": "AzureStorageLinkedService", "properties": { "type": "AzureStorage", "typeProperties": { "connectionString": "ConnectionStringDetails" }, "connectVia": { "referenceName": "AutoResolveIntegrationRuntime", "type": "IntegrationRuntimeReference" } } }

Tried using - $env:(System.DefaultWorkingDirectory)/LinkedService.json but not working

Azure DevOps Release pipeline error -

2020-07-28T04:23:31.1358415Z ##[debug]Error record:
2020-07-28T04:23:31.2243264Z ##[debug]Set-AzDataFactoryV2LinkedService : ./LinkedService.json
2020-07-28T04:23:31.2263866Z ##[debug]At D:\a\r1\a\LS_adf\ykfactory\AllADFDeploymentTasks\LinkedService.ps1:1 char:1
2020-07-28T04:23:31.2280510Z ##[debug]+ Set-AzDataFactoryV2LinkedService -DataFactoryName "DataFlowADFVNet" `
2020-07-28T04:23:31.2312596Z ##[debug]+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-07-28T04:23:31.2342558Z ##[debug]    + CategoryInfo          : CloseError: (:) [Set-AzDataFactoryV2LinkedService], FileNotFoundException
2020-07-28T04:23:31.2361626Z ##[debug]    + FullyQualifiedErrorId : Microsoft.Azure.Commands.DataFactoryV2.SetAzureDataFactoryLinkedServiceCommand
2020-07-28T04:23:31.2379154Z ##[debug] 
2020-07-28T04:23:31.2415190Z ##[debug]Script stack trace:
2020-07-28T04:23:31.2479403Z ##[debug]at <ScriptBlock>, D:\a\r1\a\LS_adf\ykfactory\AllADFDeploymentTasks\LinkedService.ps1: line 1
2020-07-28T04:23:31.2532900Z ##[debug]at <ScriptBlock>, D:\a\_temp\a652e51d-54ce-438d-b59a-d8b118516fe9.ps1: line 41
2020-07-28T04:23:31.2553826Z ##[debug]at <ScriptBlock>, <No file>: line 1
2020-07-28T04:23:31.2594489Z ##[debug]Exception:
**2020-07-28T04:23:31.2681432Z ##[debug]S*ystem.IO.**FileNotFoundException**: ./LinkedService.json***
2020-07-28T04:23:31.2716695Z ##[debug]   at Microsoft.Azure.Commands.DataFactoryV2.DataFactoryClient.ReadJsonFileContent(String path)
2020-07-28T04:23:31.2740090Z ##[debug]   at Microsoft.Azure.Commands.DataFactoryV2.SetAzureDataFactoryLinkedServiceCommand.ExecuteCmdlet()
2020-07-28T04:23:31.2760357Z ##[debug]   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
2020-07-28T04:23:31.3196351Z ##[error]./LinkedService.json
2020-07-28T04:23:31.3212488Z ##[debug]Processed: ##vso[task.logissue type=error]./LinkedService.json
2020-07-28T04:23:32.1091196Z ##[debug]Exit code: 1
2020-07-28T04:23:32.1091982Z ##[debug]Leaving Invoke-VstsTool.
2020-07-28T04:23:32.1093065Z ##[error]PowerShell exited with code '1'.
2020-07-28T04:23:32.1094438Z ##[debug]Processed: ##vso[task.logissue type=error]PowerShell exited with code '1'.
2020-07-28T04:23:32.1106224Z ##[debug]Processed: ##vso[task.complete result=Failed]Error detected
2020-07-28T04:23:32.1107726Z ##[debug]Loading module from path 'D:\a\_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\5.173.0\ps_modules\VstsAzureHelpers_\VstsAzureHelpers_.psm1'.
2020-07-28T04:23:32.1108852Z ##[debug]$OVER[enter image description here][1]

Latest Error - enter image description here 2020-07-28T14:12:59.1777082Z ##[debug]Error record: 2020-07-28T14:12:59.2606789Z ##[debug]Set-AzDataFactoryV2LinkedService : D:\a\r1\a\LinkedService.json 2020-07-28T14:12:59.2614645Z ##[debug]At D:\a\r1\a\LS_adf\ykfactory\AllADFDeploymentTasks\LinkedService.ps1:7 char:1 2020-07-28T14:12:59.2641183Z ##[debug]+ Set-AzDataFactoryV2LinkedService -DataFactoryName "DataFlowADFVNet" ` 2020-07-28T14:12:59.2665039Z ##[debug]+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2020-07-28T14:12:59.2686771Z ##[debug] + CategoryInfo : CloseError: (:) [Set-AzDataFactoryV2LinkedService], FileNotFoundException 2020-07-28T14:12:59.2731339Z ##[debug] + FullyQualifiedErrorId : Microsoft.Azure.Commands.DataFactoryV2.SetAzureDataFactoryLinkedServiceCommand 2020-07-28T14:12:59.2746815Z ##[debug] 2020-07-28T14:12:59.2791357Z ##[debug]Script stack trace: 2020-07-28T14:12:59.2841187Z ##[debug]at , D:\a\r1\a\LS_adf\ykfactory\AllADFDeploymentTasks\LinkedService.ps1: line 7 2020-07-28T14:12:59.2894685Z ##[debug]at , D:\a_temp\773f2897-4445-4797-b8c4-62e1fbc3c46a.ps1: line 41 2020-07-28T14:12:59.2915479Z ##[debug]at , : line 1 2020-07-28T14:12:59.2931046Z ##[debug]Exception: 2020-07-28T14:12:59.3013273Z ##[debug]System.IO.FileNotFoundException: D:\a\r1\a\LinkedService.json 2020-07-28T14:12:59.3039882Z ##[debug] at Microsoft.Azure.Commands.DataFactoryV2.DataFactoryClient.ReadJsonFileContent(String path) 2020-07-28T14:12:59.3062765Z ##[debug] at Microsoft.Azure.Commands.DataFactoryV2.SetAzureDataFactoryLinkedServiceCommand.ExecuteCmdlet() 2020-07-28T14:12:59.3082130Z ##[debug] at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord() 2020-07-28T14:12:59.3469116Z ##[error]D:\a\r1\a\LinkedService.json 2020-07-28T14:12:59.3489081Z ##[debug]Processed: ##vso[task.logissue type=error]D:\a\r1\a\LinkedService.json 2020-07-28T14:12:59.4029653Z ##[debug]Exit code: 1 2020-07-28T14:12:59.4104968Z ##[debug]Leaving Invoke-VstsTool. 2020-07-28T14:12:59.4476571Z ##[error]PowerShell exited with code '1'. 2020-07-28T14:12:59.4477979Z ##[debug]Processed: ##vso[task.logissue type=error]PowerShell exited with code '1'. 2020-07-28T14:12:59.4575374Z ##[debug]Processed: ##vso[task.complete result=Failed]Error detected 2020-07-28T14:12:59.4603727Z ##[debug]Loading module from path 'D:\a_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\5.173.0\ps_modules\VstsAzureHelpers_\VstsAzureHelpers_.psm1'. 2020-07-28T14:12:59.4744776Z ##[debug]$OVERRIDING $global:DebugPreference from 'Continue' to 'SilentlyContinue'. 2020-07-28T14:12:59.4926089Z ##[debug]Loading resource strings from: D:\a_tasks\AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62\5.173.0\ps_modules\VstsAzureHelpers_/module.json 2020-07-28T14:12:59.5854494Z ##[debug]Loaded 13 strings.


Solution

  • How to provide file path of JSON file kept in azure devops git repository to PowerShell script.Getting file not found exception

    We could not use the predefined variables in the powershell scripts directly.

    To use predefined variables with powershell, we could use the inline powershell task or we could pass the value of predefined variables through PowerShell parameters:

    -pass $(System.DefaultWorkingDirectory)
    

    enter image description here

    And my test.ps1:

    Param(
     [String]$pass
    )
    
    Write-Host "$pass"
    

    The result:

    enter image description here