Home > Net >  Pass a script argument in Azure DevOps pipeline which runs a Powershell script
Pass a script argument in Azure DevOps pipeline which runs a Powershell script

Time:08-27

I have a simple Azure DevOps Pipeline

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:

- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'Azure-DevOps'
    ScriptType: 'FilePath'
    ScriptPath: 'az-ps-scripts/session-07/vnet/vnet.ps1'
    ScriptArguments: '-RGNAME RunFmPp'
    azurePowerShellVersion: 'LatestVersion'
    workingDirectory: 'az-ps-scripts/session-07/vnet/'

As you can see it has a ScriptArguments: '-RGNAME RunFmPp' and it runs vnet.ps1 script. But in fact, I do not know how can my vnet.ps1 script access this argument provided by this pipeline. This script creates a Resource Group for Azure, I would like tho use -RGNAME here:

$rg = @{
    Name = $RGNAME
}
New-AzResourceGroup @rg 

But with $RGNAME it does not work, so does anyone know how can this script acces this argument from azure devops pipeline?

CodePudding user response:

Declare your parameter(s) in a param() block at the top of your script:

param(
  [string]$RGNAME
)

$rg = @{
    Name = $RGNAME
    Location = $vnetConf.Location
}
New-AzResourceGroup @rg -Force
  • Related